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

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.

ls

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

Usage

  • 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
Exercise

  • List the contents of Desktop directory.

mkdir

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
Exercise

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

cd

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
Exercise

  • Change the working directory to myfirst.

touch

Used to create a empty file.

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

For more information,

$ man touch
Exercise

  • Create a file test in the current directory.

cat

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
Exercise

  • Print the contents of test.

cp

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
Exercise

  • Copy test to mysecond.

mv

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
Exercise

  • 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
Exercise

  • 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?

echo

Gives the argument to standard output.

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

For more information,

$ man echo
Exercise

  • 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  

grep

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
Exercise

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

tr

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
Exercise

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

cut

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
Exercise

  • 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

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
Exercise

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

man

Displays a manual page for the command.

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

For more information,

$ man man

pwd

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

For more information,

$ man pwd

clear

  • 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

ssh

Used for logging into a remote machine.

  • ssh sample.ssh.com - connects to a remote computer called sample.ssh.com.
  • ssh [email protected] - can be used to log in as a different user.
  • ssh [email protected] -p port - can be used to specify the port to connect to.

For more information,

$ man ssh

scp

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

Note

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

env

Prints the defined environment variables.

xxd

Prints out the hex dump of the given file.

Usage

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

For more information,

$ man xxd

strings

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

Usage

$ strings file.txt

For more information,

$ man strings