Using find in interesting ways

find

Find all within current directory

find . -type f -iname "*"

 

Find all txt files but skip all bz2 files

find . -type f \( -iname "*.txt" ! -iname "*.bz2" \)

 

Find in all php files for a specific wording for ex: " but ignore all "php" :

find ./ -name "*.php" -exec grep -e {} +;

 

Zip all logs within a certain directory but skip already zipped files

find . -type f \( -iname "*" ! -iname "*.bz2" \) -print0 | while IFS= read -r -d $'\0' line; do
echo "Bunzipping: $line"
bunzip2 -z "$line"
done

 

 Find, Replace & Save

# In some legacy php4 code there were shortcuts to echo (big no no to use)
# which had to be replaced in many files, this would do the trick.

find ./ -name "*.php" -exec sed -i 's/\<\?=/\<\?php\ echo\ /g' {} \;



# Find and all php files, and check catch all php syntax starters but not any with php ending, show the result with sed what it would replace.

find ./ -name "*.php" -exec grep -e "&lt?" {} + | grep -vi "&lt?php" | sed s/\<\?/\<\?php\ echo\ /

 

Find and change permissions


# Find folder called logs and check file permissions within.
find ./ -name "logs" -type d -exec ls -la {} + ;

# Find and change permissions on multiple folders with the same name. 
find ./ -name "specialfolder" -type d -exec chmod 755 {} \;

# Chane permissions on all files within a folder.
find ./SomePath -type f -exec chmod 644 {} \;

 

Posted on: Thursday 11 June 2015