20 Basic Linux Commands - Learn in 20 Minutes
20 Basic Linux Commands - Learn in 20 Minutes
1. man
Syntax
-f
-d
-D
It is quite helpful if you want a quick lookup at the usage of any Linux command directly
inside the terminal instead of navigating multiple web pages on a browser to find proper
documentation of the required command.
man ls: This command is used to display the documentation on the 'ls' command in
Linux.
$ man ls
Try it Yourself
Using the man command, try exploring the documentation of the 'man' command
itself inside the bash terminal. How would you enable man command to interpret the
[Command Name] argument as a regular expression?
2. cd
Syntax
As the name suggests, the [Options] tag is optional when it comes to using any Linux
command.
$ man ls
cd ..: This command is used to navigate to the parent directory of the current directory
(here, Desktop is the parent directory of the Linux folder)
$ cd ..
Try it Yourself
Using the cd command, find out how you'll jump to the root directory of your system
without resorting to the 'cd ..' command multiple times.
3. ls
Syntax
$ls [Options].. [Files]..
This command is super useful if you want to explore the contents of a given directory
inside the terminal without navigating to the GUI folder.
ls: This command is used to navigate to the Linux folder inside our Desktop directory.
$ ls
ls -l: The -l tag is used to display a detailed version of every file and directory available
inside the current working directory.
$ ls -l
Try it Yourself
How would you list out the hidden files inside a directory using the ls command?
Add more advanced commands to your skills - Download Now
4. cat
The cat command in Linux is used to read the contents of one or more files and display
their contents inside the terminal.
Syntax
It comes in handy if you want a quick look up into the contents of any file without
opening up a separate application for the file.
Let us understand the cat command with a few examples. Let us assume we have two
text files, namely test1.txt and test2.txt
$ cat test1.txt
cat -n <filename>: Used to display the contents of the file with line numbers.
5. touch
The touch command in Linux is used to create a new file without any content inside it.
Syntax
It's very useful if you quickly want to create a new file inside your working directory
directly from the terminal.
Here are a few examples to help you understand this command better.
$ touch test1.txt
touch <filename1> <filename2>: Used to create multiple files simultaneously.
Try it Yourself
How would you add a check whether a file already exists or not before creating it
using the touch command? [HINT: You need an option tag]
6. mkdir
The mkdir command in Linux is used to create new directories inside an existing
working directory from the terminal.
Syntax
The best part about this command is that it can be simultaneously used to set user
permissions pertaining to any directory you're creating directly from the terminal.
Here are a few examples to help you understand this command better.
$ mkdir --help
$ mkdir <filename>
Try it Yourself
Can you create multiple files simultaneously using the mkdir command?
How would you create a child directory inside a parent directory (both of which do not
exist at the time of creation) using the mkdir command?
Learn better by discussing with the community. Head to the comments now.
7. pwd
The pwd command in Linux translates to "Print Working Directory" and is used to
display the path of the current working directory inside the terminal.
Syntax
$pwd [Option]
pwd: Displays the full path of the current working directory you're in.
$ pwd
Try it Yourself
Find out what "$PWD" does when executed inside the terminal. Does it print
anything? If so, what is it?
8. echo
The echo command in Linux simply displays a line of text/string which is passed in as
an argument. It is commonly used for debugging shell programs inside the terminal.
Syntax
Here are a few simple examples to help you understand this command better.
$ echo "String"
Try it Yourself
How would you print all the file and folder names inside your current working
directory using the echo command?
PREDICT the output of the following bash command:
9. rm
The rm command in Linux helps you delete files and directories. To be more specific,
rm deletes all references to objects from the filesystem, where those objects may have
several references (for example, a file with two different names).
Syntax
Here are a few simple examples to help you understand this command better.
rm <Filename>: Used to delete the corresponding existing file from inside the working
directory.
$ rm <Filename>
rm -i <Filename>: The '-i' tag enables the rm command to request confirmation from
the user before deleting the corresponding file.
$ rm -i "<Filename>"
Try it Yourself
Syntax
Here are a few simple examples to help you understand this command better.
$ rmdir --help
$ rmdir <Directory_Name>
Let's first try to delete a non-empty directory.
Now let's try to delete an empty directory inside our Linux/ path defined earlier.
Try it Yourself
Do you know which Linux commands complete which tasks? Take this quiz to find out
->
11. wget
Syntax
$wget [Option] [URL]
The best part about using the wget command is that it's pretty stable over an
unstable/weak network, i.e., wget will keep retrying until the entire file has been
retrieved from the Internet.
Here are a few simple examples to help you understand this command better.
wget <URL>: Used to retrieve relevant content associated with the specified URL from
the Internet.
$ wget <URL>
Try it Yourself
How would you download a given file in the background using the wget command?
Is it possible to resume a partially downloaded file using wget? If so, how would you
do it?
12. mv
Find out what other commands you can use to perform file/directory
operations
Syntax
But you should be wary when using this command because it doesn't prompt a default
confirmation before moving files/directories.
Here are a few simple examples to help you understand this command better.
$ mv <Initial_Filename> <New_Filename>
mv <Filename> <Directory_Name>: Used to transfer a file from a given directory to a
different directory.
$ mv <Filename> <Directory_Name>
You can see that our index.html file has been moved inside the Projects/ directory.
Try it Yourself
How would you enable a confirmation prompt before transferring a file from one
directory to another?
Can you move multiple files/folders at the same time using the mv command?
13. cp
Syntax
$cp [Options] [Source].. [Destination]
Let us assume we have two separate files 'test1.txt' and 'test2.txt' inside our current
working directory. Their contents are as follows:
cp <Source_File> <Dest_File>: Used to copy the contents of the source file into the
destination file.
$ cp <Source_File> <Dest_File>
$ mv <Filename> <Directory_Name>
You can clearly see that the contents of the test2.txt file have been overwritten with the
contents of test1.txt.
Try it Yourself
How would you copy the contents of one directory into another directory from the
bash terminal?
Can you copy multiple files into a new directory from the bash terminal?
14. tree
The tree command in Linux can be used to list out the contents of directories in a tree-
like fashion.
$tree [Options]
$ tree
You can clearly see that the directories and files present inside the Linux/ directory as
defined earlier.
tree -f: sed to display the full path of each working directory and file inside your current
directory.
$ tree -f
Try it Yourself
How would you display only the directories inside your current working directory in
the Linux terminal?
How would you display the last modification time corresponding to every directory
and file inside your current working directory solely using the tree command?
The grep command in Linux searches through a specified file and prints all lines that
match a given pattern.
Syntax
Let us assume that we have a sample text file with the following contents:
grep -i <pattern> <filename>: Used to search for the given pattern inside a given file.
The -i tag is used to disable case sensitivity while searching for patterns.
You can clearly see that the matched patterns are clearly highlighted.
grep -c <pattern> <filename>: Used to display the count of the number of occurrences
of a given pattern inside a specified file.
Try it Yourself
How would you disable case sensitivity while counting the number of occurrences of
a pattern inside a given file?
How would you display the corresponding line number for the matched pattern using
the grep command?
16. vi
The vi command in Linux allows a user to edit any text content inside the Vim text editor
from the terminal.
Syntax
This is how the Vim editor generally looks like inside the bash terminal
Here's a list of commonly used keyboard shortcuts used inside the Vim editor in Linux:
i: Used to enter insert mode in Vim editor. Use the Esc key to exit out of insert mode.
dd: Delete a line quickly.
yy: Copy a line/lines inside the editor.
p/P: Paste command
u: Undo command
Ctrl+r: Redo command
:wq: Save and quit Vim editor.
:q: Quit Vim editor without saving a file.
$ vi <filename>
Try it Yourself
Create a new file using the vi command and type in a sample text inside the editor.
Save and exit from the Vim editor. Could you do it?
17. head
The head command in Linux prints the first N lines of a given file content. Yes, it's that
simple.
Syntax
Here are a few examples to help you understand this concept better. Let us assume we
have a sample text file with the following contents
Now let us execute some sample commands.
18. tail
The tail command in Linux prints the last N lines of a given file content. Yes, it's that
simple.
Syntax
Here are a few examples to help you understand this concept better. Let us assume we
have a sample text file with the following contents
Try it Yourself
19. wc
Syntax
Here are a few examples to help you understand this concept better. Let us assume we
have a sample text file with the following contents
Now let us execute some sample commands.
$ wc <Filename>
Try it Yourself
How would you display only the number of characters present in a given file?
How would you print the number of lines present in a given file?
20. history
Syntax
$ history
$ history
Try it Yourself
How would you display the last 5 Linux commands which you've previously executed
inside the terminal?
Linux Basics-I
Linux Basics-II
And if you want to master Linux and its commonly used workflows by developers in the
industry, attend Crio’s 7-day free trial.
Experience Crio for free and learn practical web development skills through hands-on activities. Walk
away with assured scholarships after the trial. Book your free trial now.
Crio.Do
In the free trial, you will also get to add developer essential skills such as REST, HTTP,
AWS Deployment.
Any awesome Linux command you wish to share with us? Drop it in the comments
below.
Also, if you enjoyed reading this article, don't forget to share it with your friends!
Get the complete list of Linux commands to perform operations related to:
Hardware
File compression
Package installation
SSH Login
Search
File transfer
Process
Disk usage
File commands
Directory navigation
System info
Users
Network
File permission