Cracking IT Interview



Finding files Continues..

We are going to continue with some more options, practices and question-answers with find commands.


Some other options with find command:


  • "-not" : this is used to negate or inverse the given conditions / options with the command.


$ls -ltr
total 16
-rwxrwxrwx  1 pinku  guest    0 May 15  2015 myfile2.txt
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
-rwxr--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
-rwxr--r--  1 pinku  guest    3 Dec 24 02:53 file2.txt
-rw-r--r--  1 pinku  guest   32 May 10 14:14 file3.txt
$
$find . -not -name A1
.
./file3.txt
./myfile2.txt
./file1.txt
./A1/A2
./file2.txt
$







here, it displays all the files except directory "A1". This is also can be done by the "!" bang(exclamation) key:


$find . ! -name A1
.
./file3.txt
./myfile2.txt
./file1.txt
./A1/A2
./file2.txt
$


  • "-delete" : this is used to delete the file found in the file search. Let's take the above listing out of files and perform with this option:


$find . -name myfile2.txt -delete
$
$ls -ltr
total 16
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
-rwxr--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
-rwxr--r--  1 pinku  guest    3 Dec 24 02:53 file2.txt
-rw-r--r--  1 pinku  guest   32 May 10 14:14 file3.txt
$

 

  • "-exec" : with this option, we can execute some other command passed as an argument preceded by "-exec" option. It returns true for the zero exit status and the expression is terminated by the semicolon ";" which is little different against find command tradition.


For the above listing out of files, we are searching for the file which is created within 2 days and removing it (which is "file3.txt" ):


$find . -type f -mtime -2 -exec rm {} \;
$ls -ltr
total 12
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
-rwxr--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
-rwxr--r--  1 pinku  guest    3 Dec 24 02:53 file2.txt
$


Just remember to give the white space before and after "{}" remembered pattern, end the command with "\;"


  • ​"-ok" : this option is same as "-exec" option but it confirms with the user for taking the action on the found files by the command. It will ask user with yes "y" or no "n" option and takes the action accordingly.


Taking the above file listing: 


$find . -name file2.txt -ok rm {} \;
"rm ./file2.txt"? y
$
$find . -name file1.txt -ok rm {} \;
"rm ./file1.txt"? n
$
$ls -ltr
total 8
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
-rwxr--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
$








  • "-newer" : This option gives you the ability to find all the files which are created or modified newer than given file name in the command.


$ls -ltr
total 8
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
-rwxr--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
$
$cat > newfile2.txt
$D
$ls -ltr
total 8
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
-rwxr--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
-rw-r--r--  1 pinku  guest    0 May 11 04:34 newfile2.txt
$
$find . -type f -newer file1.txt
./newfile2.txt
$


  • "-maxdepth" : With this option file is going to be searched in down level directories. 


In the below example, directory structure is like A_dir -> A1 -> A2, so when we give the maxdepth value 3 means, it will search the file in current directory i.e A_dir (value 1), then one level down A1 (value 2) then finally another level down directory A2 (value 3): 


$ls -ltr
total 8
drwxr--r--  3 pinku  guest  512 Dec 24 02:48 A1
-rwxr--r--  1 pinku  guest    3 Dec 24 02:52 file1.txt
-rw-r--r--  1 pinku  guest    0 May 11 04:34 newfile2.txt
$
$cd A1
$ls -l
total 4
drwxr--r--  2 pinku  guest  512 May 11 12:28 A2
$
$cd A2
$ls -l
total 4
-rw-r--r--  1 pinku  guest  35 May 11 12:28 File3.txt
$
$cd ../../

$pwd
/home/pinku/my_dir/A_dir

$
$find . -type f -maxdepth 3
./newfile2.txt
./file1.txt
./A1/A2/File3.txt
$


  • "-mindepth" : with this option it will search the file in up level directories.


For the same above directory structure A_dir -> A1 -> A2, suppose we have to search file only in directory level 3 which id directory A2. It does not apply any action on directories level less than 3, that mean applies action only on directory A2, given below:


$find . -type f -maxdepth 3 -mindepth 3
./A1/A2/File3.txt
$


  • "-empty" : this option will search the empty file and directories as the name implies.


$pwd
/home/pinku/my_dir/A_dir
$
$find . -type f -empty
./newfile2.txt
$
$find . -empty
./newfile2.txt
./A2
$
$find . -type d -empty
./A2
$


  • "-user" : It will list out all the files by the given user name with this option.


$find . -user pinku
.
./newfile2.txt
./A2
./file1.txt
./A1
./A1/A2
./A1/A2/File3.txt
$







Some more commands for practice:


!find


- This will execute the last find command given by you.


find . -iname FILE1.txt


- ignore case, just to avoid uppercase / lowercase letters with the file name, remember "-i" option independently will not work here.


find . -type f -name "*.txt"


To find all the text files only in current directory.


find / -type f -size 0


To find all the zero byte files in whole directory structure


find . -not -type d -name "*bin*"


To find all the directories except the directories containing "bin" in directory name.


find . -type f -name ".*"


To find all the hidden files in current directory.


find / -type f -size +1G 


Finding all the files in the system which file size is greater than 1 GB.


find . -cnewer file1.txt -and ! -cnewer file2.txt


finding all the files in current directory which are created between file1.txt and file2.txt.  "and" is used as a connector here for the two conditions, if both true then the value will be true.


find . -type f -name \("*.txt" -o "*.sh"\)


Finding all the text files as well as shell scripts ".sh" in current directory 


find . -type f \(-mtime -10 -a -mtime +5\)


Finding files which are modified between 5 to 10 days back.



Questions & Answers

Qs: How to find a largest file in your current present directory ?


find . -type f -exec ls -s {} \; | sort -n -r | head -1


similarly, for smallest file size:

find . -type f -exec ls -s {} \; | sort -nr | tail -1


Qs: Find all the file where size is more than 1GB and which are older than 1 year and then remove it.


find . -type f -size +1G -mtime +365 -exec rm {} \;


Qs: How to make a back-up of all the files present in the system within 1 year ?


find / -type f -mtime -365 -exec mv {} $HOME {}.backup \;


Qs: Shortest find command for finding zero byte files and delete it.


find . -size 0 -delete


NEXT ->