The find command in Linux is a useful tool for searching for files and directories. The find command offers different search criteria or options including searching by file name, type, size, owner, modification time, empty files, etc. The searches are performed in a recursive manner. Additionally, there is an added flexibility of searching using regex patterns.
Examples:
To find a file by name in the current directory, use the below find command:
find . -name testfile1
To search for a file by name but ignore the case:
find . -iname testfile1
To find a file by name and search recursively from the root directory, use:
find / -name auth.log
find / -name auth.log 2>/dev/null (here the 2>/dev/null is used to suppress error messages)
To search specifically for files that match a given search string:
find . -type f -name “test*”
To search specifically for directories by matching the given search string:
find . -type d -name “test*”
To find files of a specific size:
To find hidden files:
find . -type f -name “.*”
To find files that were modified in the last 1 hour:
find / -cmin -60 2>/dev/null
To find files that were modified more than 1 day and less than 40 days ago:
find . -mtime +1 -mtime -40
To find files that have specific permissions:
To find empty directories, use the below find command:
find /tmp -type d -empty 2>/dev/null
Another powerful aspect of the find command is the ability to take its results and provide them as input for other commands. Consequently, we perform specific actions on files or directories that we discover using the find command. The find command can be used together with the xargs command to pipe output and then feed it to a different command as input. The xargs command makes it possible to input results to commands, such as mkdir and rm, that do not accept piped input. In the below example, we are feeding the files found by the find command into the xargs command, which in turn pipes them into the gzip and gunzip commands that compresses and decompresses those files respectfully.
Command to compress files that start with “test”:
find ./ -name “test*” -type f -print0 | xargs -0 gzip
Command to decompress files that start with “test”
find ./ -name “test*” -type f -print0 | xargs -0 gunzip
ls -l –> Command to list all files
find ./ -name “test*” –> Find command; start the search in the current directory and search by the name for those files that match the string “test*”
-type f –> Specifies to search for only files and not directories.
-print0 –> This argument tells the find command not to treat whitespace as the end of a filename.
xargs –> Reads items from standard input and executes some action for each item
xargs -0 –> The -0 option tells xargs not to treat whitespace as the end of a filename.
The find command also supports the use of the -exec option to perform actions on those files that are found using the find command. The two examples below provide a comparison of using xargs and -exec option to execute a rm action on files resulting from the find command.
find ./ -name “*.txt” -type f | xargs rm
find ./ -name “*.txt” -type f -exec rm {} \;