Sunday, July 6, 2014

Unix Shell: "find" example

1. Find Files and print out their content.
script_1:
Use "find" command to list all files whose name starting with o and ending up with one number, and the file type must be ordinary file.
 #! /bin/bash  
   
 find . -name "o[0-9]" -type f |  
  while read file   
  do  
    cat $file  
  done  

terminal:
1 - 4) Create 3 files, including one soft link and two ordinary files
5) Run the script, it will just print out the 2 ordinary files' content.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello Los Angeles!" > o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo "Hello New York!" > o2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ln -s o1 sl_o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt  
 total 8  
 -rw-rw-r-- 1 aubinxia aubinxia 19 Jul 6 15:28 o1  
 -rw-rw-r-- 1 aubinxia aubinxia 16 Jul 6 15:28 o2  
 lrwxrwxrwx 1 aubinxia aubinxia 2 Jul 6 15:28 sl_o1 -> o1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1  
 Hello Los Angeles!  
 Hello New York!  

2. Find files and save results into temporary files
script_1:
 #! /bin/bash -  
   
 #new line, space, and tab  
 #In this way, input will be separated by newline firstly  
 IFS='  
     '  
   
 PATH=/usr/local/bin:/bin:/usr/bin  
 export PATH  
   
 #Check for the input  
 if [ $# -ne 1 ]  
 then  
   echo "Usage: $0 directory" >&2  
   exit 1  
 fi  
   
 #file created in this process, has permission:  
 #rwx------, only creator has the full permission  
 umask 077   
   
 TMP=${TMPDIR:-/tmp}  
   
 TMPFILES="$TMP/files.last31.$$  
 $TMP/files.last14.$$  
 $TMP/files.last07.$$"  
   
 #Specify the working directory, enter that directory  
 #If that directory doesn't exist or it is a broken  
 #symbolic link, then exit the script  
 WD=$1  
 cd $WD || exit 1  
   
 trap 'exit 1' HUP INT PIPE QUIT TERM  
 trap 'rm -f $TMPFILES' EXIT  
   
 #We try to find all files who are modified in less than  
 #31 days, then print the result into files.last31.$$  
 #Next, in current result set, pick up all files who are  
 #modified in less than 14 days, and print out these files  
 #into files.last14.$$. Finally, in current result set,  
 #pick up all files who are modified in less than 7 days,  
 #and print the list into files.last07.$$  
 find -mtime -31 -fprint $TMP/files.last31.$$ \  
 -a -mtime -14 -fprint $TMP/files.last14.$$ \  
 -a -mtime -7 -fprint $TMP/files.last07.$$  
   
 for i in $TMPFILES  
 do  
   #Replace the "."(dot) with current directory  
   #sort, and write output to another file $i.tmp  
   sed -e "s=^[.]/=$WD/=" -e "s=^[.]$=$WD=" $i \  
     | sort >$i.tmp  
     
   #Compare the $i.tmp and $i byte by byte, if  
   #there is any difference(return code is not 0),  
   #it will trigger next part of code, replace $i  
   #with $i.tmp  
   cmp -s $i.tmp $i || mv $i.tmp $i  
   
   cat $i  
   echo ==================================  
 done  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 dir  
 dir  
 dir/o1  
 dir/o2  
 dir/o3  
 ==================================  
 dir  
 dir/o1  
 dir/o2  
 ==================================  
 dir  
 dir/o1  
 ==================================  

No comments:

Post a Comment