File Filters and Concatenation Commands
Allowing users to send output of one command to the input of another; searching and manipulating files
Commands used to search and manipulate file contents are referred to as filters. Here, we discuss the basic grep command and its variants. The more advanced sed and awk commands will be covered in another chapter.
The cat command allows users to view files without invoking an editor. The cat command may be provided with many file names as arguments; it then appends the filenames to one another in the order specified. The following command appends the contents of errors.txt to errors and displays the output on the screen:
[ LinuxUser ] ~$ cat errors errors.txt
ls: cannot access sdfjdfjsdjf: No such file or directory
ls: cannot access sdfhesur3: No such file or directory
The most useful minus option for the cat command is –n where each line of the output of cat -n is numbered. This is useful when someone wants to view the contents of a certain line of a long file. In such a case, the more or less command may be piped to receive the cat command's output so that the user could scroll through the results:
[ LinuxUser ] ~$ cat -n errors errors.txt 1 ls: cannot access sdfjdfjsdjf: No such file or directory 2 ls: cannot access sdfhesur3: No such file or directory [ LinuxUser ] ~$ cat -n lsinfo.txt | more 1 LS(1) User Commands LS(1) 2 3 4 5 NAME 6 ls - list directory contents 7 8 SYNOPSIS 9 ls [OPTION]... [FILE]... 10 11 DESCRIPTION 12 List information about the FILEs (the current directory by default). 13 Sort entries alphabetically if none of -cftuvSUX nor --sort. 14 15 Mandatory arguments to long options are mandatory for short o ptions 16 too. 17 18 -a, --all 19 do not ignore entries starting with . 20 --More--
The grep, fgrep and egrep commands return lines that match a given pattern. Using the grep commands on files and directories without read access to the same will result in error messages; the -s option may be used to suppress such error messages. The grep command returns a set of lines that contain a match to the given pattern. The syntax of grep commands is 'grep pattern files_or_path
[ LinuxUser ] ~$ grep -s 'Author: Maria Callas' /lost+found/*
The above command demonstrates how grep may be used practically. A user may have lost her files due to a system crash or some other problem; she may search the lost+found directory for files authored by her. To avoid permission related error messages (many of the files here may belong to other users) she uses the -s option. She also uses the '*' wildcard to search all the files in the lost+found directory.
[ LinuxUser ] ~$ history | grep rmdir
This example demonstrates how the output of the history command (which may get pretty large) may be filtered for just the remove directory commands
The grep pattern may use regular expressions. Regular expressions offer a lot more flexibility in specifying patterns than wildcards. While wildcards at best approximate a pattern, a regular expression allows for an accurate specification of patterns. We will learn more about regular expressions later in the tutorial. The fgrep command may also be invoked as grep -F and is a version of grep that does not allow regular expressions in the pattern. The fgrep command takes up less time especially when the search encompasses a significant portion of the Filesystem. The egrep command offers an even broader support for regular expressions (conditional statements using '|' and so on) than the grep command. The following command searches for a user with id JohnH or johnh in the '/etc/passwd' file.
[ LinuxUser ] ~$ egrep 'JohnH|johnh' /etc/passwd
The make utility
The make utility may be used to store lists of configuration commands and compilation commands that should be run in order to get an application or utility installed and ready for use. Instead of issuing the long set of commands over and over again, a makefile is created for the process. All the commands are entered into the makefile. Afterwards, issuing just the command 'make' executes the makefile.
The make utility automatically determines which pieces of a large program need to be recompiled and issues only the commands to recompile these pieces. The makefile should describe the relationships among files in the program, and the commands to update each file. The make program uses the makefile database and the modification times of the files to decide which of its files should be recompiled and rebuilt. Make may either be used with the '-f' option - this allows the user to specify a custom name for a makefile. Just running make causes the system to search for a file named 'GNUmakefile', 'makefile' or 'MakeFile' in that order.