40 Useful Linux Commands for Everyone
⇩
1. cd
. ⬘ It stands for “change directory”.
⬙ It changes the working directory to the specified one.
❯ cd images
Switch to the “images” directory
❯ cd …
Switch to the previous directory
❯ cd
Switch to the parent directory
❯ cd
Switch to the home directory
2. mkdir
⬘ It stands for “make directory”.
⬙ It creates a directory inside the current directory.
❯ mkdir data/
Creates a single directory
❯ mkdir -p data/revenue/
Creates entire directory structure
3. pwd
⬘ It stands for “print working directory”.
⬙ It outputs the absolute path of the directory you are in.
❯ pwd
/home/swapnakpanda/
4. ls
⬘ It stands for “list”
⬙ It lists out all files and, directories that exists inside a directory.
❯ ls
Lists all inside current working directory
❯ ls /home/
Lists all inside /home/
❯ ls -a
Lists all including hidden files
5 cp
⬘ It stands for “copy”
⬙ It copies a file or, directory to another place.
❯ cp file1.txt file2.txt
Copies the file file1.txt to file2.txt
❯ cp -r dir1/ dir2/
Copies entire directory “dir1” to “dir2”
6 mv
⬘ It stands for “move”.
⬗ It moves files and, directories to another directory.
⬙ It can be used to rename a file/directory.
❯ mv file1.js images/
Moves the file
❯ mv file1.js file2.js
Renames the file
7 touch
⬘ It’s used to create a new empty file.
⬙ It can also be used to change the modification date of a file to current time.
❯ touch new_file
Creates a new empty file “new_file”
❯ touch -m old_file
Changes the modification time
8 cat
⬘ It stands for “concatenate”.
⬙ It is used to view, create and concatenate files.
❯ cat file.txt
Lists the contents of file1.txt to standard output
❯ cat > file.txt
Creates a new file
❯ cat file1 file2 > file3
Concatenates file1 & file2 to file3
9 rm
⬘ It stands for “remove”.
⬙ It’s used to remove files and, directories.
❯ rm file1.txt
Removes a file
❯ rm -r dir1
Removes an empty directory
❯ rm -rf dir2
Removes any directory
10 find
⬘ It’s used to search for files in a directory hierarchy based on regular expression.
❯ find ./ -name “sha.txt”
Searches for anything whose name is “sha.txt”
❯ find ./ -type f -name “*.py”
Searches for files whose name ends with “.py”
11 grep
⬘ It searches for a line in a file that matches a regular expression.
⬙ Grep stands for Global Regular Expression Print
❯ grep “hello” message.txt
Lists all lines that contain “hello”
❯ grep -c “hi” message.txt
Displays the # of lines that contain “hi”
12 head
⬘ It outputs first few lines of a file.
❯ head message.txt
Outputs first 10 lines
❯ head -n 5 message.txt
Outputs first 5 lines
13 tail
⬘ It outputs the last few lines of a file.
❯ tail message.txt
Outputs last 10 lines
❯ tail -n 5 message.txt
Outputs last 5 lines
❯ tail -f app.log
Shows new entries in app.log
as and when they get added
14 diff
⬘ It is used to compare the contents of two files line by line.
❯ diff /dir1/source.java /dir2/source.java
Lists out all differences
15 alias
⬘ It allows you to define temporary aliases for the current session.
⬙ Aliases allow you to execute one or, more commands.
❯ alias lsa=“ls -a”
Creates a new alias “lsa”
❯ alias
Lists all aliases for the current session
16 chmod
⬘ It lets you change the mode (permission) of a file.
⬙ The basic permissions a file can have are
❍ r (read)
❍ w (write)
❍ x (execute)
❯ chmod +x “hello.s”
Makes the file executable
17 sudo
⬘ It stands for “superuser do”.
⬙ It allows you to act as a superuser or, a root user while you are running a specific command.
❯ sudo apt install python
18 tar
❯ tar -cvf abc.tar *.c
Creates a tar file (abc.tar) using all files with extension .c
❯ tar -xvf abc.tar
Extract files from abc.tar
❯ tar -cvzf abc.tar.gz *.c
Creates a tar file using gzip.
❯ tar -xvzf abc.tar.gz
Extract files from a gzip tar file.
19 echo
It displays the given text in the terminal
20. which
Shows the directory where the command is present
21 whoami
Displays the username who currently is using this session
22 man
Displays the manual page of any command
23 chown
To change the ownership
24 du
To check how much space a file or a directory takes
25 ps
Lists the processes that the current shell session is running
26 kill
To kill (terminate) a process
27 free
Show the amount of free memory
28 vmstat 4
Every 4 seconds, show statistics
29 iotop
Displays the disk IO usage details
30. systemctl
Manage systemd and services
31 journalctl
To view systemd, kernel and journal logs
32 env
Prints list of environment variables
33 host
Used for DNS lookup operations
34 hostname
Used to obtain the DNS name
35 ping
Checks the network connectivity b/w host and server
36 curl
To transfer data to or from a server
37 wget
To retrieve/download content from the internet
38 history
Displays the commands you used in the past.
39 clear
Clear the terminal window
40. exit
It ends a shell session and, closes the terminal you are using.
Hey 👋
I am a Tech Educator and, Mentor from India 🇮🇳
Here, I am sharing Tutorials, Tips, Techniques, Infographics, Cheat Sheets, Interview Questions, Project Ideas and Roadmaps on Web Development, DSA and, Databases.