find command in Linux with examples - GeeksforGeeks

24 Nov, 2021
The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions. By using the ‘-exec’ other UNIX commands can be executed on files or folders found.
Syntax :
$ find [where to start searching from]
 [expression determines what to find] [-options] [what to find]
Options :
-exec CMD: The file being searched which meets the above criteria and returns 0 for as its exit status for successful command execution.
-ok CMD : It works same as -exec except the user is prompted first.
-inum N : Search for files with inode number ‘N’.
-links N : Search for files with ‘N’ links.
-name demo : Search for files that are specified by ‘demo’.
-newer file : Search for files that were modified/created after ‘file’.
-perm octal : Search for the file if permission is ‘octal’.
-print : Display the path name of the files found by using the rest of the criteria.
-empty : Search for empty files and directories.
-size +N/-N : Search for files of ‘N’ blocks; ‘N’ followed by ‘c’can be used to measure size in characters; ‘+N’ means size > ‘N’ blocks and ‘-N’ means size < 'N' blocks.
-user name : Search for files owned by user name or ID ‘name’.
\(expr \) : True if ‘expr’ is true; used for grouping criteria combined with OR or AND.
! expr : True if ‘expr’ is false.
Examples :Consider the following tree hirerachy :
1. Search a file with specific name.
$ find ./GFG -name sample.txt 
It will search for sample.txt in GFG directory.Output :
2. Search a file with pattern.
$ find ./GFG -name *.txt 
It will give all files which have ‘.txt’ at the end.Output :
3. How to find and delete a file with confirmation.
$ find ./GFG -name sample.txt -exec rm -i {} \; 
When this command is entered, a prompt will come for confirmation, if you want to delete sample.txt or not. if you enter ‘Y/y’ it will delete the file.Output :
4. Search for empty files and directories.
$ find ./GFG -empty
This command find all empty folders and files in the entered directory or sub-directories.Output :
5. Search for file with entered permissions.
$ find ./GFG -perm 664
This command find all the files in the GFG directory or sub-directory with the given permissions.Output :
6. Search text within multiple files.
$ find ./ -type f -name "*.txt" -exec grep 'Geek'  {} \;
This command print lines which have ‘Geek’ in them and ‘-type f’ specifies the input type is a file.Output :YouTubeGeeksforGeeks500K subscribersLinux Tutorials | find command | GeeksforGeeksWatch laterShareCopy link30/36InfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 5:16•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=oPdFGUrq-XQ" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
ManasChhabra2
linux-command
Linux-file-commands
Articles
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
SQL | Views
Mutex vs Semaphore
Time Complexity and Space Complexity
SQL Interview Questions
Analysis of Algorithms | Set 2 (Worst, Average and Best Cases)
SQL | GROUP BY
Recursive Practice Problems with Solutions
Java Tutorial
Little and Big Endian Mystery