Basic Linux Commands Explained with Examples

This tutorial explains the basic Linux commands for beginners with practical examples. Learn the essential commands of the Linux and their usages in details.

Advertisements

pwd command

The pwd command prints the absolute path of the current working directory. This command does not accept any argument.

pwd command

mkdir command

The mkdir command creates new directory. To create a new directory, we have to specify the name and location of that directory with this command as the argument. If we don’t specify the location, it creates the specified directory in the current directory.

This command create new directory, only if the user have the write permission on the specified location. If the user does not have the write permission on the specified location, this command does not work.

This command also allows us to create a hierarchy of sub-directories. To create multiple directories in hierarchy, use option -p with this command.

mkdir command

cd command

Advertisements

The cd command changes current working directory to the specified directory. To specify the target directory, we can use both types of pathname; absolute path name and relative path name.

cd command

touch command

The touch command creates an empty file. Like the mkdir command, to create a new file, this command also requires the name and location of the file. If the specified file already exists at the specified location, it updates the time stamp of that file.

touch command

cat command

This command performs three tasks; create a new file, append contents in an existing file and print the contents of the file. Which task it performs, is depend on how the file name is supplied.

  • If file name is supplied after using the single greater-than sign (>), it creates new file.
  • If file name is supplied after using the double greater-than sign (>>), it appends the existing file.
  • If file name is supplied without using any sign, it prints the contents of the specified file.

To create a new file or append an existing file, specify its name with this command after typing the sign > and >> respectively and hit the entry key. Type the contents that you want to store in the new file or append in the existing file. When you are done, press ctrl+d keys to save to the file.

Key points
  • This command doesn’t allow us to edit or modify an existing file.
  • When appending an existing file, this command doesn’t display the existing contents of that file.
  • When appending an existing file, always make sure you use the double greater-than sign (>>). If single greater-than sign is used, it simply overwrites the existing contents of the file without any warning or notification.
  • When displaying the contents of the specified file, it displays all contents at once. If file’s contents are more than the screen size, contents will be scrolled up.

cat command

more command

Advertisements

The more command displays the contents of the specified file. If contents are more than the screen size, it displays only the contents those can fit in screen. To display the next contents, we have to press the Enter key. It provides scroll facility only in down direction.

more command

less command

The less command also displays the contents of the specified file. This command provides scroll facility in both directions. Up and down arrow keys are used to scroll in the up direction and in the down direction respectively.

less command

Similarities and differences between the cat command, more command and the less command

All three commands display the contents of the specified file. The cat command displays all contents at once and doesn’t provide scroll facility. The more command displays contents equal to the screen size and provides scroll facility only in down direction. Just like the more command, the less command also displays the contents equal to the screen size. But it provides scroll facility in both directions.

head command

The head command prints the specified lines from the specified file. To specify the number of line, the option -n is used. It prints the specified number of lines from top of the file. For example, number of lines and name of file are specified as 3 and users respectively, it prints first three lines from the file users.

tail command

The tail command works the same way as the head command works, except it prints the specified number of lines from the bottom of the file.

head and tail command

Similarities and differences between the head command and the tail command

Both commands print the specified number of lines from the specified file. Both commands use the option -n to specify the number of lines. If number of lines is not specified, by default both commands print 10 lines. The only difference between both commands is that the head command prints line from top of the file while the tail command prints the lines from bottom of the file.

wc command

The wc command counts the lines, words and characters from the specified file. It always displays the counted number in the sequence; Lines, Words and Characters.

wc command

rmdir command

The rmdir command deletes an empty directory. If directory is not empty, this command does not delete it.

rmdir command

rm command

The rm command deletes file. Following table lists some common options used with this command.

Option/options Description/Used for
-d To delete an empty directory. By default, it does not delete a directory.
-r To delete recursively.
-i To verify each file or directory before delete. To delete, press y. To skip, press n.
-f To delete without verification.
-rf To delete all contains of a directory including all of its subdirectories and files.

rm command

ls command

The ls command lists the contents of directory. Without any argument, it lists the contents of the current directory. To list the contents from other directory, specify its absolute pathname as the argument.

Following table lists the common options used with the ls command.

Option/Options Description/Used for
-l To list the contents in the list format. List format provides additional content details such as file type, permission, creation date and time, owner and group information etc.
-a To include hidden files in output. By default, the ls command does not list the hidden files.
-R To list contents recursively. By default, the ls command lists the contents of the specified directory only. This option forces it to list the contents from the subdirectories of the specified directory also.
-ltr To list the contents in the sorted list format. Output is sorted based on the last modification date and time.

ls command

That’s all for this tutorial. If you like this tutorial, please don’t forget to share it with friends through your favorite social network.

ComputerNetworkingNotes RHCE 7 Study Guide Basic Linux Commands Explained with Examples