Search and Sort Tools:Using find.

Using find

find is used when a user, or a system administrator, needs to determine the location of a certain file under a specified subtree in a file hierarchy. The syntax and use of find is: find path expression action

where path identifies the subtree, expression helps to identify the file and the action specifies the action one wishes to take. Let us now see a few typical usages.

  • List all the files in the current directory.

bhatt@SE-0 [F] >>find . -print /* only path and action specified */

.

/ReadMe

./testfile

  • Finding files which have been created after a certain other file was created.

bhatt@SE-0 [F] >>find . -newer testfile

.

/ReadMe

There is an option mtime to find modified files in a certain period over a number of days.

  • List all the files which match the partial pattern test. One should use only shell meta-characters for partial matches.

bhatt@SE-0 [F] >>find . -name test*
./testfile
bhatt@SE-0 [F] >>find ../../ -name test*
../../COURSES/OS/PROCESS/testfile
../../UPE/F/testfile

  • I have a file called linkedfile with a link to testfile. The find command can be used to find the links.

bhatt@SE-0 [F] >>find ./ -links 2

./

./testfile

./linkedfile

  • Finding out the subdirectories under a directory.

bhatt@SE-0 [F] >>find ../M -type d

../M

../M/M1

../M/M2

../M/M3

../M/RAND

  • Finding files owned by a certain user.

bhatt@SE-0 [F] >>find /home/georg/ASM-WB -user georg -type d

/home/georg/ASM-WB

/home/georg/ASM-WB/examples

/home/georg/ASM-WB/examples/Library

/home/georg/ASM-WB/examples/SimpleLang

/home/georg/ASM-WB/examples/InstructionSet

/home/georg/ASM-WB/Projects

/home/georg/ASM-WB/FirstTry

The file type options are: f for text file, c for character special file, b for blocked files and p for pipes.

Strings: Sometimes one may need to examine if there are ascii strings in a certain object or a binary file. This can be done using a string command with the syntax : string binaryfileName | more

As an example see its use below2:

bhatt@SE-0 [F] >>strings ../M/RAND/main | more The value of seed is %d at A

The value is %f at B

ctags and etags: These commands are useful in the context when one wishes to look

up patterns like c function calls. You may look up man pages for its description if you are a power user of c.

Sort Tool

For sort tool Unix treats each line in a text file as data. In other words, it basically sorts lines of text in text file. It is possible to give an option to list lines in ascending or descending order. We shall demonstrate its usage through examples given below.

clip_image001

bhatt@SE-0 [F] >>sort
aaa
bbb
aba
^d terminates the input ....see the output below
aaa
aba
bbb
bhatt@SE-0 [F] >>sort testfile

10000001
456
This is a test file.
a1a1a1
aaa

(Use -r option for descending order).

bhatt@SE-0 [F] >>sort testfile -o outfile
bhatt@SE-0 [F] >>

One can see the outfile for sorted output. sort repeats all identical lines. It helps to use a filter uniq to get sorted output with unique lines. Let us now modify our testfile to have repetition of a few lines and then use uniq as shown below.

bhatt@SE-0 [F] >>sort testfile|uniq|more
10000001
456
This is a test file.
a1a1a1
aaa

In table 11.4 we list the options that are available with sort. sort can also be used to merge files. Next, we will split a file and then show the use of merge. Of course, the usage is in the context of merge-sort.

One often uses filtering commands like sort, grep etc. in conjunction with wc, more, head and tail commands available in Unix. System administrators use who in conjunction with grep, sort, find to track of terminal usage and also for lost or damaged files.

split: split command helps one to split a file into smaller sized segments. For instance, if we split ReadMe file with the following command :

split -l 20 ReadMe seg

Upon execution we get a set of files segaa, segab, ....etc. each with 20 lines in it. (Check the line count using wc). Now merge using sorted segaa with segab.

sort -m segaa segab > check

A clever way to merge all the split files is to use cat as shown below:

cat seg* > check

The file check should have 40 lines in it. Clearly, split and merge would be useful to support merge-sort and for assembling a set of smaller files that can be sent over a network using e-mail whenever there are restrictions on the size of attached files.

In the next module we shall learn about the AWK tool in Unix. Evolution of AWK is a very good illustration of how more powerful tools can be built. AWK evolves from the (seemingly modest) generic tool grep!!

Comments

Popular posts from this blog

Input Output (IO) Management:HW/SW Interface and Management of Buffers.

Introduction to Operating Systems:Early History: The 1940s and 1950s

Input Output (IO) Management:IO Organization.