Essential Linux Commands


Linux commands form the basics of the DevOps journey. Anyone who is in the computer science field must be having a good knowledge of the Linux commands and their working. If you are a Linux/Mac user, then it will be pretty easy for you to follow and practice the commands in this blog.
But if you don't have a Linux or a MAC operating system, then there is no need to worry about it. You can simply download the windows subsystem for Linux and still be able to execute these commands.
What are commands? Why need them?
Commands are just the basics which are performed on our computer through the operating system. Basically, through the commands, we can instruct our operating system to perform a particular kind of action. This can be anything, like opening a file, creating directories or creating a file through the command line, which we are going to see further in this blog.
One question that you might have now is that if the above-listed tasks can be done normally by right-clicking and creating a new folder or a file, then why execute all of these actions in the form of commands using a command line interface (CLI) rather than using a graphical user interface (GUI). The simple and short answer is - it helps us to manage and do some of the configuration and management work easily and quickly as compared to the traditional GUI method which can sometimes require us to move from one directory to another frequently.
Another advantage of CLI is that we can create shell scripts using various text editors like Vim, Nano etc.
Basic Terminologies
Before moving forward and working with some of the Linux commands, let us see some basic terminologies which will be useful further.
- Terminal Emulator
A terminal emulator is a program which will be providing us with access to the terminal in a graphical way. Example - xterm.
- Shell
A shell in Linux is a command line interface which will be taking commands from the user and give them to the operating system for further action on that command. It will also show the output of the command. Examples - Bourne Shell, Bash shell etc.
- Directory
A directory is simply a folder structure in Linux which can have subdirectories and some files in it.
- Path
Path specifies the location where a particular file or folder is present.
- Environment variables
Named values which specify how the processes will be executed. Example - PATH environment variable, which specifies the directories or the locations that are to be checked to find an executable command such as "git" or "python3".
- Superuser (sudo)
For some of the commands, higher-level access is required. This requires us to type sudo at the beginning of the commands for user authentication purposes.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ echo $PATH
/home/sagar/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
Some Basic Linux Commands
ls
It is used to list all the files and folders in the current path/location at which you are. Along with this, other flags can be used to show hidden files, or display a long list of information.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls one.txt sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls -l total 4 -rw-rw-r-- 1 sagar sagar 39 Dec 9 03:38 one.txt sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls -a . .. one.txt sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls -la total 56 drwxrwxr-x 2 sagar sagar 4096 Dec 9 03:38 . drwxr-xr-x 18 sagar sagar 49152 Dec 9 04:05 .. -rw-rw-r-- 1 sagar sagar 39 Dec 9 03:38 one.txt
pwd - print working directory
Tells us the current path, or the directory in which we are.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ pwd /home/sagar/temp
man
man command is useful when you want to get information about a particular command.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ man ls LS(1) User Commands LS(1) NAME ls - list directory contents SYNOPSIS ls [OPTION]... [FILE]... DESCRIPTION
cd - Change directory
cd can be used to move in different locations/directories. "cd .." will move one directory backwards.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ cd .. sagar@sagar-HP-Laptop-14s-dk0xxx:~$ cd /home/sagar/temp/ sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ pwd /home/sagar/temp
mkdir - Make a new folder
mkdir can be used to create a new folder.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ mkdir hashnode sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls hashnode one.txt
touch
The touch command is similar to creating a new file such as a text file.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ touch names.txt surnames.txt hello.cpp sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls hashnode hello.cpp names.txt one.txt surnames.txt
echo - print a string
echo command prints a string on the standard output
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ echo This is a hashnode blog This is a hashnode blog
where / whereis [executable_file_name]
To find where a particular executable file is present, where command can be used (wheris for Linux).
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ whereis git git: /usr/bin/git /usr/share/man/man1/git.1.gz
cat - short for concatenation
This can be used to quickly display the content of a file. If the">" symbol is used after the cat command, then it creates the file if not present and also allows us to add the contents in the file at the same time.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ cat one.txt hello this is the first file in linux entering some sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ cat > new_file.txt This is a new file being created quickly !! ^C sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ cat new_file.txt This is a new file being created quickly !!
tr - translate characters
We can convert the characters in one file to uppercase, or lowercase using the "tr" command. Note that the " | " operator used here is known as the Pipe operator. It is used to give the output of one command as the input of some other command.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ cat one.txt | tr a-z A-Z HELLO THIS IS THE FIRST FILE IN LINUX ENTERING SOME
cp - copy command
It is used to copy the contents of one file into another file. The first argument is the source file and the second argument is the destination file.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ cp hello.cpp ./hashnode/hello2.cpp sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ cd hashnode/ sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp/hashnode$ ls hello2.cpp
mv - move the files
This command moves the files or the directories from one location to another. In this example, the hello2.cpp file has been moved from the hashnode folder to the previous directory. This can also be used to rename a file.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp/hashnode$ mv hello2.cpp ../ sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp/hashnode$ ls sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp/hashnode$ cd .. sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls hashnode hello2.cpp hello.cpp names.txt new_file.txt one.txt surnames.txt
rm - removes files and folder
This command can be used to remove the files and folders. For removing a folder we have to use the -f flag. To remove the subdirectories and subfolders recursively, we have to use the -r flag.
⚠️ Note that the deletion is permanent. So proceed with caution! ⚠️
agar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls hello2.cpp hello.cpp names.txt new_file.txt one.txt surnames.txt sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ mkdir hashnode sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ rm surnames.txt sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ rm -rf hashnode/ sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls hello2.cpp hello.cpp names.txt new_file.txt one.txt
df - usage monitoring
This command reports to us about the overall disk space usage. The -h flag is used to display the data in a human-readable format.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ df -H Filesystem Size Used Avail Use% Mounted on tmpfs 613M 2.3M 611M 1% /run /dev/sda2 237G 32G 194G 14% / tmpfs 3.1G 123M 3.0G 4% /dev/shm tmpfs 5.3M 4.1k 5.3M 1% /run/lock /dev/nvme0n1p1 269M 91M 179M 34% /boot/efi tmpfs 613M 111k 613M 1% /run/user/1000
du - fie monitoring
This command reports to us about file space usage. Tells the size of the current folder and the subfolders or files.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ du -h 8.0K ./new_Dir 20K .
head
Displays only the first 10 lines of a file by default. To change this default number, use the -n flag.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ head -n 3 names.txt hello this is the first file in linux entering some
tail
Works in the opposite manner of the head command displaying the last 10 lines of a file by default. This number can be changed using the -n flag.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ tail -n 5 names.txt sagar harsh kunal kushwaha
diff
Compares two files line by line and the output shows the lines which do not match in the two files.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ vim names2.txt sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ diff names.txt names2.txt 1,2c1,2 < sagar < wadhwa --- > kunal > kushwaha
locate
locate command can be used to locate the particular file(s).
Example - locate "*.txt"
The above example will find the locations of all the files which have anything in their name but end with ".txt". " * " is a wildcard character.
find
find is used to find all the stuff at a location.
The example below finds all the files in the current directory which have a name like "*.txt", are of file type and were modified less than 20 minutes ago.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ find . -name "*.txt" -mmin -20 -type f ./names.txt ./names2.txt
chmod
Every file or folder has certain permissions related to it which specify the read, write or execute access which is given to the user, group, or other people. The permission can be changed by typing characters or numerically.
Numerically, the first number denotes the access given to the user, the second place number denotes the access given to the group and the third one is for the others.
The number system for this is octal (0-7) where 4 denotes a read permission, 2 denotes a write permission and 1 denotes an execute permission
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls -l total 8 -rw-rw-r-- 1 sagar sagar 31 Dec 9 14:18 names2.txt -rw-rw-r-- 1 sagar sagar 29 Dec 9 14:18 names.txt sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ chmod u=rwx,g=rwx,o=rw names2.txt sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls -l total 8 -rwxrwxrw- 1 sagar sagar 31 Dec 9 14:18 names2.txt -rw-rw-r-- 1 sagar sagar 29 Dec 9 14:18 names.txt sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ chmod 577 names2.txt sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls -l total 8 -r-xrwxrwx 1 sagar sagar 31 Dec 9 14:18 names2.txt -rw-rw-r-- 1 sagar sagar 29 Dec 9 14:18 names.txt
chown - change owner
Used to change the owner of a file or a directory. Requires superuser access.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ chown root names.txt chown: changing ownership of 'names.txt': Operation not permitted sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ sudo chown root names.txt [sudo] password for sagar: sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ ls -l names.txt -rw-rw-r-- 1 root sagar 29 Dec 9 14:18 names.txt sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$
grep - Global regular expression print
It prints lines that match a specified pattern. -i flag ignores the case, and the -n flag prints the line number also. Other flags can be seen from the man command help text.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ grep -in "s" names.txt 1:sagar 3:is
history
Shows the history of the previous commands used. Type " !! " to execute the last command again. Type " ![command_number]" to execute a particular command from the history of commands.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ history 1123 linux 1124 ubunty 1125 where 1126 whoami 1127 echo hello world sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ !! sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ !1123
alias
Used to give another name to an existing command.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ alias alias l='ls -CF' alias la='ls -A' alias ll='ls -alF' alias ls='ls --color=auto' sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ alias ls='ls -a'
top
Shows all the processes that are running in the system.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$top
wget
Used to download certain files from the internet. The argument takes the web address from where the file has to be downloaded.
hostname
Shows or set's the hostname of the system.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ hostname sagar-HP-Laptop-14s-dk0xxx
free
Displays the amount of free and used memory in the system.
id
Used to print the id of the user and the groups.
sagar@sagar-HP-Laptop-14s-dk0xxx:~/temp$ id uid=1000(sagar) gid=1000(sagar) groups=1000(sagar),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),120(lpadmin),131(lxd),132(sambashare)
Summary
Many essential Linux commands have been discussed in this blog, but there is a lot more to cover, all of which can't be done here. You can try out these commands on your system and also explore the new ones. 🤩
For a more in-depth explanation of these commands, you can watch this amazing video by Kunal Kushwaha. 🙌
Hope you enjoyed learning about various Linux commands and trying them out. Don't forget to like this blog and share it with others! Thanks for reading ! 😊😊
Subscribe to my newsletter
Read articles from Sagar Wadhwa directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sagar Wadhwa
Sagar Wadhwa
✨Avid Learner 👨💻Associate Programmer Analyst @ Moody's 🧑🎓B. Tech CSE, Amity Noida '24