When dealing with Linux, you need to use a shell – an interface that gives you access to the operating system. The commands are required as inputs to inform or direct a computer program to perform a specific operation.
While most Linux distributions are user-friendly and come with an easy-to-use graphical interface, knowing how to use the command line can be very useful.
So let’s learn the must-know basic Linux commands with examples.
1. cd
cd (Change Directory) command in Linux is one of the most important and widely used commands. It is used to change the current working directory.
Change from the current directory to /tmp
.
cd /tmp
Switch back to the previous directory where you were working earlier.
cd -
Change Current directory to parent directory.
cd ..
Move to the user’s home directory from anywhere.
cd
2. pwd
pwd (Print Working Directory), as the name, states, prints the name of the present/current working directory. It prints the path, starting from the root /
.
pwd
/home/linuxiac
3. ls
ls (List Files and Directories) is one of the basic commands that any Linux user should know. It lists the content of a directory, such as files and folders.
Running ls
without parameters will list the content of the current directory.
ls
psforevermore.txt pulse-linux-9.1r2.0-x64.rpm website-logo.jpg
Code language: CSS (css)
Using the -l
(long format) option will display a long listing of the content of the current directory. The command will print not only the name of the file but also some attributes such as:
- permissions
- owner
- group owner
- size of the file in bytes
- time and date the file is modified.
ls -l
total 22968
-rw-r--r-- 1 linuxiac <meta http-equiv="content-type" content="text/html; charset=utf-8">linuxiac 19 Jul 27 13:53 psforevermore.txt
-rw-r--r-- 1 <meta http-equiv="content-type" content="text/html; charset=utf-8">linuxiac <meta http-equiv="content-type" content="text/html; charset=utf-8">linuxiac 23271352 Jul 28 14:57 pulse-linux-9.1r2.0-x64.rpm
-rw-r--r-- 1 <meta http-equiv="content-type" content="text/html; charset=utf-8">linuxiac <meta http-equiv="content-type" content="text/html; charset=utf-8">linuxiac 240104 Jul 28 15:12 website-logo.jpg
Code language: HTML, XML (xml)
To list the content of a particular directory refer the below command.
ls -l /home/linuxiac/
<meta http-equiv="content-type" content="text/html; charset=utf-8">total 22968
-rw-r--r-- 1 linuxiac <meta http-equiv="content-type" content="text/html; charset=utf-8">linuxiac 19 Jul 27 13:53 psforevermore.txt
-rw-r--r-- 1 <meta http-equiv="content-type" content="text/html; charset=utf-8">linuxiac <meta http-equiv="content-type" content="text/html; charset=utf-8">linuxiac 23271352 Jul 28 14:57 pulse-linux-9.1r2.0-x64.rpm
-rw-r--r-- 1 <meta http-equiv="content-type" content="text/html; charset=utf-8">linuxiac <meta http-equiv="content-type" content="text/html; charset=utf-8">linuxiac 240104 Jul 28 15:12 website-logo.jpg
Code language: HTML, XML (xml)
With the -h
option, ls
will display file sizes in a human-readable format. This option is only meaningful when combined with the -l
option.
ls -lh
total 23M
-rw-r--r-- 1 linuxiac linuxiac 19 Jul 27 13:53 psforevermore.txt
-rw-r--r-- 1 linuxiac linuxiac 23M Jul 27 <meta http-equiv="content-type" content="text/html; charset=utf-8">14:57 pulse-linux-9.1r2.0-x64.rpm
-rw-r--r-- 1 linuxiac linuxiac 235K Jul 27 <meta http-equiv="content-type" content="text/html; charset=utf-8">15:12 website-logo.jpg
Code language: HTML, XML (xml)
In Linux, a file that begins with .
is a hidden file. To show it on the ls
command, we can use the -a
parameter.
ls -a
.monitoring psforevermore.txt pulse-linux-9.1r2.0-x64.rpm .usage.log website-logo.jpg
Code language: CSS (css)
If we want to list directory entries only, we can use -d
parameter.
ls -d /home/linuxiac/
/home/linuxiac/
4. cp
cp (Copy) is a command used for copying files and directories in Linux. To copy a file with the cp
command, pass the file’s name to be copied and then the destination.
For example, running the below-mentioned command will copy a file website-logo.jpg
to a /tmp/
directory.
cp website-logo.jpg /tmp/
You need to specify the desired file name if you want to copy the file under a different name.
<meta http-equiv="content-type" content="text/html; charset=utf-8">cp website-logo.jpg /tmp/new-logo.jpg
Code language: HTML, XML (xml)
To copy multiple files, pass the names of files followed by the destination directory to the cp
command.
cp website-logo.jpg psforevermore.txt images/
To copy a directory, including all its files and subdirectories, use the -r
(recursive) option.
For example, we are copying the directory images
to images_bckp
.
cp -r images/ images_bckp/
5. mv
mv (Move) is used to move one or more files or directories from one place to another. Apart from moving the files, it can also rename a file or directory.
For instance, to move a file named website-logo.jpg
from the current directory to the images
directory, the command would be:
mv website-logo.jpg images/
If you want to rename a file named website-logo.jpg
to new-logo.jpg
, you can use the mv
command in the following way:
<meta http-equiv="content-type" content="text/html; charset=utf-8">mv website-logo.jpg new-logo.jpg
Code language: HTML, XML (xml)
Like renaming a file, you can rename a directory using the mv
command.
For example, to rename a directory named images
to images_bckp
, the command would be:
mv images images_bckp
6. rm
rm (Remove) is a command-line utility for removing files and directories.
Related: How to Delete Files and Directories in Linux from Command Line
To delete a single file, use the rm
command followed by the file name as an argument:
rm website-logo.jpg
Code language: CSS (css)
By default, rm
does not remove directories. However, if the -r
(recursive) option is presented, rm
will remove any matching directories and their contents.
rm -r images/
The rm
command will prompt you for confirmation if the given directory or a file within the directory is write-protected.
To remove a directory named images
without being prompted, use the -f
option:
rm -rf images/
Attention: Be careful when you are executing the rm -rf
command. A little typo or ignorance may result in unrecoverable system damage.
7. mkdir
mkdir (Make Directory) is the primary Linux command for creating a directory. While the rm
command lets you delete directories, the mkdir
command allows you to create them.
Creating directories is pretty simple. All you need to do is pass the name of the directory you want to create to the mkdir
command.
For example, to create a new directory named images
in the current directory, the command would be:
mkdir images
Building a structure with multiple subdirectories using mkdir
requires adding the -p
option. The -p
option tells mkdir
also to create parent directories.
mkdir -p images/upload/new
Code language: JavaScript (javascript)
8. cat
cat (Concatenate) is a command used to display the contents of one or more files without opening the file for editing. It can read, concatenate, and write file contents to the standard output.
The most basic and common usage of the cat
command is to read the contents of files.
For example, the following command will display the contents of the psforevermore.txt
file on the terminal:
cat psforevermore.txt
Code language: CSS (css)
You're the sunshine in my eyes,
You're the color of my life,
You're the reason why I'm here to say, "Alright."
Code language: PHP (php)
9. less
The less
command is used to display file contents or command output one page at a time in your terminal. It is most helpful for viewing the content of large files or the results of commands that produce many lines of output.
The syntax for the less
command is straightforward. For example, if you want to read the contents of the psforevermore.txt
file, the command would be:
less psforevermore.txt
Code language: CSS (css)
The output of less is divided into sort pages. Youโll see only the text that fills to your terminal screen. You can use the up and down arrow keys to move line by line. If you want to move page by page, use the space
key to move to the next page and the b
key to go back to the previous page.
Press q
at any given point to exit from less
.
10. head
The head
command is used for outputting the first part of files given to it via standard input. Then, It writes results to standard output. By default, the head
command returns the first ten lines of each file given.
For example, to view the first ten lines of a file, pass the name of a file to the head
command.
head <meta http-equiv="content-type" content="text/html; charset=utf-8">psforevermore.txt
Code language: HTML, XML (xml)
To set the number of lines to show with the head
command, pass the -n
option followed by the number of lines to show.
head -n 2 <meta http-equiv="content-type" content="text/html; charset=utf-8">psforevermore.txt
Code language: HTML, XML (xml)
Related: Head And Tail Commands In Linux Explained With Examples
11. tail
The tail
is complementary to the head
command. It prints the last N number of data of the given input. The tail
command default prints the last ten lines of the specified files.
tail psforevermore.txt
Code language: CSS (css)
You can use the -n
option to specify the number of lines shown.
tail -n 10 psforevermore.txt
Code language: CSS (css)
You can also omit the letter n
and use just the hyphen -
and the number with no space between them.
tail -10 psforevermore.txt
Code language: CSS (css)
12. grep
grep (Global Regular Expression Print) searches for a string of characters in a specified file. It is among the most useful commands in Linux.
For example, to search any line that contains the word color
in the filename psforevermore.txt
, the command would be:
grep color psforevermore.com
Code language: CSS (css)
You're the <strong>color</strong> of my life,
Code language: HTML, XML (xml)
Because the grep
command is case sensitive, one of the most useful operators for grep
searches is -i
. Instead of printing lowercase results only, the terminal displays both uppercase and lowercase results.
grep -i Color psforevermore.txt
Code language: CSS (css)
<meta http-equiv="content-type" content="text/html; charset=utf-8">You're the <strong>color</strong> of my life,
Code language: HTML, XML (xml)
To include all subdirectories in a search, add the -r
(recursively) operator to the grep
command.
The example command below prints the matches for word username
in all files in the /etc
directory and its subdirectories.
grep -r username /etc
13. man
man (Manuals) gives users access to manual pages for command-line utilities and tools. In addition, it allows users to view the reference manuals of a command or utility run in the terminal.
The man page includes a command description, applicable options, flags, examples, and other informative sections.
In the terminal window, type man
followed by the Linux command name which man page you want to see. For example:
man telnet
To exit, press q
.
14. chown
The chown
(Change Owner) command is used to change the file owner or group. Whenever you want to change ownership, you can use the chown
command. Superuser (sudo
) permissions are necessary to execute the chown
command.
Changing the owner of a file with chown
requires you to specify the new owner and the file.
The following command changes the ownership of a file website-logo.jpg
from linuxiac
to the user john
:
chown john website-logo.jpg
Code language: CSS (css)
To assign a new owner of a file and change its group at the same time, run the chown
command in the format given below.
For example, to set john
as the new owner and users
as the new group of the file website-logo.jpg
:
chown john:users website-logo.jpg
Code language: CSS (css)
The chown
command allows changing the ownership of all files and subdirectories within a specified directory. Add the -R
option to the command to do so.
In the following example, we will recursively change the owner and the group for all files and directories in the images
directory.
chown -R john:users images/
15. chmod
chmod (Change Mode) allows you to change the permissions on a file. Only the root, the file owner, or user with sudo
privileges can change the permissions of a file.
The references are used to distinguish the users to whom the permissions apply:
u
(owner): File’s owner.g
(group): Users who are members of the file’s group.o
(others): Users who are neither the file’s owner nor members of the file’s group.a
(all): All three of the above (same asugo
).
The operators are used to specify how the modes of a file should be adjusted:
+
Adds the specified modes to the specified classes.-
Removes the specified modes from the specified classes.=
The modes specified are to be made the exact modes for the specified classes.
The modes indicate which permissions are to be granted or removed from the specified classes:
r
Read the file.w
Write or delete the file.x
Execute the file or in the case of a directory, search it.
For example, in the following command, read permission will be added for all three levels: user, group, and other.
chmod a+r <meta http-equiv="content-type" content="text/html; charset=utf-8">website-logo.jpg
Code language: HTML, XML (xml)
Remove the execute permission for all users:
chmod a-x <meta http-equiv="content-type" content="text/html; charset=utf-8">website-logo.jpg
Code language: HTML, XML (xml)
Remove the read, write, and execute permission for all users except the fileโs owner:
chmod og-rwx <meta http-equiv="content-type" content="text/html; charset=utf-8">website-logo.jpg
Code language: HTML, XML (xml)
If you want the file’s owner to have read and write permissions and the group and other users to have read permissions only:
chmod u=rw,go=r <meta http-equiv="content-type" content="text/html; charset=utf-8">website-logo.jpg
Code language: HTML, XML (xml)
Using the =
operator means we wipe out existing permissions and then set the ones specified.
You can add the execute permission for everyone with the following command:
chmod a+x <meta http-equiv="content-type" content="text/html; charset=utf-8">website-logo.jpg
Code language: HTML, XML (xml)
To recursively operate on all files and directories under a given directory, use the chmod
command with the -R
(recursive) option.
For example, in the following command user can read, write, and execute. However, group members and other users can read and execute but cannot write. This applies to all files and subdirectories under the /tmp/test
directory.
chmod -R u=rwx,go=rx /tmp/test
16. top
The top
command allows users to monitor Linux processes and system resource usage. It is one of the most useful tool tools in a sysadmin’s toolbox, and it is pre-installed on every Linux distribution.
You simply need to type this in the terminal to launch top
:
top
17. find
The find command allows users to search for files and directories based on conditions. It is one of the most important and frequently used commands in Linux.
Find all the files whose name is website-logo.jpg
in a current working directory.
find . -name website-logo.jpg
Code language: CSS (css)
The -name
option is case-sensitive. If you donโt know the exact case of the item youโre looking for, you can use the -iname
option, which is case insensitive.
<meta http-equiv="content-type" content="text/html; charset=utf-8">find . -iname website-logo.jpg
Code language: HTML, XML (xml)
Find all directories whose name is linuxiac
in /
directory.
find / -type d -name linuxiac
Find all .php
files in /var/www/html
directory.
find /var/www/html -type f -name "*.php"
Code language: JavaScript (javascript)
18. df
The df
(Disk Free) command displays the amount of available disk space for file systems.
To view disk space usage, run the df
command. This can be useful to discover the amount of free space available on a system or filesystems.
df
Filesystem 1K-blocks Used Available Use% Mounted on
udev 1984900 0 1984900 0% /dev
tmpfs 403004 1472 401532 1% /run
/dev/vda5 30314436 12798764 16057744 45% /
tmpfs 2015016 0 2015016 0% /dev/shm
tmpfs 5120 4 5116 1% /run/lock
/dev/vda1 523248 4 523244 1% /boot/efi
Code language: PHP (php)
To view disk space in human readable format pass the -h
option. This prints sizes in G
for Gigabytes, M
for Megabytes and B
for Bytes.
df -h
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 394M 1.5M 393M 1% /run
/dev/vda5 29G 13G 16G 45% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
/dev/vda1 511M 4.0K 511M 1% /boot/efi
Code language: PHP (php)
19. du
The du
(Disk Usage) command estimates the amount of disk space used by a given file or directory.
To find out the disk usage summary of a /home/linuxiac
directory tree and each of its sub directories, enter the command as shown below. The output will display the number of disk blocks in the /home/linuxiac
directory and its sub-directories.
du /home/linuxiac
4 /home/linuxiac/Documents
888 /home/linuxiac/.cache/fontconfig
...
8 /home/linuxiac/.ssh
2143400 /home/linuxiac/
If we want to print sizes in human readable format (K, M, G), use -h
option.
du -h /home/linuxiac
4.0K /home/linuxiac/Documents
888K /home/linuxiac/.cache/fontconfig
...
8.0K /home/linuxiac/.ssh
2.1G /home/linuxiac/
To get the summary of a total disk usage size of a directory, use the options -s
as follows.
du -sh /home/linuxiac
2.1G /home/linuxiac/
20. ps
The ps
(Process Status) command displays the currently running processes in the system. In addition, it will display the list of processes running on the system, including details such as process id, name of terminal currently logged in, CPU time, etc.
System administrators generally use ps with a
, u
, x
, and w
options to get all details in a single ps command execution.
a
: All other user processes.u
: Owner of the process.x
: Other processes those not attached to the terminal.w
: Wide output.
ps auxw
Conclusion
Basic Linux commands help users execute tasks quickly and effectively. Although you can perform most of the system-related tasks using a graphical interface, the command line makes you more productive and can get more done in less time.
If you have any questions or feedback, feel free to leave a comment.