0% found this document useful (0 votes)
33 views

20 Basic Linux Commands - Learn in 20 Minutes

Uploaded by

Haribabu Sunkara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

20 Basic Linux Commands - Learn in 20 Minutes

Uploaded by

Haribabu Sunkara
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

20 Basic Linux commands

Download all Linux Commands

1. man

In Linux, the man command is used to display the documentation/user manual on just


about any Linux command that can be executed on the terminal. It includes the name of
the bash command, its detailed synopsis, a short description, existing versions of the
command as well as authors of the bash command.

Syntax

$man [OPTION].. [COMMAND NAME]..

Some popular option tags in use are:

-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?

Comment and let us know.

2. cd

The cd command in Linux expands to 'change directory' which gives a fair hint as to


what the command does. Yes, it is used to change the current working directory to a
specified folder inside the terminal.

Syntax

$cd [Options] [Directory]

As the name suggests, the [Options] tag is optional when it comes to using any Linux
command.

The two option tags pertaining to this command are:

-P: Do not follow any symbolic links


-L: Only follow symbolic links
This command comes in handy if you want to quickly navigate to a different
folder/directory inside the terminal without navigating the GUI.
We'll use the Desktop/Linux folder structure to understand more about
the 'cd' command.

cd Desktop/Linux: This command is used to navigate to the Linux folder inside our


Desktop directory.

$ 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.

Download list of commands to know the answer

3. ls

The ls command in Linux is used to display a directory's files and folders.

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.

We'll use the Desktop/Linux/Learn_by_doing folder structure to understand more


about the 'ls' command.

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

$cat [Options].. [Filename(s)]..

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 <filename>: Used to display the contents of the file as named.

$ cat test1.txt

cat -n <filename>: Used to display the contents of the file with line numbers.

$ cat -n test1.txt test2.txt


Try it Yourself
How would you concatenate the contents of test1.txt and test2.txt using the cat
command?

Don't forget to share your approach with us in the comments below.

5. touch

The touch command in Linux is used to create a new file without any content inside it.

Syntax

$touch [Option].. [Filename]..

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 <filename>: Used to create a new file with the given name.

$ touch test1.txt
touch <filename1> <filename2>: Used to create multiple files simultaneously.

$ cat -n test1.txt test2.txt$ touch style.css script.js

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]

Add your answer in the comments section.

6. mkdir
The mkdir command in Linux is used to create new directories inside an existing
working directory from the terminal.

Syntax

$mkdir [Option].. <Directory Name>..

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: Used to display relevant information pertaining to the mkdir command.

$ mkdir --help

mkdir <filename>: Used to create a folder with a specified name.

$ 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]

The pwd command has two tags:

-L: It displays the symbolic version of the existing path


-P: It displays the actual existing path

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?

Let us know your answer in the comments below.

Download for later: Linux Cheat Sheet with 100+ commands

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

$echo [Option] [String]

Here are a few simple examples to help you understand this command better.

echo "String": Used to display the string passed in as an argument inside quotes.

$ echo "String"

echo -e "Learn \nBy \nDoing": The '-e' tag enables the echo command to recognize


the backslash escape sequences inside the argument.

$ echo -e "Learn \nBy \nDoing"

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:

$ echo -e "Projects \tfor \tDevelopers"

What do you think would be the output if the -e tag wasn't used?

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

$rm [Option] [File]

Here are a few simple examples to help you understand this command better.

Let us consider our initial directory state as follows:

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

Is it possible to delete multiple files simultaneously using the 'rm' command in Linux?


Go ahead, give it a try.
How would you delete a directory using the 'rm' command in Linux? [HINT: You may
have to explore documentation for this command further to find this answer].

Get the complete list of Linux commands you ought to know

There is no way to undo a delete operation performed


using the rm command in Linux. Hence, it is advised to
always execute the command by enabling ‘confirm
before delete,’ so that you do not erase any important
files by mistake. Do you know which flag is used to
enable this mode? Take this quiz to find out →
10. rmdir
The rmdir command in Linux only allows you to delete empty directories. So if a
directory has some files/folders inside it, rmdir will display an error.

Syntax

$rmdir [Option] [Directory_Name]

Here are a few simple examples to help you understand this command better.

rmdir --help: Used to display relevant information corresponding to the rmdir command


inside the terminal.

$ rmdir --help

rmdir <Directory_Name>: Used to delete any directory provided it is empty.

$ rmdir <Directory_Name>
Let's first try to delete a non-empty directory.

You can clearly see that an error shows up.

Now let's try to delete an empty directory inside our Linux/ path defined earlier.

So this time, there's no error as our directory was empty.

Try it Yourself

Is it possible to delete multiple empty folders simultaneously using


the 'rmdir' command in Linux? Go ahead, give it a shot.

So, is it possible? Share your answer with us below.

Do you know which Linux commands complete which tasks? Take this quiz to find out
->

11. wget

The wget command in Linux is a utility tool to download applications/web pages directly


from the web. It allows file downloads with HTTP, HTTPS, and FTP protocols.

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>

wget -O <Custom_File_Name> <URL>: Used to retry a file download for a given


number of times.

$ wget -O <Custom_File_Name> <URL>


You can see that our webpage has been saved to a different name as specified.

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?

Don't forget to share your approach with us in the comments below.

12. mv

The mv command in Linux translates to 'move'. It performs two major functions in


Linux:

You can rename a file/directory using this command.


You can easily move a file/directory from one location to another.

Find out what other commands you can use to perform file/directory
operations
Syntax

$mv [Source] [Destination]

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>: Used to rename a file to a different name as


specified.

$ 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

The cp command in Linux translates to 'copy'. It is used to copy files/directories from


one location to another from inside the terminal.

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:

Now let us look at an intuitive example to understand the cp command.

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>: Used to transfer a file from a given directory to a


different directory.

$ 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.

Do you know the command to move up 1 level in the directory tree


structure? Download the commands list to find out.
Syntax

$tree [Options]

Now let us understand the tree command using a few simple examples.

tree: Used to display the contents of a directory as an indented tree.

$ 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?

Don't forget to share your approach with us in the comments below.


15. grep

The grep command in Linux searches through a specified file and prints all lines that
match a given pattern.

Syntax

$grep [Options] [Pattern] [Filename]

Let us assume that we have a sample text file with the following contents:

Now let us understand grep command with a few examples:

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.

$ grep -i <pattern> <filename>

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.

$ grep -c <pattern> <filename>

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?

Advanced Linux Commands To Broaden Your Skills [Free Access]

16. vi

The vi command in Linux allows a user to edit any text content inside the Vim text editor
from the terminal.

Syntax

$vi [Options] [Filename]

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.

Find out keyboard shortcuts to use Linux commands - Download Now


Here's a small example to get you going

vi <filename>: Used to create a new file and enter it into Vim editor.

$ 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

$head [Option] [Filename]

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.

head -n <number> <Filename>: Used to print the first <number> lines of the specified


file.

$ head -n <number> <Filename>

head -c <number> <Filename>: Used to print the first <number> bytes of the


specified file.

$ head -c <number> <Filename>


Try it Yourself

What is the default number of lines displayed using the head command in Linux?


How would you print the first N lines of multiple files simultaneously using
the head command?

18. tail
The tail command in Linux prints the last N lines of a given file content. Yes, it's that
simple.

Syntax

$head [Option] [Filename]

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.

tail -n <number> <Filename>: Used to print the first <number> lines of the


specified file.

$ tail -n <number> <Filename>


tail -c <number> <Filename>: Used to print the first <number> bytes of the specified
file.

$ tail -c <number> <Filename>

Try it Yourself

What is the default number of lines displayed using the tail command in Linux?


How would you print the last N lines of multiple files simultaneously using
the tail command?

19. wc

The wc command in Linux expands to 'word count'. It is used to display the number of


lines, words, characters, and bytes corresponding to any file mentioned in the
arguments.

Syntax

$wc [Option].. [File]..

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>: Used to print the number of lines, words, and characters present in a


file.

$ wc <Filename>

wc -w <Filename>: Used to print the number of words present in a file.

$ tail -c <number> <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

The history command in Linux is used to view a history of all the commands previously


executed inside the bash terminal. The total number of executed commands will vary
from one system to another.

Syntax

$ history

Here's an example to help you understand this command.

$ history

Try it Yourself
How would you display the last 5 Linux commands which you've previously executed
inside the terminal?

Don't forget to share your approach with us in the comments below.


Hey, it seems like you just completed learning about 20 new Linux commands along
with their implementations. Pretty amazing right?

Bonus: List of 100+ Useful Linux Commands For Every Developer


Of course, something so practical as Linux commands can never be mastered without
getting your hands dirty in the first place. So, if you're excited to explore more about the
Linux shell and gain a solid understanding of more Linux commands, check out the
following resources to understand Linux basics in a hands-on way:

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 earn scholarships

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.

Found this article interesting? Show your love by upvoting this article.

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

You might also like