20 Basic Linux Commands for Beginners Explained with Examples

4
20 Basic Linux Commands

Are you new to Linux? Here all the list of basic Linux commands contains all the common commands you’ll need to know to get you started.

When dealing with Linux, you need to use a shell – an interface that gives you access to the operating system. The commands are required as inputs to inform or direct a computer program to perform a specific operation. While most Linux distributions are user-friendly and come with an easy to use graphical interface, knowing how to use the command line can be very useful.

So let’s learn the must know basic Linux commands with examples.

1. cd

cd (Change Directory) command in Linux is one of the most important and most widely used command. It is used to change current working directory.

Change from current directory to /tmp.

cd /tmp

Switch back to previous directory where you working earlier.

cd -

Change Current directory to parent directory.

cd ..

Move to users home directory from anywhere.

cd

2. pwd

pwd (Print Working Directory) as the name states, prints the name of the present/current working directory. It prints the path, starting from the root /.

pwd
/home/linuxiac

3. ls

ls (List Files and Directories) is one of the basic commands that any Linux user should know. It lists the content of a directory such as files and folders.

Running ls without parameter will list the content of the current directory.

ls
psforevermore.txt  pulse-linux-9.1r2.0-x64.rpm  website-logo.jpg

Using -l (long format) option will display a long listing of the content of current directory. The command will not only prints the name of the file, but also some attributes such as:

  • permissions
  • owner
  • group owner
  • size of the file in bytes
  • time and date the file is modified.
ls -l
total 22968
-rw-r--r-- 1 linuxiac linuxiac       19 Jul 27 13:53 psforevermore.txt
-rw-r--r-- 1 linuxiac linuxiac 23271352 Jul 28 14:57 pulse-linux-9.1r2.0-x64.rpm
-rw-r--r-- 1 linuxiac linuxiac   240104 Jul 28 15:12 website-logo.jpg

To list the content of a particular directory refer the below command.

ls -l /home/linuxiac/
total 22968
-rw-r--r-- 1 linuxiac linuxiac       19 Jul 27 13:53 psforevermore.txt
-rw-r--r-- 1 linuxiac linuxiac 23271352 Jul 28 14:57 pulse-linux-9.1r2.0-x64.rpm
-rw-r--r-- 1 linuxiac linuxiac   240104 Jul 28 15:12 website-logo.jpg

With the -h option, ls will display file sizes in human-readable format. This option is only meaningful when used in combination with the -l option.

ls -lh
total 23M
-rw-r--r-- 1 linuxiac linuxiac   19 Jul 27 13:53 psforevermore.txt
-rw-r--r-- 1 linuxiac linuxiac  23M Jul 27 14:57 pulse-linux-9.1r2.0-x64.rpm
-rw-r--r-- 1 linuxiac linuxiac 235K Jul 27 15:12 website-logo.jpg

In Linux, a file begins with . is a hidden file. To show it on ls command, we can use -a parameter.

ls -a
.monitoring  psforevermore.txt  pulse-linux-9.1r2.0-x64.rpm  .usage.log  website-logo.jpg

If we want to list directory entries only, we can use -d parameter.

ls -d /home/linuxiac/
/home/linuxiac/

4. cp

cp (Copy) is a command used for copying files and directories in Linux. To copy a file with the cp command pass the name of the file to be copied and then the destination.

For example, by running the below-mentioned command will copy a file website-logo.jpg to a /tmp/ directory.

cp website-logo.jpg /tmp/

If you want to copy the file under a different name, you need to specify the desired file name.

cp website-logo.jpg /tmp/new-logo.jpg

To copy multiple files, pass the names of files followed by the destination directory to the cp command.

cp website-logo.jpg psforevermore.txt images/

To copy a directory, including all its files and subdirectories, use the -r (recursive) option.

For example, we are copying the directory images to images_bckp.

cp -r images/ images_bckp/

5. mv

mv (Move) is used to move one or more files or directories from one place to another. Apart from moving the files, it can also rename a file or directory.

For instance, to move a file named website-logo.jpg from the current directory to images directory, the command would be:

mv website-logo.jpg images/

If you want to just rename a file named website-logo.jpg to new-logo.jpg, you can use the mv command in the following way:

mv website-logo.jpg new-logo.jpg

Just like renaming a file, you can rename a directory using mv command.

For example, to rename a directory named images to images_bckp, the command would be:

mv images images_bckp

6. rm

rm (Remove) is a command-line utility for removing files and directories. 

To delete a single file, use the rm command followed by the file name as an argument:

rm website-logo.jpg

By default, rm does not remove directories. If the -r (recursive) option is presented, rm will removes any matching directories and their contents.

rm -r images/

If the given directory or a file within the directory is write-protected, the rm command will prompt you for confirmation.

To remove a directory named images without being prompted, use the -f option:

rm -rf images/

Attention: Be careful when you are executing rm -rf command. A little typo or ignorance may result into unrecoverable system damage.

7. mkdir

mkdir (Make Directory) is the basic Linux command for creating a directory. While the rm command lets you delete directories, it’s the mkdir command that allows you create them.

Creating directories is pretty simple. All you need to do is to pass the name of the directory you want to create to the mkdir command.

To create a new directory named images in the current directory, the command would be:

mkdir images

Building a structure with multiple subdirectories using mkdir requires adding the -p option. The -p tells mkdir to also create parrent directories as well.

mkdir -p images/upload/new

8. cat

cat (Concatenate) is a command used to display the contents of one or more files without having to open the file for editing. It can read, concatenate, and write file contents to the standard output.

The most basic and common usage of the cat command is to read the contents of files.

For example, the following command will display the contents of the psforevermore.txt file on the terminal:

cat psforevermore.txt
You're the sunshine in my eyes,
You're the color of my life,
You're the reason why I'm here to say "Alright".

9. less

less command is used to displays file contents or command output one page at a time in your terminal. It is most useful for viewing the content of large files or the results of commands that produce many lines of output. 

The syntax for the less command is extremely simple. For example, if you want to read contents of the psforevermore.txt file, the command would be:

less psforevermore.txt

The output of less is divided into sort of pages. You’ll see only the text that fills up to your terminal screen. You can use the up and down arrow keys to move line by line. If you want to move page by page, use space key to move to next page and b key to go back to the previous page.

Simply press q at any given point to exit from less.

10. head

head command is used for outputting the first part of files given to it via standard input. It writes results to standard output. By default head returns the first ten lines of each file that it is given.

To view the first ten lines of a file pass the name of a file to the head command. 

head psforevermore.txt

To set the number of lines to show with head pass the -n option followed by the number of lines to show.

head -n 2 psforevermore.txt

Related: Head And Tail Commands In Linux Explained With Examples

11. tail

tail is the complementary of head command. It print the last N number of data of the given input. By default the tail command prints the last 10 lines of the specified files.

tail psforevermore.txt

You can use the -n option to specify the number of lines to be shown.

tail -n 10 psforevermore.txt

You can also omit the letter n and use just the hyphen - and the number with no space between them.

tail -10 psforevermore.txt

12. grep

grep (Global Regular Expression Print) is used to search for a string of characters in a specified file. It is among the most useful commands in Linux.

For example, to search any line that contains the word color in the filename psforevermore.txt, the command would be:

grep color psforevermore.com
You're the color of my life,

Because the grep command is case sensitive, one of the most useful operators for grep searches is -i. Instead of printing lowercase results only, the terminal displays both uppercase and lowercase results.

grep -i Color psforevermore.txt
You're the color of my life,

To include all subdirectories in a search, add the -r (recursively) operator to the grep command.

The example command below prints the matches for word username in all files in the /etc directory and its subdirectories.

grep -r username /etc

13. man

man (Manuals) gives users access to manual pages for command line utilities and tools. It allows users to view the reference manuals of a command or utility run in the terminal.

The man page includes a command description, applicable options, flags, examples, and other informative sections.

In the terminal window, type man followed by the Linux command name which man page you want to see. For example:

man telnet

To exit, press q.

14. chown

chown (Change Owner) command is used to change the file owner or group. Whenever you want to change ownership you can use chown command. Superuser (sudo) permissions are necessary to execute the chown command.

Changing the owner of a file with chown requires you to specify the new owner and the file.

The following command changes the ownership of a file website-logo.jpg from linuxiac to the user john:

chown john website-logo.jpg

To assign a new owner of a file and change its group at the same time, run the chown command in the format given below.

For example, to set john as the new owner and users as the new group of the file website-logo.jpg:

chown john:users website-logo.jpg

The chown command allows changing the ownership of all files and subdirectories within a specified directory. Add the -R option to the command to do so.

In the following example, we will recursively change the owner and the group for all files and directories in the images directory.

chown -R john:users images/

15. chmod

chmod (Change Mode) allows you to change the permissions on a file. Only root, the file owner or user with sudo privileges can change the permissions of a file.

The references are used to distinguish the users to whom the permissions apply:

  • u (owner): File’s owner.
  • g (group): Users who are members of the file’s group.
  • o (others): Users who are neither the file’s owner nor members of the file’s group.
  • a (all): All three of the above (same as ugo).

The operators are used to specify how the modes of a file should be adjusted:

  • + Adds the specified modes to the specified classes.
  • - Removes the specified modes from the specified classes.
  • = The modes specified are to be made the exact modes for the specified classes.

The modes indicate which permissions are to be granted or removed from the specified classes:

  • r Read the file.
  • w Write or delete the file.
  • x Execute the file or in the case of a directory, search it.

For example, in following command read permission will be added for all three levels: user, group and other.

chmod a+r website-logo.jpg

Remove the execute permission for all users:

chmod a-x website-logo.jpg

Remove the read, write, and execute permission for all users except the file’s owner:

chmod og-rwx website-logo.jpg

If you want the file’s owner to have read and write permissions and the group and other users to have read permissions only:

chmod u=rw,go=r website-logo.jpg

Using the = operator means we wipe out any existing permissions and then set the ones specified.

You can add the execute permission for everyone with the following command:

chmod a+x website-logo.jpg

To recursively operate on all files and directories under a given directory, use the chmod command with the -R (recursive) option.

For example, in following command user can read, write, and execute. Group members and other users can read and execute, but cannot write. This apply to all files and subdirectories under the /tmp/test directory.

chmod -R u=rwx,go=rx /tmp/test

16. top

top allows users to monitor processes and system resource usage on Linux. It is one of the most useful tools in a sysadmin’s toolbox, and it comes pre-installed on every Linux distribution.

You simply need to type this in to launch top:

top

17. find

find allows users to search for files and directories based on conditions. It is one of the most important and frequently used commands in Linux.

Find all the files whose name is website-logo.jpg in a current working directory.

find . -name website-logo.jpg

The -name option is case sensitive. If you don’t know the exact case of the item you’re looking for, you can use the -iname option which is case insensitive.

find . -iname website-logo.jpg

Find all directories whose name is linuxiac in / directory.

find / -type d -name linuxiac

Find all .php files in /var/www/html directory.

find /var/www/html -type f -name "*.php"

18. df

df (Disk Free) is used to display the amount of available disk space for file systems.

To view disk space usage run the df command. This can be useful to discover the amount of free space available on a system or filesystems.

df
Filesystem     1K-blocks     Used Available Use% Mounted on
udev             1984900        0   1984900   0% /dev
tmpfs             403004     1472    401532   1% /run
/dev/vda5       30314436 12798764  16057744  45% /
tmpfs            2015016        0   2015016   0% /dev/shm
tmpfs               5120        4      5116   1% /run/lock
/dev/vda1         523248        4    523244   1% /boot/efi

To view disk space in human readable format pass the -h option. This prints sizes in G for Gigabytes, M for Megabytes and B for Bytes.

df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           394M  1.5M  393M   1% /run
/dev/vda5        29G   13G   16G  45% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
/dev/vda1       511M  4.0K  511M   1% /boot/efi

19. du

du (Disk Usage) is used to estimate the amount of disk space used by a given file or directory.

To find out the disk usage summary of a /home/linuxiac directory tree and each of its sub directories, enter the command as shown below. The output will display the number of disk blocks in the /home/linuxiac directory along with its sub-directories.

du /home/linuxiac
4	/home/linuxiac/Documents
888	/home/linuxiac/.cache/fontconfig
...
8	/home/linuxiac/.ssh
2143400	/home/linuxiac/

If we want to print sizes in human readable format (K, M, G), use -h option.

du -h /home/linuxiac
4.0K	/home/linuxiac/Documents
888K	/home/linuxiac/.cache/fontconfig
...
8.0K	/home/linuxiac/.ssh
2.1G	/home/linuxiac/

To get the summary of a grand total disk usage size of an directory use the option -s as follows.

du -sh /home/linuxiac
2.1G	/home/linuxiac/

20. ps

ps (Process Status) command is used to display the currently running processes in the system. It will display the list of processes running on the system including details such as process id, name of terminal currently logged in, CPU time, etc.

System administrators generally use ps with a, u, x, and w options in order to get all details in a single ps command execution.

  • a: All other user processes.
  • u: Owner of the process.
  • x: Other processes those not attached to the terminal.
  • w: Wide output.
ps auxw

Conclusion

Basic Linux commands help users execute tasks easily and effectively. Although you can perform most of the system-related tasks using a graphical interface, the command line makes you more productive and able to get more done in less time.

If you have any questions or feedback, feel free to leave a comment.

4 Comments

  1. rm -rf dir/ is very dangerous, better use : rm dir/ -rf (-rf at the end) so we have a reflexion grace delay, avoiding catastrophic récursive delete… 😏

  2. Everytime I scroll through a list like this, and it is good and useful for new users, I wonder about pwd. I can’t remember the last time I installed a distro that didn’t come with .bashrc already configured to provide that information in the prompt. I probably haven’t used pwd in over a decade.

    • I think like many other at-first-glance antiquated inclusions in shells, PWD is useful in scripting if nothing else.

      I once made a sort of home TV station that would randomly select and schedule a series of shows to play throughout the day on an remote output so I could simply flip to an input on my TV and watch. With that script, I called another function to email me the schedule for the day, and PWD was useful for the finding, the sorting, and the collating/formatting of the schedule output file because of the way I structured my storage (Show/Season/EpName, etc)

Leave a Reply

Your email address will not be published.

Latest from Tips & Tricks