Chpater-7.4 : Unix/Linux Philosophy, Pipes, and File Searching Techniques
Pipes :
The Unix/Linux philosophy is to have many simple and short programs or commands corporate together to produce quite complex results rather than have one complex program with many possible options and modes of operation, in order to accomplish this extensive use of pipes is made. You can pipe the output of one commander program into another as its input, in order to do this we use the vertical ber pipe symbol between commans as given in the example below:
command 1 | command 2 | command 3
This represents what we often call a pipeline and allows linux to combine the actions of several commands into one. This is extraordinarly efficient because command 2 and command 3 do not have to wait for previous pipeline command to complete.
Searching For Files :
Being able to quickly find the files you are looking for will save you time and enhance productivity. You can search for files in both your home directory space or in any other directory or syatem or location on the system. The main tools for doing this are the locate and find utilities. I will also show how to use wild cards and bash in order to specify any file which matches a given generalized request.
locate :
The locate utility program performs a search taking advantage of a previously constructed database of files and directories on your system matching entries that contain a specified character string, this can sometimes result in a very long list to get a shorter and possibly more relevent list we can use grep as a filter. Grep will print only the lines that contain one or more specified string as in this example which will list all the files and directories with both zip and bin in their name.
locate zip \ grep bin
Notice the use of the vertical line or pipe to pipe the two commands together.
EXAMPLES :
So suppose I want to find all files that have the string dog in them :
locate dog
another way to do this is :
find . -name "dog"
If you make a new file then the locate command will not be able to find it, for locate to find the new file you just created you must update the database. To update the database:
sudo updatedb
Wild Cards :
You can search for a file name containing specific characters using wild cards, to search for files using the question mark wildcard replace each unknown character with question mark for example - if you know only the first two letters "ba" of a three letter filename without an extension you can type this :
ls bs? .out
Wildcard | Result |
? | Matches any single character |
* | Matches any string of characters |
[set] | Matches any character in the set of characters, for example [adf] will match any occurance of a, d, or f. |
[!set] | Matches any character not in the set of characters. |
To search for a file using the wildcard replace the unknown string with asterick for example if you remember only that the extension was .out, type:
ls * . out
Suppose you want to check the disk usage by a given directory or subdirectory :
du -sh a *
Basically the above command will show the files and it's disk usage which has "a" in it.
du -sh a* log *
The above command will show the files and it's disk usage which has "a" and "log" in it.
Suppose I want to look at ones which have a certain character in them :
du * . ? . *