Find Command Cheatsheet
Linux Find Command Examples
The find command is a powerful utility in Linux and Unix-like operating systems used to search for files and directories within a directory hierarchy. It allows for flexible searching based on various criteria such as name, type, size, modification time, and more. This cheatsheet provides practical examples to help you master the find command.
Finding Files by Name
To find a file with the exact name hello.txt somewhere in the ~/workspace directory:
find ~/workspace -type f -name 'hello.txt'
This command searches the ~/workspace directory and its subdirectories for files (-type f) named exactly hello.txt (-name 'hello.txt').
Finding Directories by Name
To find any directories with the name temp somewhere in the ~/workspace directory:
find ~/workspace -type d -name 'temp'
This command searches for directories (-type d) named temp within the specified path.
Finding and Deleting Specific Directories
To find and delete any folders that start with .terraform/providers:
find . -type d -name '.terraform' -exec bash -c 'if [ -d "$1/providers" ]; then rm -rf "$1/providers"; fi' bash {} \;
This command searches the current directory (.) for directories named .terraform. For each found directory, it executes a bash script that checks if a subdirectory named providers exists within it. If it does, the providers directory is recursively deleted (rm -rf).