How to Find Files and Folders in Linux
Master the Linux find command to effortlessly locate files and manage your system.
The find
command is used to search and locate files and directories based on specified conditions such as file name, size, modification date, and other attributes. You can use it to search through your entire file system or within a specific directory.
It’s commonly used by system administrators and regular users to quickly locate files, especially in systems with a large number of files. Whether you’re looking for a lost document or need to perform bulk operations on a set of files that meet certain criteria, the find
command can be an essential tool in managing and organizing your file system.
General syntax for find
command:
$ find [OPTIONS] [PATH...] [EXPRESSION]
1. Search for specific file in a directory
find ./ExampleDir -name example.txt
With the -name
parameter, this command will attempt to search for a example.txt
within the ExampleDir
directory; and if found, will return path to the file.
./ExampleDir/subdir1/example.txt ./ExampleDir/subdir2/subsubdir/example.txt
2. Find and list files of same extension
find ./dirname -name *.txt
This command will search, within the ExampleDir
directory, all files ending with the extension .txt
. If found, each result will be returned in a new line.
./dirname/file1.txt ./dirname/file2.txt ./dirname/subdir/file3.txt ./dirname/subdir/another_subdir/file4.txt
3. Find and list empty files and empty sub-directories
find ./ExampleDir -empty
This command, with the -empty
parameter, will find and list all empty files and empty sub-folders inside the ExampleDir
folder.
Definition of empty file being 0 bytes filesize, and empty folder being no files or files with 0 bytes.
4. Find and list files that contain specific text
find ./ExampleDir -type f -name "*.txt" -exec grep 'Example' {} \;
This command searches for the word/string "Example" inside files with the extension .txt
inside ExampleDir
dierctory.
./ExampleDir/file1.txt:This is an Example line in file1. ./ExampleDir/subdir/file2.txt:Another Example in a different file. ./ExampleDir/file3.txt:Example usage of the find command.
5. Find and list files and sub-directories own by specific user
find ./ExampleDir -user ubuntu
This command, with the -user
parameter, will find files and sub-directories owned by Ubuntu
user in ExampleDir
directory. If found, the filename(s) will be returned.
In the following sample ls -l
result:
-rw-rw-r-- 1 newone ubuntu 20 Jan 27 06:24 example.txt
newone
represents group name, and ubuntu
is the user.
6. Find and list files and sub-directories own by specific group
find ./ExampleDir -group ubuntu
This command, with the -group
paramter, will find all files and sub-directories owned by Ubuntu
group in ExampleDir
directory. If found, the filename(s) will be returned.
More Linux commands:
Directory Operations | rmdir · cd · pwd · exa · ls |
File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
File System Operations | chown · mkfs · locate |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
System Information and Management | env · history · top · who · htop · glances · lsof |
User and Session Management | screen · su · sudo · open |