Linux is renowned for its vast array of powerful command-line utilities, and the find command stands out as a versatile tool for searching files and directories based on various criteria. Whether you are a seasoned Linux user or a curious beginner, mastering the find command will significantly boost your productivity and efficiency.
In this article, we will explore 26 useful find command examples to unleash its full potential for file search in Linux.
Syntax
$ find <path> {file-or-directory-name} <options> <action-on-result>
Actions on find command result can be
- – delete : Delete files or directories
- -exec command {}\; : Run the command on the result obtained from find command
- -ok command : It will run command same as -exec but it will prompt before the actual execution.
Without any further delay let’s jump into find command examples,
1) Search all Files and Directories
To search for directories in current working folder, employ the -type d flag.
$ find . -type d
To search all the files only & not directories, run
$ find . -type f
2) Lists all the files of Specific Directory
Let’s suppose we want to list all files and directories of /home/linuxtechi/Downloads folder, run
$ find /home/linuxtechi/Downloads
Print only files, run
$ find /home/linuxtechi/Downloads -type f
Run following command to print only directories,
$ find /home/linuxtechi/Downloads -type d
output of above commands,
3) Searching File with Name
The most straightforward use of find is to search for files by name. The following command will find all files named “cleanup.sh” within the current directory and its sub-directories.
$ sudo find /home -type f -name cleanup.sh
Above command will look for cleanup.sh file in /home folder. We can also look for all the files with .log extension in /var/log folder, run
$ sudo find /var/log -type f -name *.log
4) Searching Files in Multiple Directories
Let’s assume we want to search .sh extension files in /home and /root folder, run
$ sudo find /home /root -type f -name '*.sh'
5) Case-Insensitive File Search
For a case-insensitive search, use the -iname option, example is shown below,
$ sudo find /home -type f -iname CleanUP.SH /home/linuxtechi/automation/cleanup.sh $
The result of the command will find the file with name cleanup.sh, whether its in lower case or upper case or in mixed cases.
6)Exclude Specific Files or Directories
Let’s suppose we want to find all the files that are not the mentioned type, to achieve this we can use -not option in find command. Example is shown below,
$ sudo find /home -type f -not -name '*.mp3'
Use the -not option to exclude specific directories from the search results.
$ find /home -type f -not -path "./exclude/*"
7) Locate Files with Multiple Conditions
We can also combine more than one condition to search the files using regular expression , Let’s suppose we want to search files of ‘.sh’ and ‘.mp3’ extensions in our home directory, run
$ find $HOME -regex ".*\.\(sh\|mp3\)$"
8) Combining Multiple Conditions
Combine multiple conditions with logical operators like -a (AND) and -o (OR) to fine-tune your search.
$ find $HOME -name "*.sh" -o -name "jumpscripts" /home/linuxtechi/automation/cleanup.sh /home/linuxtechi/dumpdata.sh $
9) Search for Files with Specific Permissions
To search for files based on the permissions, use -perm option in find command. Find all files in /home folder with permissions ‘0777’, run
$ sudo find /home -type f -perm 0777
Search all the executable scripts in user’s home directory
$ find $HOME -type f -perm /a+x
10) Locate All Hidden Files
To search for all the hidden files in user’s home directory, run the command
$ find $HOME -type f -name ".*"
11) Search all the files with SGID
To locate all the files with SGID bits, we can use
$ sudo find / -perm /g=s or $ sudo find / -perm -2000
12) Find all the files with SUID
To locate all the files with SUID bits, run
$ sudo find / -perm /u=s or $ sudo find / -perm -4000
13) Search all files which are readable but don’t have execute permissions
Search for the files that are readable by everybody but can not be executed by anybody, run
$ sudo find $HOME -perm -a+r \! -perm /a+x
14) Search Files Basis on File Types
In single find command, we can search multiple file types,
$ find $HOME -type f,d,l
15) Locate Files by Ownership
To locate all the file that are owned by a particular user in /home directory use -user option, run following command,
$ sudo find $HOME -user linuxtechi
16) Locate Files by Group Ownership
To locate all the files that are owned by a particular group use -group option, below command will search all files which are owned by apache group.
$ sudo find / -group apache
17) Search for Files by Size
Use -size option in find command to search files based on the size. Run following command to find all files whose size is exactly 50MB.
$ find $HOME -size 50M /home/linuxtechi/dbstuff $
Search files whose is size greater than 50MB,
$ find $HOME -size +50M
Search files whose size is less than 50MB
$ find $HOME -size -50M
Locate all files whose size is in range between 40MB to 500MB
$ find $HOME -size +40M -size -500M
18) Restrict Find Command
To restricty the find command from descending into directories on other file systems, you can use the -xdev option. This option instructs find to stay on the same file system and not cross mount points. This is useful when you want to search for files within a specific file system and avoid traversing into separate partitions or devices.
Beneath command will search all files whose size is more than 100MB in / file system and exclude other mounted file system moreover it will redirect error message to /dev/null
$ find / -xdev -size +100M 2>/dev/null
19) Search by File Modification Time
For example, we want to search all the files that have been modified 10 days ago. We can accomplish that using ‘-mtime’ option in find command
$ sudo find / -mtime 10 2>/dev/null
20) Searching by File Access Time
Similarly like above example, we can also locate files that have been accessed 30 days ago using ‘-atime’,
$ sudo find / -atime 30 2>/dev/null
21) Search Empty Files and Directories
To search all the empty files in user’s home directory, run
$ find $HOME -type f -empty or $ find $HOME -type f -size 0
Similarly, to locate all the empty directories
$ find $HOME -type d -empty
22) Search and Delete Files
Using find command, we search and delete the files using a single command. ‘-delete’ option in find command can delete files.
In following example, we are searching and deleting mp3 files from user’s home directory
$ find $HOME -type f -name "*.mp3" -delete
Note : Above is destructive command, be careful while executing it.
23) Locate Largest and Smallest Files
To search largest and smallest file on the basis of their size, we will combine sort command along with find command & if we further want to list top three of those largest files, we will combine head command.
To list top three files in the user’s home directory, run
$ find $HOME -type f -exec ls -s {} \; | sort -n -r | head -3 51200 /home/linuxtechi/dbstuff 8276 /home/linuxtechi/.cache/gnome-software/appstream/components.xmlb 2764 /home/linuxtechi/.local/share/gnome-photos/tracker3/private/meta.db-wal $
We can similarly find the smallest files in the user’s home directory,
$ find $HOME -type f -exec ls -s {} \; | sort -n | head -3
24) Search All log Files and Redirect them to a File
The -exec option in the find command is used to perform actions on the files or directories that are found during the search. It allows you to execute external commands, scripts, or other utilities on the files that match the specified criteria
$ find <path> <search-pattern> -exec <command> {} \;
Following command will locate all log files and redirect their names to a file /tmp/logsfiles.txt
$ sudo find /var -type f -name '*.log' -exec ls -lah {} \; > /tmp/logfiles.txt
Another Example
$ sudo find / -type f -name "*.jpg" -exec cp {} /var/tmp/images \;
25) Search Files and Change their Permissions
Let’s suppose we want to search all files whose permissions is 777 and change their permissions to 644
$ find $HOME -type f -perm 777 -exec chmod 644 {} \;
26) Search Text from Files
Let’s assume we want to search error word in all log files, run following command
$ sudo find /var -type f -name '*.log' -exec grep -i 'error' {} \; or $ sudo find / -type f -name "*.log" -print0 | xargs -0 grep "error"
In above command we have combined find and grep command to accomplish the task.
Conclusion
The “find” command is undoubtedly a powerful tool for searching and managing files in Linux. Its flexibility and extensive range of options make it a must-have utility for any Linux user or system administrator. From basic file searches to complex operations, these 26 examples demonstrate the versatility and efficiency of the “find” command. Experiment with these commands and unleash the full potential of your Linux command line experience.
Read Also: 16 Useful ‘cp’ Command Examples for Linux Beginners
Item 8
We can also combine more than one condition to search the files we need,
$ find ./root -name “*.txt” | -name “linuxtechi*”
That pipe looks like a mistake.
Hi ,
Thanks for identifying the error. It was a typo, i have correct it now.
Thank you for making an awesome list of commands. It’s very useful but I don’t find anything about grep command? It’s amazingly powerful and helpful command in Linux to access file contents.
Sir, how to install Nero application in my laptop. I am using Linux ubuntu 16.4 OS at dell inspiron 15 5000 core i5 7th gen. I am confused. So clear and send practice video and pdf