Table of contents
Commonly used Linux Commands
ls - List files and directories in the current directory.
cd - Change directory. You can use cd followed by a directory name or a path to navigate to a different directory.
pwd -Print the current working directory.
uname - It shows the name of the kernel
clear - It uses for clear screen
whoami - It shows the current login user name
date - It shows time and date
mkdir - Create a new directory. You can specify the directory name as an argument.
touch - Create an empty file or update the timestamp of an existing file. You can specify the file name as an argument.
df - displays the amount of disk space available on the filesystem
id - display the user ID (UID) and group ID (GID)
cp - Copy files or directories. You need to specify the source and destination file/directory names.
cp <option><source> destination #Options: # -r for recursive # -v for verbose # -f for forcefully
mv - Move or rename files or directories. You can use it to both move and rename files/directories.
mv <option><source> destination #Options: # -r for recursive # -v for verbose # -f for forcefully
rm - Remove files or directories. Be careful when using this command, as it permanently deletes files/directories.
rm <option><source> destination #Options: # -r for recursive # -v for verbose # -f for forcefully
cat - Display the contents of a file on the terminal. It helps us to list, combine and write file content to standard output.
#creates a new file cat > file1.txt #copy the content of file1 to file2 cat file1.txt > file2.txt
grep - Search for a pattern in a file or a group of files.
grep <options> <search pattern> <files> #Options: # -i -> Ignore case during search # -r, -R -> Search recursively # -v -> invert match, match everything except the pattern # -l -> List files that match the pattern # -L -> List files that do not match the pattern # -n -> Prefix each line of output with the line number
chmod - Change the permissions of a file or directory. You can use it to set read, write, and execute permissions for different users.
chmod <permission> <file/directory name> #Set permission with numeric value # r(read) = 4 # w(write) = 2 # x(execute) =1
chown - Change the owner of the file or directory.
chown <username> <file/directory name>
sudo - Execute a command with administrative privileges. It's often used to perform tasks that require root access.
man - Display the manual page for a command. It provides detailed documentation and usage instructions for various commands.
history - Show a list of previously executed commands.
top - display the top process running in the system
wget - Download files from the internet using the command line. These are just a few basic Linux commands to get you started.
useradd - To create a new user account
passwd - To create user account password
userdel - To delete user account
groupadd - For add Group account
groupdel - For delete group account
find - find command used to search and locate list of files and directories based on conditions applied
find <directory> -name <permission> find <directory> -perm <permission> find <directory> -user <user> find <directory> -size -10M
locate - search a file by file name in the database, whereas the find command searches in the file system
find <file_name>
awk - searches files for text containing a pattern
awk '{action}' your_file_name.txt #search for text that has a specific pattern awk '/regex pattern/{action}' your_file_name.txt #Built in Variable # NR: Keeps a current count of the number of input records. # NF: Keeps a count of the number of fields within the current input record. # FS: Contains the field separator character which is used to divide fields on the input line. # RS: Stores the current record separator character. # OFS: Stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. # ORS: Stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character.
wc - the wc(word count) is use for the count and line numbers
#count number of lines wc -l <directory> #count number of words wc -w <directory>
head - Head command is used for to display top line of the file
head <directory> head -n count <directory>
tail - Tail command is used for to display the bottom line of the file
tail<directory> tail -n count <directory>
tar - tar is used to compress size or drive backup
tar -<options> <files> #Options: # c - for create # x - for extract # v - for verbose # f - for forcefully # t - for test # z - for gzip # j - for bz2 # J - for xz # C - for specific destination
crontab - used for to execute job multiple time
#for set cron jobs crontab -e #for show cron jobs of current user crontab -l # removing a existing cronjob crontab -r
sudo - sudo allows us to run a program as a different user. this command runs through root privilege.
ip addr - for showing ip address
cal - display the current month's calendar with the current date highlighted
vim - vim editor used for editing a file
# Insert mode -> i # Quit -> q # Save & Exit -> wq
Task:
Linux command to view what’s written in a file.
~ cat
Linux command to change the access permissions of files.
~ chmod
Linux command to check which commands you have run till now.
~ history
Linux command to remove a directory/ Folder.
~ rmdir
To create a fruits.txt file and to view the content.
~ vim fruits.txt
Add content in devops.txt (One in each line) — Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
~ vim devops.txt and add fruits name one by one.
To show only the top three fruits from the file
~ cat devops.txt | head -3
To Show only the bottom three fruits from the file.
~ cat devops.txt | tail -3
To create another file Colors.txt and to view the content. Add content in Colors.txt (One in each line) — Red, Pink, White, Black, Blue, Orange, Purple, Grey
~ vim Colors.txt and add colors names one by one and cat Colors.txt to view the content
To find the difference between fruits.txt and Colors.txt files.
~ diff fruits.txt colors.txt
Thank You,
Abhisek Moharana