Sunday, July 6, 2014

Unix Shell: Find Files(6)

1. Treatment of symbolic link -- Find files with specific type

When using find command to list files with specific types
1) List all files and indicates that we have one ordinary file o1, and one symbolic link pointing to o1.
2 - 3) find all files using the default -P option, never follow the link. So in this case, o1 is taken as ordinary file, sl_o1 is taken as link.
4 - 5) -H option without command argument, it won't follow the link to extract file type information. So in this case, o1 is taken as ordinary file, sl_o1 is taken as link.
6 - 7) -H option with command argument, it will follow the link. So in this case, both o1 and sl_o1 are taken as ordinary files.
8 - 11) -L option will follow the link all the time. In this case, both o1 and sl_o1 are taken as ordinary files.

 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$  ls -lrt
total 0
-rw-rw-r-- 1 aubinxia aubinxia 0 Jul  6 10:33 o1
lrwxrwxrwx 1 aubinxia aubinxia 2 Jul  6 10:33 sl_o1 -> o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -type f
./o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -type l
./sl_o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H -type f
./o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H -type l
./sl_o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H * -type l
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H * -type f
o1
sl_o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L -type f
./o1
./sl_o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L -type l
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L * -type l
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L * -type f
o1
sl_o1

2. Treatment of symbolic link -- find files with newer
 newer option will let find command list files whose modification time is later than the given file's.

1) List all files. We have one ordinary file o1, modified at 2001, ordinary file o2, modified at 2008, and symbolic link sl_o1, modified at 2014.
2) With default -P option, it will never follow the link, -newer option will just list files whose modification time is later than sl_o1 link itself's modification time, which is nothing in this case.
3 - 4) -H option will follow the link with -newer option.
5 - 6) -L option will follow the link all the time.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 0  
 -rw-rw-r-- 1 aubinxia aubinxia 0 Jan 1 2001 o1  
 -rw-rw-r-- 1 aubinxia aubinxia 0 Jan 1 2008 o2  
 lrwxrwxrwx 1 aubinxia aubinxia 2 Jul 6 11:00 sl_o1 -> o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -newer sl_o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H -newer sl_o1  
 .  
 ./sl_o1  
 ./o2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H * -newer sl_o1  
 o2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L -newer sl_o1  
 .  
 ./o2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L * -newer sl_o1  
 o2  

3. Combination of different conditions
-a means "AND"
-o means "OR"
1)List all files, we have 2 ordinary files o1 and o2, and one symbolic link sl_o1
2) List all files whose type is soft link, only sl_o1 get selected
3) List all files whose type is soft link, at this point of time, only sl_o1 get selected. Next step, and size is equal to 3 bytes, at this point of time, sl_o1 is not qualified, and the result set becomes empty.
4) Following point 3, we have one empty result set. Next step, "or" the file type is ordinary file, at this point of time, find command will scan through all files again to find all files who are ordinary files
5) Following point 4, we have o1 and o2 in result set. Next step, "and" the file size is larger than 3 bytes. At this point of time, find command will scan through the files in result set to locate qualified files, finally only o2 get picked.

Summary: -a will trigger find command to filter files in existing result set, -o will trigger find command to scan all files set again.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 4  
 -rw-rw-r-- 1 aubinxia aubinxia 0 Jan 1 2001 o1  
 -rw-rw-r-- 1 aubinxia aubinxia 13 Jan 1 2008 o2  
 lrwxrwxrwx 1 aubinxia aubinxia 2 Jul 6 11:00 sl_o1 -> o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -type l  
 ./sl_o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -type l -a -size +3c  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -type l -a -size +3c -o -type f  
 ./o1  
 ./o2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -type l -a -size +3c -o -type f -a -size +3c  
 ./o2  

4. -fprint option
find command could use -fprint option to print out the finding result to a file
terminal:
1) List all files in current directory
2)  A complicated command: list all files whose type is directory, at this point of time, print out the finding result into the od, so od only consists of directory entries. Next step, go through all files again to look for ordinary files, at this point of time, print out the result into "of", so "of" consists of both directory entities and ordinary file entities. Lastly, it will go through existing result set to locate files who is a soft link, and at this point of time, print out the result into ol. Since there is no soft link in existing result set, ol is empty.
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ find -ls  
 264219  4 drwxrwxr-x  3 aubinxia aubinxia   4096 Jul 7 20:49 .  
 920783  4 drwxrwxr-x  2 aubinxia aubinxia   4096 Jul 7 20:48 ./dir  
 920785  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 7 20:48 ./dir/d2  
 920784  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 7 20:48 ./dir/d1  
 264220  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 7 20:48 ./o1  
 264222  0 lrwxrwxrwx  1 aubinxia aubinxia    2 Jul 7 20:48 ./sl_o1 -> o1  
 265481  4 -rw-rw-r--  1 aubinxia aubinxia    8 Jul 7 20:57 ./od  
 264221  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 7 20:48 ./o2  
 265480  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 7 20:50 ./of  
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ find -type d -fprint od \  
 > -o -type f -fprint of \  
 > -a -type l -fprint ol  
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ cat od  
 .  
 ./dir  
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ cat of  
 ./dir/d2  
 ./dir/d1  
 ./o1  
 ./ol  
 ./od  
 ./o2  
 ./of  
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ cat ol  
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$   

-fprint could only write the file names, it is not able to record other information.
terminal:
Although -ls option could help list more detailed information, but ols only contain the file names.
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ find * -ls -fprint ols  
 920783  4 drwxrwxr-x  2 aubinxia aubinxia   4096 Jul 7 20:48 dir  
 920785  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 7 20:48 dir/d2  
 920784  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 7 20:48 dir/d1  
 264220  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 7 20:48 o1  
 264221  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 7 20:48 o2  
 265481  4 -rw-rw-r--  1 aubinxia aubinxia    8 Jul 7 20:59 od  
 265480  4 -rw-rw-r--  1 aubinxia aubinxia    43 Jul 7 20:59 of  
 265482  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 7 20:59 ol  
 265491  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 7 21:09 ols  
 264222  0 lrwxrwxrwx  1 aubinxia aubinxia    2 Jul 7 20:48 sl_o1 -> o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ cat ols  
 dir  
 dir/d2  
 dir/d1  
 o1  
 o2  
 od  
 of  
 ol  
 ols  
 sl_o1  

No comments:

Post a Comment