The find command is used to search for files and directories within a directory hierarchy in Linux.
-
Syntax:
find [path] [options] [expression]
-
Examples:
find /home/user -name "file.txt"- Searches for a file named
file.txtin the/home/userdirectory and its subdirectories.
find /var/log -type f -name "*.log"- Finds all files with a
.logextension in the/var/logdirectory.
find / -type d -name "backup"- Searches for directories named
backupstarting from the root directory.
find . -mtime -7- Finds files modified in the last 7 days in the current directory.
find /home/user -size +100M
- Finds files larger than 100 MB in
/home/user.
- Searches for a file named
-
-name [pattern]:- Searches for files or directories that match the specified name or pattern.
find . -name "*.txt"
- Finds all
.txtfiles in the current directory.
-
-type [d/f]:- Specifies the type of item to search for:
dfor directoriesffor files
find / -type d -name "config"- Finds directories named
config.
- Specifies the type of item to search for:
-
-size [N]:- Finds files of a specific size.
+Nfor files larger than N units.-Nfor files smaller than N units.Nfor files exactly N units.
find . -size +10M- Finds files larger than 10 MB.
- Finds files of a specific size.
-
-mtime [N]:- Finds files modified
Ndays ago.+Nfor files modified more than N days ago.-Nfor files modified less than N days ago.Nfor files modified exactly N days ago.
find . -mtime -1- Finds files modified in the last 24 hours.
- Finds files modified
-
-exec [command] {} \;:- Executes a command on each file found.
find . -name "*.log" -exec rm {} \;
- Deletes all
.logfiles in the current directory and its subdirectories.
-
-perm [mode]:- Finds files with specific permissions.
find . -perm 644- Finds files with
644permissions.
-
-user [username]:- Finds files owned by a specific user.
find /home -user alice
- Finds all files owned by
alicein/home.
-
-group [groupname]:- Finds files belonging to a specific group.
find /data -group admins
- Finds all files owned by the
adminsgroup in/data.
-
-inum [inode number]:- Finds files with a specific inode number.
find / -inum 123456
- Finds the file with inode number
123456.
-
--help:- Displays help information about the
findcommand.
find --help
- Shows usage information and options.
- Displays help information about the
-
Search from Current Directory:
- Running
findwith.as the path starts the search from the current directory.
find . -name "*.conf"
- Running
-
Combining Multiple Conditions:
- You can combine multiple conditions using
-and(default) or-or.
find / -name "*.conf" -or -name "*.log"
- You can combine multiple conditions using
-
Exclude Certain Paths:
- Use
-pruneto exclude directories from the search.
find / -path "/proc" -prune -or -name "*.log"
- Use
-
Speed Up Searches:
- To speed up searches, specify a narrower path or use more specific options.
The find command is a versatile and powerful tool for searching files and directories in Linux. It supports various options to filter results by name, type, size, modification time, permissions, and more. Use it to efficiently locate files, perform bulk operations, or analyze the filesystem.