en
  English
Phone :  +370 (5) 204-1903
Email :  sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. Basic Linux Commands with Examples [Linux Cheat Sheet]

Basic Linux Commands with Examples [Linux Cheat Sheet]

Since the mid1990s, Linux has been in the market but now has become the most widely used operating system in the world. Nowadays, you can find Linux on your smartphones, computers, servers, cars, home appliances and pretty much everything. To make your product, you should have knowledge of basic Linux commands. This blog post will come up with the best commands which you can go for:

Linux Tutorial Nov 13, 20 by Nisal N 18 min Read
Basic Linux Commands with Examples [Linux Cheat Sheet]

Linux has become one of the most popular platforms on the planet. It is mostly used in servers, and more than 90% of the internet, is powered by Linux servers. It is open-source software, making it possible for anyone to make changes to the code and redistribute it under a different name. Some of the most famous Linux-based operating systems are:

  • Ubuntu
  • Android
  • Fedora
  • FreeBSD
  • Debian
  • Kali Linux
  • CentOS
  • Red Hat Linux
  • openSUSE

Linux is everywhere! The chances of you coming across a Linux interface are very high. A fun fact is that it is a mostly used operating system without a GUI (graphical user interface), which results in some complications for beginners.

Before starting our journey, we need to familiarise ourselves with the terminal or shell.

What is Linux Shell?

In short, the user enters commands to the system, and the system will show the output as a result. Most Linux distributions come with a CLI (Command-line interface), and you use the terminal/shell to input the commands.

Linux distros come with a GUI, but this tutorial is solely dedicated to beginners who want to familiarize themselves with the Linux command line.

Now let’s open the terminal and get started with the commands.

Note that the Linux shell is case-sensitive, so you should be careful when entering commands.

However, we researched and compiled a list of basic Linux commands that will get you to master the Linux environment. We have separated it according to the level of experience you have:

  1. Basic Linux commands
  2. Intermediate Linux commands
  3. Advanced Linux commands

Basic Linux Commands for Beginners

1. PWD command

The PWD command will show you the current working directory (also known as a folder) you are working on. It will show the absolute path with all the directories.

Here's an example: when you type in the pwd command while inside a folder called folder1, the result will be as follows:

/root/folder1

pwd command

2. LS command

The ls command is used to show all the content inside a directory. The default settings will show the contents of the current directory.

If you want to see the content in other folders, you should type the folder path after the ls command.

For example, you should enter the file's absolute path after the ls command to view the files inside the books folder.

ls /root/folder1/books

The ls command has many variations:

ls –R  List all files in subdirectories
ls –a List of hidden files
ls -al List detailed information of files and directories (permissions, size, owner, etc.)

ls command

3. CD command

The cd command is used to navigate between files and folders. You will need to type the full path or the directory's name (depending on your current directory).

There are some shortcuts to help you navigate quickly:

  • cd .. (with two dots) to move one directory up
  • cd to go straight to the home folder
  • cd- (with a hyphen) to move to your previous directory

An example of the cd command will be:

cd folder1

cd command

4. MKDIR command

You can use the mkdir command to create new directories. For example, To make a directory called pictures, you can type mkdir pictures.

Here are some tips on how to use the mkdir command:

  • To generate a new directory inside another directory. Use this format

mkdir pictures/europe

  • Using p to create directories in between two existing directories. Here's an example:

mkdir –p pictures/trips/Europe

mkdir command

5. RMDIR command

If you need to delete a directory, use the rmdir command. However, rmdir only allows you to delete empty directories.

For example:

rmdir folder1

rmdir command

6. RM command

You can use the rm command to delete files. If you want to delete all the files in a directory, you can use this rm –r command.

Here's an example. Let's say that in folder1, there is a text file named test.txt. To remove this file from folder1, enter:

rm test.txt

7. TOUCH command

The touch command is used to create empty files directly from the command line. For example, the following command will create a new text file with the name 'docnew'.

touch docnew.txt

You can also create files in a different location by specifying the path which will generate an HTML file under the docs directory.

touch /home/1gbits/docs/mywebsite.html

touch command

8. FIND command in Linux

The find command is used to search for files and directories. Just like the name suggests, the find command is used to find files within a given directory.

An example is:

find docnew.txt

find command

9. MV command

The mv command has two functions:

Here's are two examples (check the screenshot if it is not so clear):

To move a file, you can type the command as shown in the example, and you should set the name of the file that needs to be moved and the path.

mv test.txt /home/1gbits/document

To rename files, you can use the below command. The name of the file will be changed to file5.txt.

mv file1.txt file5.txt

mv command

10. NANO command

The nano command is used to create and edit text files. This easy-to-use command-line text editor includes all the basic functionalities of a regular text editor. Let me show you how to create a file using the nano command:

nano test

Enter the text inside the editor and exit to save the file.

11. SUDO command

The sudo command will let you perform tasks that require root privileges. However, it is not advised to use this command for daily usage because it will be hard to make it right if an error is made.

12. HISTORY command

Once you start using the Linux working environment daily, you will soon notice that you can run 100s of different commands every day. To know about all the Linux terminal commands that you have entered, you can enter history command to review all the commands you've entered.

13. APT-GET command

You can use apt to work with packages in the Linux command line. Apt-get is used to install packages and requires root privileges. You can use the Sudo command with it. Here are some examples of using the apt-get command.

  • To install the text editor Jed, you can type in;

sudo apt-get install jed

  • To update your current repository, enter;

sudo apt-get update

  • To upgrade the system, type;

sudo apt-get upgrade

14. CAT command

CAT (short for concatenate) is one of Linux's most frequently used commands. This is used to list the contents of a file on the standard output.

For example, this command will open the file and display the contents.

cat testdoc.txt

Here are other ways to use the cat command:

  • To create a new file:

cat > filename

  • To join two files (1 and 2) and store the output of them in a new file (3):

cat filename1 filename2>filename3

  • To convert a file to upper or lower case use:

cat filename | tr a-z A-Z >output.txt

15. CP command in Linux

The cp command is used to copy files between two places through the use of the command line. The command takes two arguments; the first is the file's location that needs to be copied, and the second is the destination of the file where the file should be copied. Here's an example.

There is a file named my.txt in the current directory. You want to move this file to a folder called personal. Type this command

cp my.txt /home/user/personal/

Note the spaces used in the command.

16. ZIP, UNZIP command

Use the zip command to compress files into a zip archive and use the command to extract the zipped files from a zip archive.

17. MAN command

There are many Linux commands, and we don't remember what each of them does. In such situations, the man command comes to help. It shows the manual pages of the command. For example, man ls will show the manual pages of the ls command.

No matter command, with the man command, you can know everything about each of them.

man command

18. WHATIS command

Whatis command in Linux is used to get a one-line manual page description. In Linux, each manual page has some description within it. So this command search for the manual pages' names and show the manual page description of the specified filename or argument.

Here's an example which will display a help message with one-line explanations.

whatis -help

Intermediate Linux Commands

1. ECHO command

The echo command is used to move data into a file. A simple example of how this command works will be as follows. Imagine you wish to move the text "I like the Linux command line" into a file called interests.txt; then, the corresponding command will be:

echo I like the Linux command like >> interests.txt

2. DIFF command

The diff command compares the content of two files line by line. This is useful when you need to make program alterations instead of writing the entire code again.

An example would be:

diff code1.txt code2.txt

3. JOBS command

The jobs command will display all currently running jobs along with their status. A job is a process that is started by the shell.

4. KILL command

Like in Windows, when we encounter an unresponsive program, we end the task from the taskbar. You can terminate these kinds of programs from Linux using the kill command.

There's quite a range of using the kill command, which can be checked using the below command.

kill –l

Even though there is a list of 64 different commands, the most common ones are SIGTERM (15) and SIGKILL (9).

  • SIGTERM (15)— requests a program to stop running and gives it some time to save all of its progress. This signal will be used if you don't specify the signal when entering the kill command.
  • SIGKILL (9)— forces programs to stop immediately. Unsaved progress will be lost.

Knowing the kill command is not enough to terminate the process without the PID number (process identification number). To find out the PID, run the command:

ps ux

An example of terminating a process will look like this:

kill -{kill number} PID

This command kill -9 2020 will terminate the process 2020.

5. WGET command

If you want to download something from the internet, you can do that using the Linux command line. Use the wget command followed by the download link.

An example is shown below, where I downloaded the content of 1gbits.com, which will be saved as an HTML file.

wget https://www.1gbits.com

6. PING Command

Like in windows, you can use the ping command to check the connectivity status to a server/internet. Here's an example where we checked if our system can connect to 1gbits.com and the response time. Note that the pinging will not stop automatically, so simply use ctrl+z to cancel.

ping 1gbits.com

7. WHEREIS Command

The whereis command in Linux is used to locate a command's binary, source, and manual page files. This command will search for files in the restricted set of locations (man page directories, binary file directories, library directories). The most common usage of this command is to find the executable files of a program, configuration files and the man pages.

The syntax is fairly simple: you type whereis followed by the name of the program.

For example, whereis netstat will show the netstat executable and the location of the man page.

8. SERVICE Command

The service command runs System V init scripts. The available syntax for using the service command is as follows:

service SCRIPT COMMAND [OPTIONS]

service --status-all

service --help | -h | --version

To use the service command is not complicated at all. Let me give some simple examples (you will need root privileges):

  • To run a script:

service sshd start

  • To stop an already running script:

service sshd stop

  • To restart an already-running service:

service sshd restart

9. ALIAS command

The command alias creates 'aliases' for your most commonly used commands. Aliases are like custom-made shortcuts to represent a command.

To view all the default sets of aliases, enter:

alias

To create a temporary alias, enter the name you wish to use followed by a '=' sign and quote the command you wish to alias.

The syntax is as follows:

alias yourname="The custom command"

An example of using an alias is as follows:

alias sus="cd /home/test/Downloads"

10. DF Command

The df command displays information related to the file system's total and available space.

The most common options for using the df command are as follows:

  • a –all: includes pseudo, duplicate and inaccessible file systems.
  • B –block-size=SIZE: scales sizes by SIZE before printing them.
  • h –human-readable: print sizes in the power of 1024
  • H –si: print sizes in the power of 1000
  • i –inodes: list inode information instead of block usage
  • l –local: limit listing to local file systems
  • P –portability: use POSIX output format

Here's an example of using the df command to display all file system information:

df -a

11. PASSWD Command

passwd command in Linux is used to change the user account passwords. The root user reserves the privilege to change the password for any user on the system, while a normal user can only change the account password for his or her own account.

To change your password in Linux, enter:

passwd

12. LPR command

The Linux command lpr submits files for printing. The LPR Command transmits and manages print jobs.

When using the LPR command, you can use the following options:

  • E: Forcesencryptionwhen connecting to the server.
  • P destination: Prints files to the named printer.
  • # copies: Sets the number of copies to print from 1 to 100.
  • C name: Sets the job name.
  • J name: Sets the job name.
  • T name: Sets the job name.
  • l: Specifies that the print file is already formatted for the destination and should be sent without filtering. This option is equivalent to-oraw.
  • o option: Sets a job option.
  • p: Specifies that the print file should be formatted with a shaded header with the date, time, job name, and page number. This option is equivalent to-oprettyprint and is only useful when printing text files.
  • r: Specifies that the named print files should be deleted after printing them.

13. CMP command

CMP command in Linux/UNIX is used to compare two files. It will compare byte by byte and will help you to find out whether the two files are identical or not.

  • When CMP is used, it reports the location of the first mismatch to the screen if the difference is found, and if no difference is found, the files compared are identical.
  • CMP displays no message and simply returns the prompt if the files compared are identical.

Advanced Linux Command-List

1. ifconfig Command

Ifconfig stands for "interface configuration". It is used to view and change the configuration of the network interfaces on your system. Running the ifconfig command with no arguments will display information about all network interfaces currently in operation.

2. CHOWN command

When it comes to the Linux infrastructure, all files are owned by a specific user. In the complete list of Linux commands, the chown command enables you to change or transfer a file's ownership to a particular user.

Here's an example. Imagine a user named john, and the root user wants to provide the ownership of the file data.txt to john. The command will be as follows:

chown john data.txt

The ownership of the file will be under the user john.

3. TOP command

Like the Windows task manager, the top command will display a list of running processes and how much CPU each process uses. This is very useful for monitoring system resource usage.

4. USERADD, USERDEL Command

Linux is a multi-user system, which means that more than one user can use the same system simultaneously. To show how to add a user, follow the screenshots that are shown below.

Adding a user max can be done by entering:

useradd max

To check if the user is added, you can enter the following:

cat /etc/passwd

The user is created. Now to delete the user max, we can use the command:

userdel max

Once deleted, check the list of users; you will no longer see the user max.

5. GREP command

The grep command is used to search for all the text in a given file. For example, the following command will search for the word "dummy" in the notepad file. Lines that contain the searched word will be displayed and highlighted.

grep dummy testdoc.txt

grep command

6. HEAD command

The head command will make your life much easier when going through documents. This command is used to view the first lines of any text file. By default, it will show the first ten lines of text.

The format will look something like this:

head testdoc.txt

head command

7. TAIL command

This one has a similar function to the head command, but instead of showing the first lines, the tail command will display the last ten lines of a text file.

8. GCC command

GCC stands for GNU Compiler Collections, which compiles mainly C and C++ language. It can also be used to compile Objective C and Objective C++.

Syntax of GCC command is

gcc [-c|-S|-E] [-std=standard]

Let's go through an example of using the GCC command.

gcc source.c

This will compile the source.c file and give the output file as a.out file, which is the default name of the output file given by the GCC compiler, which can be executed using ./a.out

Conclusions

There are many more best Linux commands that are out there, but we believe that with this basic Linux command list, you can start getting familiar with the world of Linux. Here are some tips you can follow:

  • You can use the clear command to clear the terminal
  • Enter TAB can be used to fill the rest of the text of the command. For example, after you type cd doc and press TAB, it will automatically fill it as cd documents
  • you can close the terminal by typing in exit
  • You can turn the computer ON and reboot the computer by using the sudo halt and sudo reboot

If you feel that some more commands should be added to this list, you can let us know through the comment section below. 

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment