Skip to content

Linux commands

Commonly used commands in linux. Open up terminal using ctrl+alt+t and do the exercises as you learn each command. Read Files before starting this since you will need it to do the exercises.


Pipe is used to redirect the output of one command/program to input of another command/program.
The character | is used for this purpose. You can see the below example.

cat file | grep gives the contents of file to grep.


Lists the contents of the current directory or the directory that we mentioned.


  • ls - lists the contents of the current directory.

  • ls dir - lists the contents of directory dir.

  • ls -a dir lists the contents including the hidden files/folders.

    • Hidden files/folders - files that start with a .
  • ls -l file - gives information about owner, creation date and time, permissions for file.

For more information,

$ man ls

  • List the contents of Desktop directory.


Used to create a new directory.

  • mkdir dir - will make a new directory dir.
  • mkdir -p dir1/dir2 - creates the required parent directories if it dosent't already exist.

For more information,

$ man mkdir

  • Create a directory with the name myfirst and a directory mysecond inside myfirst.
  • Try to do this in one command.


Used to change from one directory to another.

  • cd dir - changes the directory to dir.
  • cd or cd ~- moves to home directory.
    • ~ - /home/{user}/
  • cd .. - moves to parent directory of current directory.

For more information,

$ man cd

  • Change the working directory to myfirst.


Used to create a empty file.

  • touch file - creates a new empty file with filename file.

For more information,

$ man touch

  • Create a file test in the current directory.


Display the contents of a file or concatenate files.

  • cat file - displays contents of file.
  • cat file1 file2 > file3 - create a new file file3 with contents of both file1 and file2
  • cat file1 >> file2 - adds contents of file1 to end of file2

For more information,

$ man cat

  • Print the contents of test.


Used to make copies of files or directories.

  • cp file1 file2 - copies contents of file1 to a new file file2.
  • cp -r dir1 dir2 - copies the directory dir1 to a new directory dir2 with the contents of dir1.

For more information,

$ man cp

  • Copy test to mysecond.


Move a file or directory into another file or directory or rename files or directories.

  • mv file1 file2 - changes the name of file from file1 to file2.
  • mv dir1 dir2 - changes the name of directory from dir1 to dir2.
  • mv file1 file2 dir1 - moves both file1 and file2 to the directory dir1 given that the directory exists.

For more information,

$ man mv

  • Move mysecond to ~.

rm & rmdir

Removes a file or a directory.

  • rm file1 - removes the file file1.
  • rmdir dir1 - removes the directory dir1 only if the directory is empty.
  • rm -rf dir1 - removes the directory dir1 even if it has contents.

For more information,

$ man rm

$ man rmdir

  • Try removing myfirst directory using rmdir. What happened?
  • Now remove test inside myfirst using rm and try removing myfirst again.
  • Remove mysecond using rm. What happened?


Gives the argument to standard output.

  • echo argument - writes argument to standard output.
$ echo "Hello World!"
Hello World!

For more information,

$ man echo

  • Recreate myfirst and test. Put the following data into test using echo.
Name    Height Age Place  
Arun    172    18  Delhi  
Amy     160    22  NYC  
Benny   175    35  LA  
Xavier  165    15  Egypt  
Mani    170    23  Florida  


Searches file for specified string or expression.

  • grep "hello" file - gives the lines that contain hello.
  • grep -i "hello" file - search for the string case insensitively.
  • grep -c "hello" file - prints the number of lines which has the string.
  • grep -v "hello" file - displays the line that are not matched.
  • grep "^hello" file - matches the lines that start with the string.
  • grep "hello$" file - matched the lines that end with the string.

For more information,

$ man grep

  • Find lines that start with A from test file. (Use |)


Used for deleting or translating characters.

  • tr -d 'w' file - removes all w from the file.
  • tr -s file - replaces multiple spaces in a string with single space.
  • tr -cd 'w' file - complement of what other option does. So here removes everything except w.

For more information,

$ man tr

  • Print content of test with a single space in between words.


The cut command is a command for cutting out the sections from each line of files.

  • cut -b 1,2,3 file.txt - prints the first 3 bytes of each line in the file.
  • cut -b 2-5 file.txt - prints the 2nd to 5th bytes of each line.
  • cut -d " " -f 1 file.txt - d spefifies the delimiter and f specifies the field. In this example, 1st word from each line is printed.

For more information,

$ man cut

  • Print only the age column from test file. Use the output of the above exercise. It will be something like cat test | tr <options> | cut <options>.


Sort command sorts the contents of a text file, line by line.

  • Lines starting with a number will appear before lines starting with a letter.
  • Lines starting with a letter that appears earlier in the alphabet will appear before lines starting with a letter that appears later in the alphabet.
  • Lines starting with a lowercase letter will appear before lines starting with the same letter in uppercase.

  • sort file.txt - sorts the contents of the file.

  • sort -o out.txt in.txt - puts the output to out.txt.
  • sort -r file.txt - sorts in reverse order.
  • sort -n file.txt - sorts the files in numeric order.
  • sort -u file.txt - removes duplicate elements.

For more information,

$ man sort

  • Print the sorted age from test. Use the output of above command.
  • Print the sorted name from test.


Displays a manual page for the command.

  • man command - displays a manual page about the command.

For more information,

$ man man


  • pwd - prints the absolute path of the current directory.

For more information,

$ man pwd


  • clear - clears the teminal screen and put the cursor back at the top of the window.

For more information,

$ man clear

find & locate

Used for locating a file/directory.

  • locate - is fast way to locate files and uses a recorded snapshot of the system and might miss recent additions.
  • find - can be also used to find files but might take some time depending on the size of file system.

For more information,

$ man locate

$ man find


Used for logging into a remote machine.

  • ssh - connects to a remote computer called
  • ssh - can be used to log in as a different user.
  • ssh -p port - can be used to specify the port to connect to.

For more information,

$ man ssh


scp copies files between hosts on a network.

  • -r - can be used for copying a directory recursively.

Remote to local machine:

  • scp username@ip:file_on_remote.txt folder_on_local

Local to remote machine:

  • scp /path/file_on_local username@ip:folder_on_server

For more information,

$ man scp


For ssh and scp, you might have to provide ssh key using -i key.


Prints the defined environment variables.


Prints out the hex dump of the given file.


$ xxd file.txt
00000000: 4865 6c6c 6f20 576f 726c 6421 0a         Hello World!.

For more information,

$ man xxd


Print the sequences of printable characters which are aleast 4 characters long in file.


$ strings file.txt

For more information,

$ man strings