Saturday, July 5, 2014

Unix Shell: Find Files(5)

Principal of "find" command treatment of symbolic link:
-P option: never follow the symbolic link, this is the default option.
-H option: normally doesn't follow the symbolic link, except when there is one command argument to specify the file name pattern.
-L option: follow the symbolic link all the time.

1. Treatment of symbolic link -- Print out files information

-P, -H, -L must be placed in front of searching files

When printing out information of files:
1) List all files. We have one file: o1, whose modification time is 2001/05/30. Another soft link: sl_o1 who is pointing to o1, whose modification time is 2014/07/05.
2) By default, find "add -P option between find and *", which means, it will never follow the symbolic link. When printing out the information of o1 and sl_o1, it will just use its original information.
3) -H option, since we specify the command argument "*" to find files whose name matches. So it will follow the link to use information sl_o1 points to. In this case, the modification time of sl_o1 become 2001/05/30, which is the o1's information.
4) -H option, since we don't specify the command argument "*" to find files whose name matches. So it won't follow the link to get the file information.In this case, sl_o1's time is just the link itself's time.
5) -H option, although we specify the command argument ".", but it is not used to represent the file name pattern, instead, it is used to represent the starting path. In this case, -H won't follow the link. So in this case, sl_o1's time is just the link itself's time.
6) -H option, we don't use command argument to specify the file name pattern, instead we use the -name option to specify the name pattern. So in this case, as we mentioned above, we don't use command argument for file name pattern, so it won't follow the link. So in this case, sl_o1's time is just link itself's time.
7 - 9) -L option, no matter what it will follow the symbolic link, sl_o1's time is the time of o1.

aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt
total 0
-rw-rw-r-- 1 aubinxia aubinxia 0 May 30  2001 o1
lrwxrwxrwx 1 aubinxia aubinxia 2 Jul  6 09:38 sl_o1 -> o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find * -ls
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 Jul  6 09:42 o1
134338    0 lrwxrwxrwx   1 aubinxia aubinxia        2 Jul  6 09:38 sl_o1 -> o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H * -ls
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 May 30  2001 o1
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 May 30  2001 sl_o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H -ls
154684    4 drwxrwxr-x   2 aubinxia aubinxia     4096 Jul  6 09:38 .
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 May 30  2001 ./o1
134338    0 lrwxrwxrwx   1 aubinxia aubinxia        2 Jul  6 09:38 ./sl_o1 -> o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H . -ls
154684    4 drwxrwxr-x   2 aubinxia aubinxia     4096 Jul  6 09:38 .
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 May 30  2001 ./o1
134338    0 lrwxrwxrwx   1 aubinxia aubinxia        2 Jul  6 09:38 ./sl_o1 -> o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H -ls -name .*1
154684    4 drwxrwxr-x   2 aubinxia aubinxia     4096 Jul  6 09:38 .
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 May 30  2001 ./o1
134338    0 lrwxrwxrwx   1 aubinxia aubinxia        2 Jul  6 09:38 ./sl_o1 -> o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L * -ls
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 May 30  2001 o1
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 May 30  2001 sl_o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L -ls
154684    4 drwxrwxr-x   2 aubinxia aubinxia     4096 Jul  6 09:38 .
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 May 30  2001 ./o1
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 May 30  2001 ./sl_o1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L -ls -name .*1
154684    4 drwxrwxr-x   2 aubinxia aubinxia     4096 Jul  6 09:38 .
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 May 30  2001 ./o1
134334    0 -rw-rw-r--   1 aubinxia aubinxia        0 May 30  2001 ./sl_o1

2. Treatment of symbolic link -- Print out broken link information
When printing out "broken symbolic link" information.
1) List all files, we have one symbolic link pointing to file o1, whose modification time is 2001.
2) Print out sl_o1's file content.  we find that it is a broken link, since original file o1 is not existed.
3 - 5) No matter what kind of option we use, it won't follow the link. It will just show the link itself's time, since the link is broken, there is no way to resolve it.

 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 0  
 lrwxrwxrwx 1 aubinxia aubinxia 2 Jul 5 19:45 sl_o1 -> o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat sl_o1  
 cat: sl_o1: No such file or directory  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find * -ls  
 134448  0 lrwxrwxrwx  1 aubinxia aubinxia    2 Jul 5 19:45 sl_o1 -> o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H * -ls  
 134448  0 lrwxrwxrwx  1 aubinxia aubinxia    2 Jul 5 19:45 sl_o1 -> o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L * -ls  
 134448  0 lrwxrwxrwx  1 aubinxia aubinxia    2 Jul 5 19:45 sl_o1 -> o1  

3. Treatment of symbolic link -- Print out link to the directory information

When using "find" command to find symbolic links who point to the directory.
1 - 2) Create the file structure
3) Find all files with default -P option, it will just list the symbolic link in local directory, and recursively dig into directory dir, list all files there. It doesn't follow the symbolic link.
4) -H option without command argument, it won't follow the link.
5) -H option with the command argument, it will follow the link and recursively find the files there.
6 - 7) -L option, no matter what it will follow the symbolic link.
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt
total 4
drwxrwxr-x 2 aubinxia aubinxia 4096 Oct  1  2001 dir
lrwxrwxrwx 1 aubinxia aubinxia    3 Jul  6 10:15 sl_dir -> dir
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch ./dir/d1 ./dir/d2
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find 
.
./dir
./dir/d2
./dir/d1
./sl_dir
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H
.
./dir
./dir/d2
./dir/d1
./sl_dir
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -H *
dir
dir/d2
dir/d1
sl_dir
sl_dir/d2
sl_dir/d1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L
.
./dir
./dir/d2
./dir/d1
./sl_dir
./sl_dir/d2
./sl_dir/d1
aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find -L *
dir
dir/d2
dir/d1
sl_dir
sl_dir/d2
sl_dir/d1

No comments:

Post a Comment