LAB – 3 Some more linux commands
Linux Commands Note: • Spend some time on man pages to know more about the commands, like their general syntax, how to modify their behavior, etc. • Before trying the following commands, ◦ create a folder lab_3, ◦ in this folder create some text files with random text (e.g. This is file1) and ◦ download one or two sample text files from http://www.textfiles.com/directory.html. ◦ Preferably, try to do the first two steps using terminal only.
Accessing Files, Commands
• more: allows file contents to be sent to the screen one page at a time. $ more
• less: is similar to more command, but has more features. A user can page up and down through the file. $ less
[Exercise: Try using more and less on a big sample text file and identify the difference]
• head: is used to output first part of the files. $ head
• tail: is used to output last part of the files. $ tail
[Exercise: Use head/tail to output first/last k lines instead of default 10 lines]
• whereis: is used to locate the binary, source and manual pages of any command. $ whereis
• which: is used to locate executables in the system. $ which
[Exercise: Read about executables in linux and how is it different from windows]
• whatis: is used to display one-line description from manual pages. $ whatis
[Exercise: Find out how to search for all the files in a directory with a given (e.g. .txt) extension]
• history: prints the history of long list of executed commands in the terminal. $ history
Altering Files and Operations on Files
• cp: is used to copy files and directories. It creates a new file if the destination file doesn't exist, and overwrites the destination file if it already exists. $ cp
[Exercise: Find how to copy all the files, subdirectories in a directory to new a directory]
• cat: is used to join two or more plain files and/or print contents of a file on standard output. $ cat
[Exercise: Try cat
• grep: searches the given input file for lines containing a match to the given strings or words. $ grep
[Exercise: Try different options (-i, -x, -c, -n) and identify the difference. Find out how to search for a word in all the files in a directory]
• sort: is used to the sort the lines of text files. $ sort
• cmp: compares two files of any type byte by byte and writes the results to the standard output. It outputs the byte where files differ. $ cmp
[Exercise: Try using SKIP bytes (refer to man page) and identify the difference using '-b' option]
• uniq: reports or omits repeated lines. $ uniq
[Exercise: Try options -c, -d, -i, -u]
• touch: updates the access and modification times of given file to the current time. It also creates the file in case the given file doesn't exist. $ touch
[Exercise: Use ls -l before and after using touch command on a file, and check the difference between access times]
• tar: stands for Tape ARchive, is a utility to archive and extract files in a number of file formats. $ tar
[Exercise: Try to create an archive using two or more files, display the table of contents in an archive and extract an archive]
• chmod: is used to change file permissions of each given file, folder, script, etc. $ chmod
[Exercise: Try to change permissions of all the files in a directory] Miscellaneous
• lsblk: prints block devices by their assigned name. By default, ram is not listed (Try using option '-a'). $ lsblk
• df: reports disk usages of file system.
• kill: is used to send a signal to a process. To stop a process, a pid (process id) which can be obtained from ps or top and a signal number (optional) is required. Two signals in particular SIGTERM (9) and SIGKILL (15) are used to stop a process. The default signal is SIGTERM. $ kill
• uname: prints detailed information about the machine name, OS and kernel. $ uname a
• echo: displays a line of text on the standard output. This command is commonly used in interactive shell scripting. Piping and Redirection
'|' - Pipe operation is a mechanism for sending data/output from one program/command to another. The operator '|' feeds the output from the command on the left as input to the command on the right. e.g. In the below example output from ls is given to head which outputs first 3 lines of the input.
We can pipe as many commands as we like:
Some more examples: • ls l | grep "Aug" outputs only list of files which were accessed or modified in August. • ls l | sort nk5 outputs the sorted list of files by their size. • cat one.txt | grep the searches for the word 'the' in the file one.txt.
Redirection to files is performed using '>' - save output to a file, '<' - read input from a file, '>>' - append to a file.
Using '>' outputs data to a file. If the file exists, it clears the file first and then saves the data. If the file doesn't exist, it creates a file first and then writes the data. e.g. ls > output1 writes the output of ls to file output1. Using '>>', we can append data to the existing file, instead of overwriting it. Below, we can see that '>>' appends the output of wc l 221baker.txt at the end of the file output1.
Similarly, '<' is used to read from a file. e.g. wc l < output1 outputs the number of lines in the file output1.
References: • Images on page 2 and 4 from tecmint.com.