Hello friends, Today in this post we will see the Linux commands that you should know as a web developer. This post will also help those who are new to Linux or if you are already familiar with Linux then it will help in brushing up your skills. Linux web servers are used widely used due to their security, low cost, open-source, etc so it is important for web developers to learn Linux commands to work with it. You could see Linux servers everywhere from AWS Cloud, Google Cloud, etc. Also, it is important to learn about shell scripts which is the programming language used in Linux so before getting into that we should understand the Linux commands. So let’s get started.
pwd— command to return the current directory where we are working in.
marish@CodeWithMarish:~$ pwd
/home/marish
ls— List all files and directories in the current working directory
marish@CodeWithMarish:~/test$ ls
file1.txt file2.txt test1
It has some useful options which are as follows:
-a — list all files including hidden files.
-l — long listing of file which shows permissions associated with the file, user or group to which it belongs, file size, and last modification
-t — list and sort files based on the last modification time(last modified first)
-r — reverses the sorting order
marish@CodeWithMarish:~$ ls -lrt
total 28
drwxr-xr-x 3 marish marish 4096 Jan 27 00:48 test
drwxr-xr-x 2 marish marish 4096 Jan 27 14:09 random
-rwxrw-r-- 1 marish marish 32 Jan 27 15:08 file.txt
The above command uses 3 options that we have seen.
mkdir— It will create a new directory in the current working directory
marish@CodeWithMarish:~$ mkdir test
-p — it will help in creating a child directory
Suppose we need to create a child directory, and if the parent directory does not exist, this option will help in creating a parent directory if not exists
marish@CodeWithMarish:~$ mkdir testing/test123
mkdir: cannot create directory ‘testing/test123’: No such file or directory
marish@CodeWithMarish:~$ mkdir -p testing/test123
Here, while creating directory test123 within testing we got an error since the testing directory does not exist. So we need to use option -p such that it will create a parent directory if not exists.
cd— Command to change directory
marish@CodeWithMarish:~$ cd testing
marish@CodeWithMarish:~/testing$
To move one directory up use below
marish@CodeWithMarish:~/testing$ cd ..
marish@CodeWithMarish:~$
~ — represents home directory so cd ~ will switch from the current directory to the home directory.
cat— stands for concatenate, commonly used for reading the content of the file and displaying it in the standard output system.
marish@CodeWithMarish:~$ cat file.txt
hello world
You can also create a file and write text using the below command
marish@CodeWithMarish:~$ cat > file1.txt
hello i am file1
After entering press Ctrl+c to exit.
If you want to append text to the end of the file use the below command
marish@CodeWithMarish:~$ cat >> file.txt
I am appended text
^C
marish@CodeWithMarish:~$ cat file.txt
hello world
I am appended text
mv— This command will help to move and rename files.
Below, I have moved file1.txt to the testing directory.
marish@CodeWithMarish:~$ mv file1.txt testing/
marish@CodeWithMarish:~$ cd testing
marish@CodeWithMarish:~/testing$ ls
file1.txt test123
To rename the file we will just move the file to the same directory with the new name as shown below
marish@CodeWithMarish:~/testing$ mv file1.txt newname.txt
marish@CodeWithMarish:~/testing$ ls
newname.txt test123
cp— Command to copy files.
marish@CodeWithMarish:~/testing$ cp newname.txt ../
marish@CodeWithMarish:~/testing$ ls
newname.txt test123
marish@CodeWithMarish:~/testing$ cd ..
marish@CodeWithMarish:~$ ls
file.txt newname.txt random test testing
We have some useful options such as:
-r — used to copy directories
marish@CodeWithMarish:~$ cp -r testing testing2
marish@CodeWithMarish:~$ ls
file.txt newname.txt random test testing testing2
-p — used to preserve metadata of file such as permission, modified time, owner, etc
marish@CodeWithMarish:~$ cp -p file.txt newfile.txt
marish@CodeWithMarish:~$ ls -lr
-rwxrw-r-- 1 marish marish 32 Jan 27 15:08 newfile.txt
-rwxrw-r-- 1 marish marish 32 Jan 27 15:08 file.txt
head— Returns first n lines from a file.
marish@CodeWithMarish:~/$ cat names.txt
KL Rahul
Sachin Tendulkar
Virat Kohli
Rohit Sharma
Hardik Pandya
marish@CodeWithMarish:~/$ head -n 2 names.txt
KL Rahul
Sachin Tendulkar
tail— Returns the last n lines from a file.
marish@CodeWithMarish:~/$ head -n 2 names.txt
Rohit Sharma
Hardik Pandya
chmod— Change permission for files.
marish@CodeWithMarish:~$ ls -l
total 32
-rwxrw-r-- 1 marish marish 32 Jan 27 15:08 file.txt
...
marish@CodeWithMarish:~$ chmod u=rx,g=w,o=x file.txt
marish@CodeWithMarish:~$ ls -l
total 32
-rwxrw-r-- 1 marish marish 32 Jan 27 15:08 file.txt
...
u -> user, g -> group, o -> others & r -> read, w -> write, x -> execute
You can also represent permission using octal representation.
4 — read, 2 — write, 1 — execute
adding the value will help us in combining permissions. for eg: if we we want read and write then 4+2=6
marish@CodeWithMarish:~$ chmod 475 file.txt
marish@CodeWithMarish:~$ ls -l
total 32
-r--rwxr-x 1 marish marish 32 Jan 27 15:08 file.txt
...
here in chmod 475 file.txt, first value 4 -. read which is for user, second value 7 -> read(4) + write(2) + execute(1), third value 5 -> read(4) + execute(1).
If we want to give no permissions then use 0 as below.
marish@CodeWithMarish:~$ chmod 000 file.txt
marish@CodeWithMarish:~$ ls -l
total 32
---------- 1 marish marish 32 Jan 27 15:08 file.txt
grep— searches for a specific pattern in a file and based on the option it returns the lines that are matching.
marish@CodeWithMarish:~$ grep 'K' names.txt
KL Rahul
Virat Kohli
-i — used for matching case insensitive.
marish@CodeWithMarish:~$ grep 'rohit' names.txt
marish@CodeWithMarish:~$ grep -i 'rohit' names.txt
Rohit Sharma
-w — used to match words
marish@CodeWithMarish:~$ grep -w 'Pandya' names.txt
Hardik Pandya
Krunal Pandya
-n — get line numbers matching the string/pattern.
marish@CodeWithMarish:~$ grep -n 'Pandya' names.txt
5:Hardik Pandya
6:Krunal Pandya
-c — to get the count of a number of matching string/patterns.
marish@CodeWithMarish:~$ grep -c 'Pandya' names.txt
2
-B n — to get the n lines before matching the string/pattern.
marish@CodeWithMarish:~$ grep -B 2 'Hardik' names.txt
Virat Kohli
Rohit Sharma
Hardik Pandya
find— Command to find files and directories and perform subsequent operations. To understand this better we will solve questions.
To find all files in the current directory and its subsequent child directories we use option -type to check whether it's a file or a directory after -the type we specify f means file if we want to search only directory then mention d.
marish@CodeWithMarish:~$ find . -type f
./file.txt
./newfile.txt
./testing/newname.txt
./names.txt
./test/test1/random.txt
./test/file1.txt
./test/file2.txt
./testing2/newname.txt
./newname.txt
Now we need to find all the files which have names starting with “file” for that we use option -name and then in double-quotes mention file and *(represents placeholder which can have anything).
marish@CodeWithMarish:~$ find -type f -name "file*"
./file.txt
./test/file1.txt
./test/file2.txt
To find all files which have specific permissions such as 644.
marish@CodeWithMarish:~$ find . -type f -perm 644
./names.txt
./test/file1.txt
./test/file2.txt
sort— It is used to sort the contents of files. The below example demonstrates the use of the sort command. By default, it will sort in ascending order. Use option -r if you want the content to be in reversed order.
marish@CodeWithMarish:~$ cat names.txt
KL Rahul
Sachin Tendulkar
Virat Kohli
Rohit Sharma
Hardik Pandya
Krunal Pandya
marish@CodeWithMarish:~$ sort names.txt
Hardik Pandya
KL Rahul
Krunal Pandya
Rohit Sharma
Sachin Tendulkar
Virat Kohli
marish@CodeWithMarish:~$ sort -r names.txt
Virat Kohli
Sachin Tendulkar
Rohit Sharma
Krunal Pandya
KL Rahul
Hardik Pandya
df— Command to check the amount of disk space consumed by the file system. Use -h option for human-readable format
marish@CodeWithMarish:~$ df -h /home/marish
Filesystem Size Used Avail Use% Mounted on
/dev/sdb 251G 2.2G 237G 1% /
If you don’t specify the file/directory then it will show all the available disk space consumption
marish@CodeWithMarish:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sdb 251G 2.2G 237G 1% /
tools 442G 226G 217G 52% /init
drivers 442G 226G 217G 52% /usr/lib/wsl/drivers
lib 442G 226G 217G 52% /usr/lib/wsl/lib
du— It is used to check the disk space consumed by files and directories. use -h for human-readable format. Below we have not mentioned any files or directories then it will work on files and directories in the current working directory
marish@CodeWithMarish:~$ du -h
8.0K ./random
4.0K ./testing/test123
12K ./testing
4.0K ./testing2/test123
12K ./testing2
Suppose we need to know the total space consumed by a specific file/directory then use option -s
marish@CodeWithMarish:~$ du -sh /home/marish
136K /home/marish
chown— Command to change owners of files/directories.
marish@CodeWithMarish:~$ ls -l
total 32
-rwxrw-r-- 1 marish marish 32 Jan 27 15:08 file.txt
-rw-r--r-- 1 marish marish 79 Jan 27 22:29 names.txt
-rwxrw-r-- 1 marish marish 32 Jan 27 15:08 newfile.txt
marish@CodeWithMarish:~$ chown root names.txt
chown: changing ownership of 'names.txt': Operation not permitted
marish@CodeWithMarish:~$ sudo chown root names.txt
[sudo] password for marish:
marish@CodeWithMarish:~$ ls -l
total 32
-rwxrw-r-- 1 marish marish 32 Jan 27 15:08 file.txt
-rw-r--r-- 1 root marish 79 Jan 27 22:29 names.txt
-rwxrw-r-- 1 marish marish 32 Jan 27 15:08 newfile.txt
Above, I have changed the owner of names.txt from marish to root, as it is a root user command we need to execute it using sudo.
whoami— command to get the name of the currently logged-in user.
marish@CodeWithMarish:~$ whoami
marish
uname— It is used to get information about the operating system and system hardware.
marish@CodeWithMarish:~$ uname
Linux
marish@CodeWithMarish:~$ uname -o
GNU/Linux
marish@CodeWithMarish:~$ uname -r
5.4.72-microsoft-standard-WSL2
marish@CodeWithMarish:~$ uname -n
CodeWithMarish
marish@CodeWithMarish:~$ uname -m
x86_64
marish@CodeWithMarish:~$ uname -a
Linux CodeWithMarish 5.4.72-microsoft-standard-WSL2 #1 SMP Wed Oct 28 23:40:43 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
Above, uname without option will return kernel name. The description for options is as follows.
-o — to get the name of the operating system
-r — to get kernel release
n — to get the hostname
-m — to get the architecture of the system
- a — if you don’t remember all the options, just use -a it will return all the information such as kernel name, hostname, kernel release, kernel version, architecture, os name
free— It displays the amount of free memory space available in the system along with the used memory space. use option -h for human-readable format.
marish@CodeWithMarish:~$ free -h
total used free shared buff/cache available
Mem: 3.8Gi 75Mi 3.7Gi 0.0Ki 59Mi 3.6Gi
Swap: 1.0Gi 0B 1.0Gi
top— List all the running Linux processes and other information such as load average, CPU & memory used, etc. It also shows the process in real-time.
top - 07:48:01 up 31 min, 0 users, load average: 0.00, 0.00, 0.00
Tasks: 5 total, 1 running, 4 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3873.0 total, 3736.4 free, 76.8 used, 59.7 buff/cache
MiB Swap: 1024.0 total, 1024.0 free, 0.0 used. 3653.1 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 1740 1084 1016 S 0.0 0.0 0:00.05 init
7 root 20 0 1760 72 0 S 0.0 0.0 0:00.00 init
8 root 20 0 1760 88 0 S 0.0 0.0 0:00.10 init
9 marish 20 0 10052 4996 3280 S 0.0 0.1 0:00.13 bash
91 marish 20 0 10864 3740 3228 R 0.0 0.1 0:00.10 top
lsof— Displays a list of all open files.
marish@CodeWithMarish:~$ lsof
COMMAND PID TID TASKCMD USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 9 marish cwd DIR 8,16 4096 664 /home/marish
bash 9 marish rtd DIR 8,16 4096 2 /
bash 9 marish txt REG 8,16 1183448 1602 /usr/bin/bash
bash 9 marish mem REG 8,16 51832 11403 /usr/lib/x86_64-linux
If we want to get only for a specific user use option -u and then specify username
marish@CodeWithMarish:~$ lsof -u marish
COMMAND PID TID TASKCMD USER FD TYPE DEVICE SIZE/OFF NODE NAME
bash 9 marish cwd DIR 8,16 4096 664 /home/marish
bash 9 marish rtd DIR 8,16 4096 2 /
bash 9 marish txt REG 8,16 1183448 1602 /usr/bin/bash
bash 9 marish mem REG 8,16 51832 11403 /usr/lib/x86_64-linux
htop— It is more of an interactive version of the top command which provides an additional option to search, filter, kill, etc in an interactive way.
kill — It is used to kill a process.
marish@CodeWithMarish:~$ kill process_id
lscpu— To get the CPU details of the system.
marish@CodeWithMarish:~$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 39 bits physical, 48 bits virtual
CPU(s): 8
...
To get the details about the operating system,
marish@CodeWithMarish:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04 LTS"
hostname— To get the hostname of the system. You can use -i to get the IP address of the hostname.
marish@CodeWithMarish:~$ hostname
CodeWithMarish
marish@CodeWithMarish:~$ hostname -i
127.0.1.1
ps — It is used to get all the currently running processes of the system.
marish@CodeWithMarish:~$ ps
PID TTY TIME CMD
9 pts/0 00:00:00 bash
109 pts/0 00:00:00 ps
PID — process ID, TTY = terminal type, TIME — process running time, CMD — the command that started the process
marish@CodeWithMarish:~$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 1740 1084 ? Sl 07:16 0:00 /init
root 7 0.0 0.0 1760 72 ? Ss 07:16 0:00 /init
root 8 0.0 0.0 1760 88 ? S 07:16 0:00 /init
marish 9 0.0 0.1 10052 5000 pts/0 Ss 07:16 0:00 -bash
marish 128 0.0 0.0 10608 3348 pts/0 R+ 08:38 0:00 ps aux
The above command will return all processes along with some additional information such as CPU and memory used.
useradd— It is used to add a new user.
marish@CodeWithMarish:~$ sudo useradd marish2
[sudo] password for marish:
passwd— Used to set the password for new users.
marish@CodeWithMarish:~$ sudo passwd marish2
New password:
Retype new password:
passwd: password updated successfully
userdel— It is used to delete a user.
marish@CodeWithMarish:~$ sudo userdel marish2
history— It is used to return all the previously executed commands.
marish@CodeWithMarish:~$ history
1 ls
2 ls
3 pwd
4 mkdir testing
5 mkdir -p test/test1/
vmstat— It is used to get the virtual memory statistics.
marish@CodeWithMarish:~$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 3783392 19820 79028 0 0 1 45 1 3 0 0 100 0 0
wget— It is used to download files from the internet. In the below example I have downloaded the index.html file from my blogs i.e codewithmarish.com. wget requires an URL to download from so I passed the URL https://codewithmarish.com
marish@CodeWithMarish:~$ ls
file.txt names.txt newfile.txt newname.txt random test testing testing2
marish@CodeWithMarish:~$ wget https://codewithmarish.com
--2022-01-29 11:58:45-- https://codewithmarish.com
Resolving codewithmarish.com (codewithmarish.com)... xx.xx.xx.xxx, xx.xxx.xxx.xx, xxx.xx.xx.x, ...
Connecting to codewithmarish.com (codewithmarish.com)|xx.xx.xx.xxx|:xxx... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8553 (8.4K) [text/html]
Saving to: ‘index.html’
index.html 100%[================================================>] 8.35K --.-KB/s in 0.001s
2022-01-29 11:58:46 (6.00 MB/s) - ‘index.html’ saved [8553/8553]
marish@CodeWithMarish:~$ ls
file.txt index.html names.txt newfile.txt newname.txt random test testing testing2
alias— It is used to get all shortcuts created for the command. It will be act as an proxy for a command. Suppose if we have long command which you use frequently, in this case we will create an alias for that command.
To get all aliases
marish@CodeWithMarish:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
To create a new alias, below I have created an alias for echo “Hello World” as hello so whenever I type hello in the terminal it will execute echo “Hello World”
marish@CodeWithMarish:~$ alias hello="echo Hello World"
marish@CodeWithMarish:~$ hello
Hello World
marish@CodeWithMarish:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias hello='echo Hello World'
ifconfig— ifconfig ( Interface Configuration ) is used for viewing network interface configuration.
nslookup— It is used to query domain name servers.
marish@CodeWithMarish:~$ nslookup codewithmarish.com
Server: xxx.xx.xxx.x
Address: xxx.xx.xx.x#xx
Non-authoritative answer:
Name: codewithmarish.com
Address: xx.xx.xx.xx
Name: codewithmarish.com
Address: xx.xx.xx.xx
Name: ns1.vercel-dns.com
Address: xxx.xx.xx.x
Name: ns2.vercel-dns.com
Address: xxx.xx.xx.x
man— It is used to get the manual page for a command
marish@CodeWithMarish:~$ man ls
Thanks for reading this post, if you found this post helpful please share maximum, Thanks for reading 😊 Stay tuned.
If you are facing any issues please contact us from our contact section.
Also please don’t forget to subscribe to our youtube channel codewithmarish for all web development-related challenges.