basic linux commands

What is Linux and why should you learn more about it?
Linux is the most commonly used server-based operating system. It’s also common in embedded systems. Google Chrome and Google Earth, for example, are heavily reliant on Linux. So, whether you want to build server-side applications or deal with embedded systems, Linux will make your life as a developer much easier.

Learn to use Linux Machine and get familiar with commonly used developer workflows in Crio’s 7-day free trial. Enroll now!

There’s a fair chance you’ll have to interact with the bash terminal at some point in your programming career, so it’s wise to get familiar with it early on to gain a competitive edge over other developers who don’t.

The core of the entire GNU/Linux operating system used to administer the entire system is Linux commands. You may not realize it, but most of the applications you run in the graphical user interface run Linux commands in the background for you to complete the task at hand.

Explore list of fascinating Linux commands you can use to do various tasks
By the end of this blog, you will have a strong grasp of these 20 basic Linux commands which are extensively used in the bash terminal.

Basic 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

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

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

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

$ cat test1.txt

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

$ cat -n test1.txt test2.txt

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

$ touch test1.txt

touch : Used to create multiple files simultaneously.

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

6. mkdir

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

Syntax

$mkdir [Option].. ..
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 : Used to create a folder with a specified name.

$ mkdir

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

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”

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.

$ rm

rm -i : The ‘-i’ tag enables the rm command to request confirmation from the user before deleting the corresponding file.

$ rm -i “

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.

$ rmdir

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.

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 : Used to retrieve relevant content associated with the specified URL from the Internet.

$ wget

wget -O : Used to retry a file download for a given number of times.

$ wget -O

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

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

$ mv

mv : Used to transfer a file from a given directory to a different directory.

$ mv

You can see that our index.html file has been moved inside the Projects/ directory.

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: Used to copy the contents of the source file into the destination file.

$ cp

mv : Used to transfer a file from a given directory to a different directory.

$ mv

You can clearly see that the contents of the test2.txt file have been overwritten with the contents of test1.txt.

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

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

You can clearly see that the matched patterns are clearly highlighted.

grep -c : Used to display the count of the number of occurrences of a given pattern inside a specified file.

$ grep -c

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 : Used to create a new file and enter it into Vim editor.

$ vi

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 : Used to print the first lines of the specified file.

$ head -n

head -c : Used to print the first bytes of the specified file.

$ head -c

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 : Used to print the first lines of the specified file.

$ tail -n

tail -c : Used to print the first bytes of the specified file.

$ tail -c

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

$ wc

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

$ tail -c

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