Basic Linux Commands With Examples






If you are a Linux user or if you have recently installed Linux on your computer it is good to know these basic Linux commands. Although most of the Linux Operating System comes with a decent GUI, it is important to know and use the power of the Command Line Tool. There are many Linux commands, but these are the common commands that you will often use while using Linux. For the sake of understanding, I have divided this article into two parts, Basic commands, and Intermediate commands.

What you will learn?
In this article, you will learn how to navigate in the Linux terminal and some basic CRUD (Create Remove Update Delete) operations.

1. pwd (Print Working Directory):

When you are using a terminal in Linux, it is very easy to get lost in the command line interface. So, it is very important to know where exactly you are before doing any operations.

To know in which directory you are currently present in, you can use the pwd command. pwd command prints the current working directory i.e, the directory that you are currently present in.


pwd command



2. ls (list):

ls(list) command prints the information about the files that are present in the directory (by default ls command prints the current working directory ie, the directory you are currently present in).

ls command (list)

Including the files present in the above image there are lots of files that are hidden, and it is not listed in a typical ls command.

You may ask - Why is it not listed?, - Well the answer is that you don’t actually have to see them. These are the small system files that are necessary to run your system smoothly, and there is no point to list such files. But, if you want to list those files anyway then you can use ls -a command.


ls - a command


If you want to list all the hidden files along with their file permission, size, when it was created you can use ls -al command.


3. cd (Change Directory):

the cd command is used to go to a directory. For example, suppose you are in the home directory and you want to go to the Desktop directory, you can do so by typing ‘cd Desktop’ in the terminal.


cd command

Note:

  • the cd command is case sensitive, you have to write the exact name of the folder or else you will get an error.
  • If you want to change to a folder which is of more than one word then you have to use a backslash in between each space, for example, cd web\ tools OR you have to put the word in single quotes cd ‘web tools’.
  • If you want to go back to the previous directory (parent directory) you can type cd .. command.
  • If you want to go to the root directory, instead of typing cd .. several times you can type cd / command to directly go to the root folder.

4. mkdir (Make Directory):

mkdir command is used to create folders. Suppose, if you want to create a folder name ‘Linux’ you can type mkdir Linux in the terminal.

If your folder name is of more than one word for example ‘i love Linux then you have to use a backslash in between each and every space i.e, ‘i\ love\ Linux.


mkdir Command


5. rmdir (Remove Directory):

rmdir command is used to delete a folder or a directory. Taking the previous example, if you want to delete the Linux folder then you have to type rmdir Linux in the terminal and then press enter.


rmdir command


Note: rmdir is used to delete only the empty directories or folders. If you want to delete a folder that contains files then you have to use the rm command.


6. rm :

rm command is used to remove (delete) files (.txt, .jpg, .pdf etc. any type of files). By default, the rm command does not remove directories but you can use rm with -r or -R option to recursively delete any matching directories, sub-directories, and all files they contain.


rm command


7. cp (Copy file or Directory):

cp Command is used to make copies of files or folders. cp command takes two arguments, the First one for the source address and the second for the destination address.


cp command


8. mv (Move):

mv Command is used to move files through the command line. Like cp command, mv command also takes two arguments, the First one for the source address and the second for the destination address.


mv Command


You can also use mv command to rename files.


mv command to rename file


9. Locate & updatedb:

As the name suggests, locate command is used to locate files in the Linux system. locate command is just like search in Windows.


locate and updatedb command


One thing to note is that locate command does not search the file system, rather it searches the database log of the file system created by updatedb command. So if you recently created a new directory then you have to update the database log by using the updatedb command otherwise you will not be able to find the file.


10. man & --help:

There are lots of commands in Linux and it is not possible to remember all of them and what it does. Luckily, Linux has provided a user manual for all commands. You can access the manual by typing man <keyword>. For example, if you cannot remember what ls command does you can simply type man ls, and it opens the manual with a proper description and a full list of options that you can perform using the command.


man command


You can also use --help to get the help page of command.





Comments

Post a Comment