One of the main reasons behind the global popularity enjoyed by Linux and BSD systems today is their default tools and utilities. Linux offers the best methods of searching and accessing files directly from the command line.
The find command in Linux is one such utility that allows users to search their system for particular types of files effectively. It is a command-line utility that allows powerful searching functionalities and can be a compelling tool for seasoned users.
If you want to master the find command in detail and accelerate your Linux skills, you have come to the right place.
Everyday Examples of Find Command in Linux
We believe the best way to learn Linux commands is to try them out yourself. The commands below should work fine on every Linux distribution despite your desktop environment.
So, you can quickly try some find commands in Linux by opening a terminal using Ctrl + T
and copy-pasting the commands from here.
Try to utilize these commands on your day-to-day computing as much as possible to master them quickly.
Structure of the Find Command
The find command in Linux has the below structure.
find [paths] [expression] [actions]
Here, paths denote the directory where you won’t “find” to search for the specified files. The expression will allow you to filter your files based on some criteria, while actions will let you execute shell commands on files.
The default action is print, which prints the files matched by expression in any path. Find shows the files recursively, meaning it will traverse every directory and print out the results accordingly.
Basic Linux Find Commands
You can utilize some basic find commands to understand what find can do. These commands are pretty straightforward and don’t require any prior experience with other command-line utilities.
1. List All Files Present Inside the Current Directory
$ find
This will print all the files present in the current directory. If the current directory contains additional directories, it will show them also. This command is equivalent to the command
$ find -print
2. List All Files Present in a Specific Directory
$ find /folder
This command will print all the files present inside the directory /folder
. You can use this command to list all the files of a specific path in your Linux system.
3. Search for a Specific File
$ find -name test.txt
This command searches for a file called test.txt
inside your current directory and in every other sub-directory. Use this command when you are searching for specific files.
4. Search for a Specific File in a Directory
$ find /Docs -name test.txt
This command will search for the file named test.txt in the folder called /Docs
. You can use both absolute paths and relative paths when using this command.
5. Search for a File in Multiple Directories
$ find /opt /usr /var -name foo.scala -type f
You can use this find command when searching for a file in multiple directories simultaneously. Put the directory names one after another, followed by a space when searching.
6. Search for a File Ignoring Case
$ find -iname test.txt
This command will search for the file test.txt without matching the case. So, if you’ve two files called test.txt
and Test.txt
, it will display both files. The -iname
option allows the find command to do this.
7. Search for Folders Inside the Current Directory
$ find -type d
This command will list every directory you have under your current working directory. You can add the name option for listing out specific directories.
8. Search for A Specific Folder in a Directory
$ find /home -type d -name users
This command will search for a folder called users inside the /home
directory. You can add the -iname
option instead of -name
to search without respect to the case.
9. Search for PHP Files Using Name
$ find -type f -name test.php
This command will list the PHP file test.php
, which is in the current working directory.
10. Search for all PHP Files
$ find -type f -name "*.php"
This find command in Linux will print out every PHP file you have inside your current working directory. You can add the path option before the type for listing PHP files that are present only in a specific directory.
11. Find All Symbolic Links
$ find /usr -type l
The above command will search for every symbolic link inside your current directory and print out accordingly.
12. Search for Files With Different Extensions
$ find . -type f \( -name "*cache" -o -name "*xml" -o -name "*html" \)
The above find command searches for files named cache with different extensions. You can find a search for additional extensions by adding the name option followed by the -o
flag.
Find Files Based on Permission
The find command allows Linux users to search for files based on their permission status. This will come in handy when your system has multiple users and you need to ensure nobody gets unauthorized access to your data.
13. Find Files That Have 777 Permission Set
$ find -type f -perm 0777 -print
This command will list all the files in the current working directory with their permission set to 777. These are the files that any user can read, write, and execute.
14. Find Files Without 777 Permissions
$ find / -type f ! -perm 777
This find command in Linux will only search for files with permissions set other than 777. You can replace / with any other location to narrow your search results.
15. Find SGID Files with 644 Permissions
$ find / -perm 2644
This find command will only look for SGID files with permission status set to 644. SGID files allow temporary access to files you don’t own or have access to.
16. Find All Sticky Bit Files with 551 Permissions
$ find / -perm 1551
Sticky Bit files are files or folders that can only be renamed or deleted by the user who created them or the root user. This command will show all Sticky Bit files in your system with 551 permissions.
17. Find All SUID Files
$ find / -perm /u=s
SUID files allow temporary ownership of a filegroup to users other than the owner of the filegroup or root user. This find command lists all SUID files in your current Linux machine.
18. Find All SGID Files
$ find / -perm /g=s
SGID files are similar to SUID files in many ways, except that when files with SGID permissions are run, the execution takes place as if the original owner was running the process. This find command lists every single SGID file irrespective of their permission status.
19. Find Read-Only Files
$ find / -perm /u=r
Read-only files prohibit Linux users from writing to them or executing them. They can be only written or executed by the file owner or the root user. This find command will display all the read-only files your machine currently has.
20. Find All Executable Files
$ find / -perm /a=x
Executable files are simply files that can be executed, such as binary files. The above Linux “find command” will search the system for every such file and list them accordingly.
21. Find All Files with 777 Permissions and chmod to 644
$ find / -type f -perm 0777 -print -exec chmod 644 {} \;
The above find command searches all files with 777 permission associated with them and will change their permission status to 644 using the chmod command. Only you can now read or write to the files with 644 permission.
22. Find All Directories with 777 Permissions and chmod to 755
$ find / -type d -perm 777 -print -exec chmod 755 {} \;
This Linux find command will search for every directory that has 777 permission and will change its permission status to 755. What this does, in essence, is allow full permissions only for the owner and read and execute permission for other users.
Search Specific Files Using Find
Find can be used to search specific files quite effectively. You can use Find to select files based on some criteria and perform shell operations like file deletion on them.
23. Find a File and Remove It
$ find -type f -name "test.txt" -exec rm -f {} \;
This Linux “find” command is used when you need to delete a file from a list of many files. In this case, it finds the file test.txt
in the current directory and removes it using rm- f.
24. Find Multiple Files and Remove them at Once
$ find -type f -name "*.mp3" -exec rm -f {} \;
The find command is useful for searching large arrays of specific file types and removing them simultaneously. The above command searches your system for .mp3
files and deletes them without prompt. You can add the interactive flag -i
with the rm part to get a prompt each time a deletion occurs.
25. Find All Empty Files in The System
$ find /tmp -type f -empty
Empty files can hog up your system resources in a very short time. Using the find command, use the above command to list all the empty files. You can delete these files by adding -exec rm -f {} \;
just like the above command.
26. Find All Empty Folders in The System
$ find /tmp -type d -empty
This command will list all empty folders residing inside the /tmp
directory. You can use this to find empty folders in any other directory and delete them, as you did earlier.
27. Find All Hidden Folders in The System
$ find /home -type f -name ".*"
Hidden folders are usually prefixed by a single dot(.) in Linux systems. Use the above command to list all the hidden folders inside your /home
directory.
Find Files Based on the User
The find command is also useful for searching files based on user groups. You can search for specific files for certain user groups and quickly modify file permissions.
28. Find A File that Belongs to User
$ find / -user root -name test.txt
You can use the find command in Linux to search for a single file owned by a specific user. The above command searches for a file called test.txt
in the /
directory that belongs to the user root.
29. Find All Files that Belong to User
$ find /home -user username
The above Linux command searches for all files in the /home directory that belongs to the user “username”. You will need to replace “username” with your Linux username to find all the files that belong to you.
30. Find All Files that Belong to a Group
$ find /home -group programmer
Linux files usually belong to some groups. The above find command allows you to search for all the files that belong to a particular group called “programmer” and print them in the terminal. Replace “programmer” with the group name you want to search for.
31. Find Specific Files for a User
$ find /home -user bob -iname "*.txt"
You can use the find command to search for specific files that belong to a user. The above command does this and lists all the .txt
files belonging to the user bob
. Replace bob
with your username and .txt
with any other file types for finding files of a certain type that belong to you.
Find Files Based on Time
Find also allows sysadmins to monitor their system effectively. It allows searching for files based on modification time, access time, etc.
32. Find All Files That Have Been Modified in Last 50 Days
$ find / -mtime 50
The find command allows users to search for files that have been modified within a given time. The above command will print out all the files in your system that have been modified 50 days ago.
33. Find All Files That Have Been Accessed in Last 50 Days
$ find / -atime 50
The -atime
option shows the files that have been accessed within a defined timeframe. The above command lists all your system files that were accessed 50 days back.
34. Find All Files Modified in the Last 50-100 Days
$ find / -mtime +50 –mtime -100
The find command in Linux allows users to search for all the files modified in a given time range. The +
and -
operator is used in conjunction with -mtime
for doing this. The above command finds all the files you have modified in the last 50 to 100 days.
35. Find All Files Changed in Last 1 Hour
$ find / -cmin -60
This command will find and list all the files that have been changed in the last hour. You can replace / with a specific directory for narrowing down your searches. Change 60 to any other number, like 120, to find files that have been changed in that time (2 hours for 120).
36. Find All Files Modified in the Last 1 Hour
$ find / -mmin -60
The above command will show all the files that have been modified within the last 1 hour. Switch 60 to any other number to change the timeframe your purpose requires.
37. Find All Files Accessed in the Last 1 Hour
$ find / -amin -60
This command displays all the files you have accessed in the last hour. As with the two preceding commands, change 60 for your desired outcome.
Find Files Based on Size
Sometimes, you’ll need to search files based on their sizes. Find also comes in handy in this respect. You can add different options for searching files based on size more accurately.
38. Find Files of Size 50 MB
$ find / -size 50M
This “find command” in Linux prints out all the files you have over 50 MB of size. Replace /
with your desired directory and 50M with any other size to narrow your search results more effectively.
39. Find All Files Over 100 MB
$ find / -size +100M
The above command lists all your files over the 100 MB mark inside your /
directory. You can change 100M with other file sizes to get your desired result.
40. Find Files Between 50MB to 100MB
$ find / -size +50M -size -100M
Sometimes, you’ll need to find files within a specified size range. The above command will display all your files between 50MB and 100MB sizes. Change the optional parameters to match any specific search criteria.
41. Delete All Files Over 500 MB
$ find /Movies -size +500M -exec rm -rf {} \;
The find command is useful for searching files over a specific limit and deleting them instantly from the terminal. Suppose you’ve got some old movies in a folder and want to delete them in one go. The above command will let you do precisely this. Replace /Movies
with the folder name where your files reside.
42. Find Largest Files
$ find . -type f -exec ls -s {} \; | sort -n -r | head -5
The above find command will print the 5 largest files in your working directory under its sub-directories.
43. Find Smallest Files
$ find . -type f -exec ls -s {} \; | sort -n | head -5
You can also use the find command in Linux to display the smallest files. This command prints out the 5 smallest files under your current directory.
Miscellaneous Find Commands
The find command in Linux offers many additional capabilities, like finding files based on the text they contain, searching and deleting files, finding files based on patterns, etc. The below commands demonstrate some of these abilities in short.
44. Find and Delete Specific Files
$ find / -type f -name *.mp3 -size +10M -exec rm {} \;
This Linux “find command” enables users to find all .mp3 files in their system that occupy more than 10 MB space and delete them. You can replace .mp3 with any other file type and the size parameter for specific types of files.
45. Find Files that Don’t Match a Pattern
$ find /home -type f -not -name "*.html"
The above find command in Linux will search for all the files in the /home
directory that doesn’t end in .html
. The -not
option allows “find” to do this.
46. Find Files by Text inside The File
$ find . -type f -name "*.java" -exec grep -l StringBuffer {} \;
You can use grep to find files based on the text they contain. The above Linux “find command” searches for .java
files containing StringBuffer. Adding the -i
flag to grep will make this search ignore the case.
47. Find and Copy Files
$ find . -type f -name "*.mp3" -exec cp {} /home/MusicFiles \;
The find command can find certain files and copy them to a new location. The above command finds all .mp3 files in the current directory and copies them to the folder /home/MusicFiles
.
48. Find and Move Files
$ find . -type f -name "*.jpg" -exec cp {} /home/Pictures \;
Find can also be used for moving files effectively. The above command searches every .jpg file you have under your current directory and moves them to the directory /home/Pictures
.
49. Find and Tar Files
$ find . -type f -name "*.java" | xargs tar cvf myfile.tar
You can use find to search for some specific files and archiving them into tarballs. The above command finds all .java
files in the current directory and compresses them into a tar file called myfile.tar
.
50. Filtering Error Messages
$ find [paths] [expression] [actions] 2>/dev/null
Sometimes, you may face errors like ‘Permission Denied’ or something else while trying out some find commands. As shown above, you can redirect these errors to /dev/null
.
Ending Thoughts
The “Find command” in Linux is an incredibly useful utility that allows users to search for specific files based on various criteria. With its powerful searching functionalities and ability to execute shell commands on files, it can be a compelling tool for seasoned users.
The basic find commands mentioned in this article are straightforward and can be tried out even by users without prior experience with other command-line utilities. Using these commands daily, Linux users can quickly master them and accelerate their Linux skills.
Moreover, the find command allows users to search for files based on their permission status, making it an important tool for ensuring unauthorized access to data doesn’t occur.
cool