Learn 20 Basic Linux Commands with Simple Examples

Learn 20 Basic Linux Commands with Simple Examples

Bonus: Download list of 100+ Linux Commands [Free]

History of Linux

Back in the 1950s, did you know that computers used to be as enormous as houses? So you can already imagine how cumbersome it must have been to operate them.  Furthermore, each computer had its operating system that was fundamentally different from many other computers, making it even more difficult to operate on different machines at once.

In 1969, a team of engineers at Bell Labs decided to work on a standardized operating system known as "Unix." It stood out from other operating systems due to its simple codebase and use of the 'C' programming language rather than assembly language.

In 1991, Linus Torvalds (a student at Helsinki University) decided to work on a freely accessible version of Unix because, up until that moment, Unix had been reserved exclusively for government entities and major financial enterprises, leaving the populace with essentially the same problems that had led to Unix's rise in the first place.

Today, Linux finds extensive uses in smartphones, desktops, laptops, tablets, and home appliances like modems, washing machines, and routers. Commercial behemoths such as IBM and Hewlett-Packard (HP) rely heavily on Linux support for their commercially available products and applications.

Understanding "distributions" in Linux

Linux comes with a wide range of variants to accommodate the expectations of every kind of consumer. 'Distributions' (or 'distros') are the generic names given to these variants. Almost every Linux distribution can be downloaded and installed for free on your machine.

Following are some of the most common Linux distributions :

  • Ubuntu
  • Linux Mint
  • Manjaro
  • Fedora
  • Debian

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.

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.

Get hands-on with Linux, and other Developer essentials in Crio's 7-day free trial

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.

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.

Before you proceed further, take a look at the list of Linux commands you'll be exploring in this blog:

  • man
  • cd
  • ls
  • cat
  • touch
  • mkdir
  • pwd
  • echo
  • rm
  • rmdir
  • wget
  • mv
  • cp
  • tree
  • grep
  • vi
  • head
  • tail
  • wc
  • history

Download Cheat Sheet with 100+ Linux Commands [Free]

You will find working examples alongside every command which will help you understand their functionality in a better way.

Since at Crio we believe in Learn by Doing, we won’t let you go without some fun activities to strengthen your understanding of these shell commands along the way.

Without further ado, let's dive right in.

20 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

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

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.

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


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

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


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?

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?

Share your answer with us 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].

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.

Don’t miss out on the complete list of 100+ Linux commands


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.


Save for later: Super Handy Linux Commands for Software Devs

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.

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?

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


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?

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


14. tree

The tree command in Linux can be used to list out the contents of directories in a tree-like fashion.

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.

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.

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 as such, I recommend that you check out the following two links below:


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!

Sandipan Das

Written by Sandipan Das

Trying to bring about a difference through words.
You've successfully subscribed to Crio Blog
Great! Next, complete checkout to get full access to all premium content.
Welcome back! You've successfully signed in.
Unable to sign you in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Billing info update failed.