Living in the Shell #10; find (File/Directory Search) (Part 2)
find 🔍
Finds files matching given criteria.
Detailed result -ls
find /home/babak -name "*bash*" -ls
11053679 4 -rw-r--r-- 1 babak babak 3772 Nov 25 12:45 /home/babak/.bashrc 11010465 28 -rw------- 1 babak babak 24763 Nov 16 12:26 /home/babak/.bash_history
Evaluate by executing a command on files -exec
Example 1: looking for a word within files
find ~ -name '*.txt' -exec grep -q "hello" {} \; -print
- Finds all
.txtfiles that contain word “hello” in their content.- The
{}is the placeholder for the file path.\;indicates the end of the command string.
Example 2: looking for damages ZIP archives
find ~ -name '*.zip' -not -exec zip -qT {} \; -print
zip -qTquietly checks ZIP archive integrity.-notnegates the logical expression.
Example 3: detect unformatted code files in a Python codebase
find . -type f -name '*.py' -not -path '*/venv/*' -not -path '*/__pycache__/*' -not -exec sh -c 'python3 -m autopep8 {} >/dev/null' \; -print
autopep8applies Python PEP8 standard formatting.>/dev/nullavoids cluttered/unnecessary output.-not -path '*/venv/*'and-not -path '*/__pycache__/*'excludevenvand__pycache__directories.
Example 4: detect unformatted code files in a JavaScript codebase
find . -type f -name '*.js' -not -exec sh -c 'eslint --no-eslintrc {} >/dev/null' \; -print
eslintis the ESLint CLI.--no-eslintrcis for safety, to eliminate the need for a.eslintrcfile.
About Living in the Shell
Obsessed with doing things in the shell, I’ve decided to share my daily struggles on living in the shell as terse but informative posts.