In this Linux tutorial post, we are writing on all Linux basic commands with examples which every Linux beginner should know. It will help you to kickstart on any Linux Operating System.
We expect, you have already installed the Linux Operating System in your system and we are ready to start the command in terminal.
We are trying to keep this post simple and easy to use for all Linux Beginners. So all the examples are taken as reference from the daily and commonly use command. We will be showing only some arguments in commands which are commonly use and very helpful. Knowing about the command’s argument is good but all arguments are mostly not in regular use. With the experience you will naturally learn some more arguments with commands.
Table of Contents
- Know Autocompletion Of Command On Terminal
- How to take help on command in terminal itself
- pwd
- clear
- whoami
- id
- ls
- cd
- mkdir
- cat
- rm
- cp
- mv
- less
- more
- touch
- date
- history
- top
- ps
- kill
- pidof
- echo
- which
- i/o redirection
- Some More Learning On Linux Commands
- FAQ
Know Autocompletion Of Command On Terminal
Press tab key for autocompletion of the command on terminal. Linux has this awesome feature which will reduce your time on typing the command . Let’s say an example, write half command ‘mkd’ then press Tab key in your keyboard. It should autocomplete the command. Have you also tried ? What did you get ?
How to take help on command in terminal itself
Linux beginners mostly get confused with commands when they see some experts using arguments with command. To solve this problem, we generally use two commands for taking help in terminal itself.
- By using
man
command : To keep the things simple, use the man command followed with command name. For example.man ls
. (Know more about man pages in our two helpful articles, “Search keyword in all available man pages – man command“, “search keyword in man page like we search in vi / vim editor“) - By using
--help
with each command: Use –help with commands , it will show you the help section in terminal as an output. For eg.ls --help
In this article, we have included both Internal and External Commands.
pwd
pwd
command is for printing the name of the current working directory. Means, it shows the directory path where the current login user is working.
In given below example, it shows that the user ‘sharadchhetri’ working directory is /var/log .
We have written about pwd command in detail, the topic called “print working directory ( pwd , PWD , OLDPWD ) in Linux / Unix“
sharadchhetri@server01:/var/log$ pwd /var/log sharadchhetri@server01:/var/log$
clear
The clear
command is for clearing the output in terminal.
We have already written about clear command in our article called “Linux : clear command and its keyboard shortcut“
# Example: Try like this and hit enter after clear command sharadchhetri@server01:~$ ls mydir myfile newdir test.txt sharadchhetri@server01:~$ clear
whoami
The whoami
command will show user id which login to terminal. To be very precise, it print the user name associated with the current effective user ID.
In given below example, you can see that user called ‘sharadchhetri’ is the user id which is currently working on the terminal.
If you login and use the same command, it will show your user id on terminal. Isn’t great way to check , which user you are login to machine !?
sharadchhetri@server01:/var/log$ whoami sharadchhetri sharadchhetri@server01:/var/log$
You can learn more about whoami command in details – here is our post called Linux / Unix : whoami command use and advantage.
id
The id
command shows the user id information. It print user and group information for each specified USER.
Lets take a look in the example.
Example 1: The output has information of user called ‘user1’. We can see its uid(user id), gid(group id)and groups name(also showing each group id) where the user1 is associated with.
Example 2: Here, we have only use the id command with current user login. It shows the current user information about its uid(user id), gid(group id) and group name with gid.
# Example 1 sharadchhetri@server01:~$ id user1 uid=1000(user1) gid=1000(user1) groups=1000(user1),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),110(lxd) sharadchhetri@server01:~$ # Example 2 sharadchhetri@server01:~$ id uid=1001(sharadchhetri) gid=1001(sharadchhetri) groups=1001(sharadchhetri) sharadchhetri@server01:~$
ls
ls
command output shows the list of files and directories. If you define the location then it will show the list of files/directories of that location.
We have given some examples of ls
command here. You can also combine the arguments with the command.
-l
= use a long listing format. Shows some good details.
-a = shows hidden files and folders.
-d
= list only directories not their contents inside.
-i
= shows inode numbers (or index number) of files .
# Example 1: List the files with details sharadchhetri@server01:/home$ ls -l total 8 drwxr-xr-x 2 sharadchhetri sharadchhetri 4096 Mar 17 13:57 sharadchhetri drwxr-x--- 5 user1 user1 4096 Mar 13 11:29 user1 sharadchhetri@server01:/home$ # Example 2: list inside the directory sharadchhetri@server01:/home$ ls -l /var/log/ total 3156 -rw-r--r-- 1 root root 21535 Mar 13 10:51 alternatives.log -rw-r----- 1 root adm 2306 Feb 15 03:46 apport.log drwxr-xr-x 2 root root 4096 Mar 13 10:50 apt -rw-r--r-- 1 root root 64549 Aug 9 2022 bootstrap.log -rw-rw---- 1 root utmp 768 Mar 5 16:43 btmp -rw-r----- 1 root adm 101517 Mar 17 13:49 cloud-init-output.log -rw-r--r-- 1 root adm 2371570 Mar 17 13:49 cloud-init.log drwxr-xr-x 2 root root 4096 Aug 3 2022 dist-upgrade -rw-r--r-- 1 root root 508766 Mar 13 10:51 dpkg.log -rw-r--r-- 1 root root 32064 Mar 17 13:56 faillog -rw-r--r-- 1 root root 485 Mar 6 17:15 fontconfig.log drwxr-x--- 3 root adm 4096 Feb 11 14:48 installer drwxr-xr-x 2 jenkins jenkins 4096 Feb 8 13:25 jenkins drwxr-sr-x+ 3 root systemd-journal 4096 Feb 11 14:49 journal -rw-rw-r-- 1 root utmp 292584 Mar 17 13:56 lastlog drwxr-s--- 2 mysql adm 4096 Mar 7 11:01 mysql drwxr-xr-x 2 root root 4096 Mar 7 17:11 nginx -rw------- 1 root root 5294 Mar 17 13:48 php8.1-fpm.log drwx------ 2 root root 4096 Aug 9 2022 private drwxr-x--- 2 root adm 4096 Feb 11 14:49 unattended-upgrades -rw-rw-r-- 1 root utmp 65280 Mar 17 13:51 wtmp sharadchhetri@server01:/home$ # Example 3: list hidden files with -a argument. sharadchhetri@server01:~$ ls -l total 0 sharadchhetri@server01:~$ sharadchhetri@server01:~$ ls -la total 12 drwxr-xr-x 3 sharadchhetri sharadchhetri 4096 Mar 17 16:11 . drwxr-xr-x 4 root root 4096 Mar 17 13:57 .. drwxrwxr-x 2 sharadchhetri sharadchhetri 4096 Mar 17 16:11 .myfolder -rw-rw-r-- 1 sharadchhetri sharadchhetri 0 Mar 17 16:11 .test sharadchhetri@server01:~$ # Example 4: List only directory, not its content inside. sharadchhetri@server01:~$ ls -ld /var drwxr-xr-x 14 root root 4096 Mar 7 11:15 /var sharadchhetri@server01:~$ # Example 5: List index numbers of files. sharadchhetri@server01:/tmp$ ls -i 136737 hsperfdata_jenkins 136743 systemd-private-df8285a24ce94843975ca1d17763939c-systemd-logind.service-2gQ93X 4685 winstone1040274427452123975.jar 136745 jetty-0_0_0_0-8080-war-_-any-7600975038121345778 136732 systemd-private-df8285a24ce94843975ca1d17763939c-systemd-resolved.service-3ygtJ1 136725 snap-private-tmp 136734 systemd-private-df8285a24ce94843975ca1d17763939c-systemd-timesyncd.service-uFF7J0 sharadchhetri@server01:/tmp$
cd
The cd
command means change the current directory directory. As a user , we need to switch or change to directories for work. For example, I want to go to /home directory and then late wants to go to /etc directory. Here the cd
command will help.
cd ~
: Changing to current USER Default Home Directory
cd -
: Changing back to previous directory.
cd ..
: Changing back to One level back
We have written a article on cd command with example, the post is called “6 important examples of cd command on Linux and Unix Systems” . This post is comprehensive guide on cd
command.
# Example 1: Changing directory by using the path. sharadchhetri@server01:/tmp$ pwd /tmp sharadchhetri@server01:/tmp$ cd /home sharadchhetri@server01:/home$ sharadchhetri@server01:/home$ pwd /home sharadchhetri@server01:/home$ cd /etc sharadchhetri@server01:/etc$ pwd /etc #Example 2: Changing Directory to USER default Home directory sharadchhetri@server01:/etc$ cd ~ sharadchhetri@server01:~$ pwd /home/sharadchhetri sharadchhetri@server01:~$ # Example 3: Changing back to previous directory. Using hyphen with cd sharadchhetri@server01:~$ cd - /etc sharadchhetri@server01:/etc$ sharadchhetri@server01:/etc$ pwd /etc sharadchhetri@server01:/etc$ # Example 4: Changing back to one level directory. sharadchhetri@server01:/etc$ cd .. sharadchhetri@server01:/$ pwd / sharadchhetri@server01:/$ # Example 5: Similar to Example 4 ,only Changing directory to two level. sharadchhetri@server01:/var/www/html$ pwd /var/www/html sharadchhetri@server01:/var/www/html$ cd ../.. sharadchhetri@server01:/var$ pwd /var sharadchhetri@server01:/var$
mkdir
The mkdir
command is for creating directory.
-p
: when using this argument, it create parent directory when needed. See the Example 3.
# Example 1: Create Directory in current path. sharadchhetri@server01:~$ mkdir test sharadchhetri@server01:~$ ls -ld test drwxrwxr-x 2 sharadchhetri sharadchhetri 4096 Mar 17 16:37 test sharadchhetri@server01:~$ #Example 2: Create Directory in custom path sharadchhetri@server01:~$ mkdir /tmp/mytest drwxrwxr-x 2 sharadchhetri sharadchhetri 4096 Mar 17 16:38 /tmp/mytest/ sharadchhetri@server01:~$ # Example 3: Create parent directories as needed. sharadchhetri@server01:~$ ls -l test total 0 sharadchhetri@server01:~$ sharadchhetri@server01:~$ mkdir -p test/new/another/mydir sharadchhetri@server01:~$ ls -ld test/new/another/mydir/ drwxrwxr-x 2 sharadchhetri sharadchhetri 4096 Mar 17 16:41 test/new/another/mydir/ sharadchhetri@server01:~$
cat
The cat
command is for concatenating the file contents. Mostly this is use for reading the small size file also. Explore its arguments by yourself, some interesting things you will find.
# Example 1 sharadchhetri@server01:~$ cat test.txt This is a test file For showing cat command sharadchhetri@server01:~$ # Example 2: Concatenating the two files sharadchhetri@server01:~$ cat test.txt This is a test file For showing cat command sharadchhetri@server01:~$ cat color This is color file red blue green sharadchhetri@server01:~$ cat color test.txt This is color file red blue green This is a test file For showing cat command sharadchhetri@server01:~$
rm
The rm command is for removing files and directories.
-d = Remove empty directory
-f = Remove forcefully
-r = Remove recursively.
-i : Prompt before removing
# Example 1: Removed empty directory. sharadchhetri@server01:~$ ls -l newtest/ total 0 sharadchhetri@server01:~$ rm -d newtest/ sharadchhetri@server01:~$ ls -ld newtest ls: cannot access 'newtest': No such file or directory sharadchhetri@server01:~$ # Example 2: Remove recursive dir sharadchhetri@server01:~$ rm -r test/new/another/mydir/ sharadchhetri@server01:~$ ls -l test test/ test.txt sharadchhetri@server01:~$ ls -l test/new/another/ total 0 sharadchhetri@server01:~$ # Example 3: Forcefully and recursively remove the dir sharadchhetri@server01:~$ rm -fr test/new/another/ sharadchhetri@server01:~$ ls -l test/new/ total 0 sharadchhetri@server01:~$ # Example 4: Remove file. sharadchhetri@server01:~$ ls -l total 12 -rw-rw-r-- 1 sharadchhetri sharadchhetri 34 Mar 17 16:47 color drwxrwxr-x 3 sharadchhetri sharadchhetri 4096 Mar 17 16:41 test -rw-rw-r-- 1 sharadchhetri sharadchhetri 44 Mar 17 16:46 test.txt sharadchhetri@server01:~$ rm color sharadchhetri@server01:~$ ls -l total 8 drwxrwxr-x 3 sharadchhetri sharadchhetri 4096 Mar 17 16:41 test -rw-rw-r-- 1 sharadchhetri sharadchhetri 44 Mar 17 16:46 test.txt sharadchhetri@server01:~$
cp
The cp
command is for copying files and folders.
-r
= Recursive for directories.
-f
= Forcefully
# Example 1: Copying file sharadchhetri@server01:~$ ls -l total 8 drwxrwxr-x 3 sharadchhetri sharadchhetri 4096 Mar 17 16:41 mydir -rw-rw-r-- 1 sharadchhetri sharadchhetri 44 Mar 17 16:46 test.txt sharadchhetri@server01:~$ cp test.txt myfile sharadchhetri@server01:~$ ls -l total 12 drwxrwxr-x 3 sharadchhetri sharadchhetri 4096 Mar 17 16:41 mydir -rw-rw-r-- 1 sharadchhetri sharadchhetri 44 Mar 17 17:01 myfile -rw-rw-r-- 1 sharadchhetri sharadchhetri 44 Mar 17 16:46 test.txt sharadchhetri@server01:~$ # Example 2: Copying directory by using -r argument. See intentional error. sharadchhetri@server01:~$ ls -ld mydir/ drwxrwxr-x 3 sharadchhetri sharadchhetri 4096 Mar 17 16:41 mydir/ sharadchhetri@server01:~$ cp mydir newdir cp: -r not specified; omitting directory 'mydir' sharadchhetri@server01:~$ sharadchhetri@server01:~$ cp -r mydir/ newdir sharadchhetri@server01:~$ ls -l total 16 drwxrwxr-x 3 sharadchhetri sharadchhetri 4096 Mar 17 16:41 mydir -rw-rw-r-- 1 sharadchhetri sharadchhetri 44 Mar 17 17:01 myfile drwxrwxr-x 3 sharadchhetri sharadchhetri 4096 Mar 17 17:03 newdir -rw-rw-r-- 1 sharadchhetri sharadchhetri 44 Mar 17 16:46 test.txt sharadchhetri@server01:~$
mv
The mv command can be used for two purposes.
- To move the file or directory
- To rename the file or directory
We have written about mv command in this Article called “How To Rename And Move The Directory And Files In Linux“
Syntax
mv [arguments] SOURCE DESTINATION
Example 1: Here in this example, we are moving Directory called newtest to Desktop
mv newtest/ /home/linux/Desktop/
Example 2: In this example, we are renaming newtest Directory and also moving it to /home/linux directory
mv newtest/ /home/linux/sharadtest
less
The less command is for viewing file contents. It is recommended to use less command if you want to view big size file. The readable width get adjusted automatically.
# Example: Give filename and use cursor for navigation. Press 'q' to exit. less filename
more
The more command is also similar to less command. Use for viewing the file. The difference can be seen in File Output when sometime you will notice the content is wide and out of screen.
want to view big size file. The readable width get adjusted automatically. # Example: Give filename and use cursor for navigation. Press 'q' to exit. more filename
touch
The touch command update the access and modification times of each FILE to the current time . You can modify the access and modification time on file and directory both. But by using this command we can also create empty file.
# Example: Creating empty file sharadchhetri@server01:~$ touch /tmp/myfile sharadchhetri@server01:~$ ls -l /tmp/myfile -rw-rw-r-- 1 sharadchhetri sharadchhetri 0 Mar 17 17:21 /tmp/myfile sharadchhetri@server01:~$ # Example 2: See the access and modification time of file. sharadchhetri@server01:~$ touch -d '8 July 2022 11:02' testfile sharadchhetri@server01:~$ stat testfile File: testfile Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 427460 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1001/sharadchhetri) Gid: ( 1001/sharadchhetri) Access: 2022-07-08 11:02:00.000000000 +0000 Modify: 2022-07-08 11:02:00.000000000 +0000 Change: 2023-03-17 17:23:52.337607682 +0000 Birth: 2023-03-17 17:23:52.337607682 +0000 sharadchhetri@server01:~$
date
The date command shows date as well as we can set date in the system. It is one of the useful command which every system admin needs. One of the important use case is , this command is heavily use in Backup Scripts or while taking backup for marking as a backup date of source.
user1@server01:~$ date Sat Mar 18 10:55:16 UTC 2023 user1@server01:~$
You can learn more on Date command from our this article – “Learn Linux Date Command With Examples“
history
The name of command itself speaks its use. This is for showing history of the commands which user has run on the terminal. To use this command simply run –
history
top
The top command is for displaying the process and other various useful information like Date, System Uptime, Load Average, CPU, Memory etc.
Type the command and hit enter.
top
ps
The ps
displays information about a selection of the active processes but not in a repetitive manner just like top command do.
Example 1: Without argument when we run ps
command , it shows the process of current login shell.
user1@server01:~$ ps PID TTY TIME CMD 2258 pts/0 00:00:00 bash 3051 pts/0 00:00:00 ps user1@server01:~$
Example 2: To display process full format listing
ps -ef
Example 3: Show all process owned by you
ps -x
kill
The kill command is for sending SIGNAL to the process. The default signal is SIGTERM means signal for terminating the process.
The kill command syntax is given below
kill [SIGNAL] [Process id]
Example: Terminate the process by using its pid (process id). We are showing the example which is often use that is kill -9 [process id]
Here in this example, we have terminated the top command process which was running in background.
user1@server01:~$ ps -x PID TTY STAT TIME COMMAND 2231 ? Ss 0:00 /lib/systemd/systemd --user 2233 ? S 0:00 (sd-pam) 2257 ? S 0:00 sshd: user1@pts/0 2258 pts/0 Ss 0:00 -bash 3358 pts/0 T 0:00 top 3359 pts/0 R+ 0:00 ps -x user1@server01:~$ kill -9 3358 user1@server01:~$ [1]+ Killed top user1@server01:~$ ps -x PID TTY STAT TIME COMMAND 2231 ? Ss 0:00 /lib/systemd/systemd --user 2233 ? S 0:00 (sd-pam) 2257 ? S 0:00 sshd: user1@pts/0 2258 pts/0 Ss 0:00 -bash 3368 pts/0 R+ 0:00 ps -x user1@server01:~$
pidof
The pidof command will help to display the pid or process id of the process/application.
The Syntax is as follows.
pidoff [Process Name /Application Name]
In this example, we will see the process id of the sshd.
user1@server01:~$ pidof sshd 2257 2224 661 user1@server01:~$
echo
The echo command shows the output as string when arguments are used along with it. Here is the example, it will display its own argument.
The echo command is widely used in the bash scripting also. It has various use cases.
user1@server01:~$ echo "Welcome in sharadchhetri.com" Welcome in sharadchhetri.com user1@server01:~$
which
The which command display the absolute path of the command . The more details we have written in this post – “How to find absolute path of command on Linux / Unix : which command”
In given below example, we have used the which command to find the absolute path of ls command.
user1@server01:~$ which ls /usr/bin/ls user1@server01:~$
i/o redirection
The abbreviation of i/o is Input Output. In Linux/Unix Operating System we can do the i/o redirection. To understand in simple manner the command output can be saved in some file. As this post was meant for beginner so we kept the definition simple. (If you want to know more, sharing just some clues, it has 3 streams – STDIN, STDOUT, STDERR)
Here is the one of the example.
echo Write Something In File > file.txt
There are so many commands in Linux which gives a powerful control and excitement to use. We tried to give some overview of some commands in this post. We hope this will give you a quick kickstart on your Linux terminal. Happy Linuxing .
Some More Learning On Linux Commands
In this article we tried to provide some overview and brief explanation on Linux Commands so that it eases your first step of Linux Learning.
We generally keep all the articles here on Linux Command section . You can visit to this section and read and learn from our 70+ Articles written only on Linux Command.
FAQ
In this section, we have taken some Frequently Asked Questions.
What is Linux basic command ?
Linux basic commands are the commands run on terminal, which are mostly use for basic requirements. For example, commands like cat which is use for reading file, cd command which is use for changing directory, mv command for renaming files and directory etc. There are so many Linux Basic commands which every Linux beginner should learn.
Is using command line faster then GUI ?
Yes, commands which we run are faster. Even many GUI apps in the backend call the commands only.
Why should I learn the Linux commands ?
Linux commands will make your work easy and faster. Linux means command line. Either you are a common Linux user or professional, Linux commands is crucial for all. The command line gives you more power to control and manage the Operating System. If you run Linux Servers in GUI , it consumes more CPU/Memory resources compare to Linux running without GUI.