<<

LAB – 3 Some commands

Linux Commands Note: • Spend some 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 contents to be sent to the screen one page 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]

: is used to output first part of the files. $ head

: 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 e.g. whereis will give you the following output: ls: /bin/ls /usr/share/man/man1/ls.1.gz

: is used to locate executables in the system. $ which e.g. which ls gives: /bin/ls

[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 e.g. whatis gives: grep (1) - lines matching a pattern • : searches for files in the given directory, hierarchically starting at the parent directory and moving to subdirectories. $ find ­name , here -name makes the search case sensitive.

[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

: is used to 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]

: is used to two or more plain files and/or print contents of a file on standard output. $ cat

[Exercise: Try cat >> . Find out the difference between using > and >>]

• grep: searches the given input file for lines containing a match to the given or words. $ grep e.g. grep this one.txt searches for the word 'this' in file one.txt.

[Exercise: Try different options (-i, -x, -, -n) and identify the difference. Find out how to search for a word in all the files in a directory]

: is used to the sort the lines of text files. $ sort [Exercise: Try sort, sort -r and sort -n and identify the differences]

: compares two files of any 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 ) and identify the difference using '-b' option]

: reports or omits repeated lines. $ uniq

[Exercise: Try options -c, -d, -i, -u]

: 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]

: stands for Tape ARchive, is a utility to archive and extract files in a number of file formats. $ tar . . . . Refer to these link: https://users.cs.duke.edu/~ola/courses/programming/tar.html and http://www.cyberciti.biz/faq/tar-extract-linux/ to know more about creating and extracting archives using tar.

[Exercise: Try to create an archive using two or more files, display the table of contents in an archive and extract an archive]

: is used to change file permissions of each given file, folder, script, etc. $ chmod e.g. chmod 664 one.txt gives read and permissions to user and group, and gives only read permission to others for one.txt file.

[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

: reports disk usages of file system.

: is used to send a signal to a process. To stop a process, a pid (process id) which can be obtained from 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 [Exercise: Try kill ­l, kill ­9 pid, kill ­15 pid. Also, identify the difference between kill and pkill.]

: prints detailed information about the machine name, OS and kernel. $ uname ­a

: displays a line of text on the standard output. This command is commonly used in interactive 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 ­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.