Linux Commands For Beginners

Linux Commands

Linux is free and open-source, so you can simply change anything in Linux. There are several Linux Distributions (also known as distros) - Ubuntu, Red Hat, Mint, Debian, Fedora.

Linux Shell or Terminal

A shell is a program that receives commands from the user and gives it to the OS to process, and then it shows the output. Linux distributions come in GUI (graphical user interface), but Linux has a CLI (command line interface). To open the terminal, press Ctrl+Alt+T in Ubuntu, or press Alt+F2, type in gnome-terminal, and press enter.

Basic Linux Commands

For any modification, first you need to go to a particular directory to do the file operation. When you open your Linux OS and login, you will go to your default home directory.

~ is the short code or abbreviation of your home directory. 

You can use the clear command to clear the terminal if it gets filled up with too many commands. TAB can be used to fill up in the terminal. For example, you just need to type cd Doc and then press TAB, the terminal fills the rest up.

Ctrl+C can be used to stop any command in terminal safely. If it doesn't stop with that, then Ctrl+Z can be used to force stop it. You can exit from the terminal by using the exit command. You can power off or reboot the computer by using the command sudo halt and sudo reboot.

1. Present Working Directory: pwd

If you want to know in which directory you are, use pwd command (present working directory). If you have just logged in into the linux, then typed pwd, will get following path: /home/username or /root. It gives us the absolute path, which means the path starts from the root. The root is the base of the Linux file system. It is denoted by a forward slash (/).

2. Change Directory: cd

If you want to change your present working directory, use cd command (change directory).

cd /

For example, if you are in the home folder, and you want to go to the Downloads folder, then you can type in cd Downloads. To go back from a folder to the folder before that, type cd ... The two dots represent back.

3. List Files and Directories: ls

For list of files and folder of your present working directory (or any other directory), use ls (list) command. You can see all the hidden files by using the command ls -a. You can also use the command ls -lah to view the file sizes of all the files in a folder.

4. Make Directory: mkdir

Use the mkdir command when you need to create a folder or a directory. For example, if you want to make a directory called NEWDIR, then type mkdir NEWDIR.

5. Remove Directory and Files: rm

Use rmdir to delete a directory. But rmdir can only be used to delete an empty directory. To delete a directory containing files, use rm. But rm cannot simply delete a directory. Use rm -r to delete a directory and the files in it.

You can use the interactive form of rm to make sure you delete only the files you intend to delete. When you follow rm with the -i option and the name of the file you want to delete, rm prompts you with the name of the file and waits for you to respond with y (yes) before it deletes the file.

6. Create File: touch

The touch command is used to create a file. For example, touch new.txt.

7. Copy, Move or Rename Files: cp, mv

Use the cp command to copy files through the command line. It takes two arguments - the first is the location of the file to be copied, the second is where to copy. Use the mv command to move files through the command line. You can also use the mv command to rename a file. It takes the two arguments, just like the cp command.

8. Get Location of File: locate

To get location of any file, use locate command. This command is useful when you don't know where a file is saved or the actual name of the file. Using the -i argument with the command helps to ignore the case. For example, if you want a file that has the word "world", it gives the list of all the files in your Linux system containing the word "world" when you type in locate -i world.

9. Display Text: echo

The echo command helps to move text into a file. For example, if you want to create a new text file or add to an already made text file, you just need to type in, echo hello world >> new.txt.

You do not need to separate the spaces by using the backward slash here, because you put in two triangular brackets when you finish what you need to write.

10. Read or Display File: cat

By using cat command, you can read or display any text file. suppose you are in folder /etc and want to check php.ini file, you can use cat command.

11. Text Editor: nano, vi

nano and vi are already installed text editors in the Linux command line. The nano command is a good text editor that denotes keywords with color and can recognize most languages. But vi is simpler than nano. You can create a new file or modify a file using these editor. For example, if you need to make a new file named "newfile.txt", you can create it by using the command nano newfile.txt. You can save your file after editing by using the sequence Ctrl+X, then Y (or N for no).

12. SuperUser Do: sudo

The sudo is a widely used command in the Linux command line. if you want any command to be done with administrative or root privileges, you can use the sudo command. For example, if you want to edit a file like like filename.conf, which needs root permissions, you can use the command sudo nano filename.conf.

13. Disk Space: df

Use the df command to see the available disk space in each of the partitions in your system. You can see used and available space in % and in KBs. If you want it to be shown in megabytes, you can use the command df -m.'

14. Disk Usage

Use du to know the disk usage of a file in your system. If you want to know the disk usage for a particular folder or file in Linux, you can type in the command df and the name of the folder or file. For example, if you want to know the disk space used by the Documents folder in Linux, you can use the command du Documents.

15. Compressed Files

Use tar to work with tarballs (files compressed in a tarball archive) in the Linux command line. It can be used to compress and decompress different types of tar archives like .tar, .tar.gz, and .tar.bz2. It works on the basis of the arguments given to it. For example, tar -cvf for creating a .tar archive, tar -xvf to untar a tar archive, tar -tvf to list the contents of the archive, and so on.

Use zip to compress files into a zip archive, and unzip to extract files from a zip archive.

16. System Information: uname

Use uname to show the information about the system your Linux distribution is running. Using the command uname -a shows the most of the information about the system.

17. Packages: apt-get

Use apt to work with packages in the Linux command line. Use apt-get to install packages. This requires root privileges, so use the sudo command with it. For example, if you want to install the text editor jed, you can type in the command sudo apt-get install jed. Similarly, any packages can be installed like this.

It is good to update your repository each time you try to install a new package. You can do that by typing sudo apt-get update. You can upgrade the system by typing sudo apt-get upgrade. You can also upgrade the distribution by typing sudo apt-get dist-upgrade. The command apt-cache search is used to search for a package. If you want to search for one, you can type in apt-cache search jed (this doesn't require root).

18. Change Permissions for Files: chmod

Use chmod to make a file executable and to change the permissions granted to it in Linux. 

19. Host Name and IP Address: hostname

Use hostname to know your name in your host or network. It displays your hostname and IP address. Use this utility if you are not sure that you are logged in on the correct machine. Typing in hostname -I gives you your IP address in your network.

20. Check Connection to Server: ping

Use ping to check your connection to a server. When you type in ping google.com, it checks if it can connect to the server and come back. It measures this round-trip time and gives you the details about it. One use of this command is to check your internet connection. If it pings the Google server (in this case), you can confirm that your internet connection is active.

21. Search in File: grep

Use grep command (Global Regular Expression Print) for searching lines in text based data to match a particular regular expression. You can also get the line number of the match by adding -n to the command. The grep utility searches through one or more files to see whether any contain a specified string of characters. The –w (words) option causes grep to match only whole words.

Some More Linux Commands

  • whoami - ​show the current user.
  • date - ​show the system time and date.
  • uptime - show uptime.
  • clear - ​clear the terminal output.
  • head, tail, sort, uniq, diff, file