How to Use the Find Command in Linux?
Introduction
The `find` command is a magic tool for Linux users. It helps you search for files and directories on your system easily. It is magic because it is not simply a search command. You can specify various search criteria, such as file type, size, permissions, and modification dates.
All this is very useful when you are managing a large file system or trying to find a specific file quickly. Thus, it simplifies your work and saves you a lot of time as it can scan through entire directory structures and execute actions on matched files.
What Is the Find Command in Linux?
The find command is a Linux utility that searches directories recursively for files and folders by name, type, size, owner, permissions, or modification time. It can also run actions such as -exec or -delete on the matches, which makes it useful for locating files and managing storage on large file systems.
Use find when you need to search by file attributes, not just names, for example, files over 100MB, files modified in the last 7 days, or files owned a specific user.
What Is the Basic Syntax of the find Command?
The general syntax of the ‘find’ command has a simple structure, and that is –
find [path] [expression]
Here,
- The path defines where the search begins. It starts from the current directory if you do not specify a path.
- The expression consists of search parameters such as file names, types, and permissions. This helps filter results.
How to Set Up Sample Files to Practice find
Let us start to understand how ‘find’ works. First, create some example files and directories. The below steps give you a basic setup to test different ‘find’ command options.
=> Now, create some text files –
touch test1.txt test2.txt Test3.txt
=> Next, create directories and sub-directories –
mkdir -p folder1 folder2/subfolder
=> Create another file and change its permissions –
touch example.txt chmod 755 example.txt
What Are the Most Useful find Command Options?
The find command provides many options to refine searches. Below, you will see some commonly used ones.
- -name →
This finds files by their exact name. For example – Locate a file named test1.txt in the current directory and sub-directories with the below command –find . -name "test1.txt"
- -iname →
It performs a case-insensitive name search. It is useful if you are unsure about the exact capitalisation of a file name.find . -iname "test1.txt"
- -type →
This filters by file type (e.g., f for files, d for directories). Also, it is useful to list all directories within the current directory –find . -type d
- .-size →
Searches for files based on size (e.g., +50M for files larger than 50MB). Thus, it is very useful to search for Large Files larger than 100MB –find . -size +100M
- -mtime →
Finds files modified recently within last 7 days –find . -mtime -7
- -atime →
Searches for files accessed recently, say in past 7 days:find . -atime -7
- -user →
You can find all files owned by a specific user, say Max:find . -user max
- -perm →
Locate files with specific permissions
Example:find . -perm 755
Other Options –
- -group → Searches for specific group owners.
- -exec → Executes a command on matched files.
- -delete → This deletes files that match the search criteria.
- -ls → You can list files with detailed information.
- -print → This shows the file paths of matching results.
How to Use find with -exec, -delete, and Other Commands?
- See how -exec option execute a command on matched files –
Example – Find all test1.txt files with -name option and list their details in long format with the -ls option –find . -name "test1.txt" -exec ls -l {} \;{} means the placeholder. It is for working filename
\; is to excuse and specify the end of the command - -delete option delete matching files –
Example – Remove all test1.txt files:-find . -name "test1.txt" -delete
- Copy specific files to another directory –
Example – Find all .txt files and copy them to a folder:find . -name "*.txt" -exec cp {} /destination/path/ \; {} is replaced with the file name when you execute /destination/path/ will get your actual path. - Move Specific Files
Example – Locate .txt files and move them to a different directory:find . -name "*.txt" -exec mv {} /destination/path/ \; - Find and Modify Permissions
Example – Search for files with 644 permissions and change them to 600:sudo find . -perm 644 -exec chmod 600 {} \; - Use ‘find’ with wc
Find files named “.txt” and pipe (|) with wc -l command counts no. of lines matching.$ find . -name ".txt" | wc -l
Conclusion
The find command is a must-know tool for Linux users. You can search files in a precise manner with advanced filtering using multiple conditions. All in all, it is great for locating files, managing storage, or automating tasks. ‘find’ makes every process efficient. Go on, experiment with different options and understand their full potential.
FAQs
What is the syntax of the find command in Linux?
The syntax is find [path] [expression]. The path is where the search starts (the current directory if omitted), and the expression holds the criteria.
find . -name "test1.txt"
This searches the current directory and all subdirectories for test1.txt.
What are the most common find command options?
The most used options are -name (match by name), -iname (case-insensitive name), -type (file or directory), -size (match by size), -mtime (modified time), -user (owner), -perm (permissions), -exec (run a command on matches), and -delete (remove matches).
find . -iname "test*.txt"
How to find a folder in Linux?
Use find with -type d and the folder name.
find / -type d -name "folder1" 2>/dev/null
/ searches the whole system, and 2>/dev/null hides “permission denied” errors. Use . to search only the current directory, or -iname to ignore case.
How to find the path of a file in Linux?
Run find with the file name. Each result printed is the file’s full path.
find / -name "example.txt" 2>/dev/null
To get the absolute path of a file you are already looking at, use realpath example.txt.
How to find only files or only directories?
Use -type f for files and -type d for directories.
find . -type f find . -type d
Use -type l for symbolic links. Combine it with -name to narrow the results, for example find . -type f -name "*.log".
How to find a user or files owned by a user in Linux?
To find files owned by a user, use -user:
find /home -user max
To check that the user exists, run id max or getent passwd max. To list all users on the system, run cut -d: -f1 /etc/passwd.
How to search for text inside files with find?
find matches file names, not contents, so combine it with grep using -exec.
find . -type f -name "*.txt" -exec grep -l "keyword" {} +
This lists every .txt file that contains “keyword”. For a quick search without filtering by file type, grep -rn "keyword" . also works.