Wednesday, July 9, 2014

Unix Shell: Find Problematic Files

1. find -print0
-print 0 option allow find command to output the file name in a null terminated string

terminal:
1) We create the 2 files: oa and ob
2) Use -print0 option to print out all result in a null-terminated string, in this case, string is: "../ob./oa" without new line in the end.
3) Use od -ab to convert the string into octal expression, we can see what is inside the string.
"../ob./oa" is actually "dot null dot slash o b null dot /slash o a null", different results are separated by a "null"
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ touch oa ob  
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ find -print0  
 ../ob./oaaubinxia@aubinxia-fastdev:~/Desktop/xxtest$ find -print0 | od -ab  
 0000000  . nul  .  /  o  b nul  .  /  o  a nul  
     056 000 056 057 157 142 000 056 057 157 141 000  
 0000014  

2. Find Problematic Files
Unix allow files to have "." (dot), space or newline, so it is tough to read such file names with normal command, we can use -print0 option and convert it to a readable format by "od" command(octal expression) or "tr" command.
1 - 2) Use touch command to create 2 files: one's name is "space dot" another one's name is "space dot space new line"
3) Use find command to list all files, it is very tough to read these names
4) Use find -print0 option to convert all results into one null-terminated string, still tough to read
5) Port the result into "od" command to convert the string to an octal expression: so the string is: dot null(first file), dot slash space dot null(second file), dot slash space dot space newline null(3rd file)
6) Port the result into "tr" command to convert into a more readable name. In this case, tr command is converting space to S, newline to n, and null to newline.
 Then we can see 3 results in different lines.

 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ touch " ."  
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ touch " .   
 > "  
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ find  
 .  
 ./ .  
 ./ . ?  
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ find -print0  
 ../ ../ .   
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ find -print0 | od -ab  
 0000000  . nul  .  / sp  . nul  .  / sp  . sp nl nul  
     056 000 056 057 040 056 000 056 057 040 056 040 012 000  
 0000016  
 aubinxia@aubinxia-fastdev:~/Desktop/xxtest$ find -print0 | tr " \n\0" "SN\n"  
 .  
 ./S.  
 ./S.SN  

No comments:

Post a Comment