Linux: CLI Searching, renaming and deleting

Change permissions or DIRs and FILEs resp.:

find /path/to/base/dir -type d -exec chmod 755 {} \; (find /path/to/base/dir -type f -exec chmod 644 {} \;

Search for files by filename or date:

Let's assume you want to find files with a .bar extension under the directory /foo (or with date 25 September):

find /foo -name *.bar

find /foo -type f -ls |grep '*.bar'

find /foo -type f -ls |grep '25 Sep'

Delete files with a particular extension

find . -type f -name '*.o' -delete

Search for files containing a pattern:

Now let's assume you want to find the text bar in files under the directory foo.

grep -rnw 'foo' -e "bar"

-r is recursive, -n is line number and -w means match whole word.

You can also choose to --exclude or --include files, e.g. search only those having .txt or .doc extensions:

grep --include=\*.{txt,doc} -rnw 'foo' -e "bar" 

And exclude files having a .doc extension:

grep -rnw --exclude=*.doc 'foo' -e "bar" 

You can also exclude or include directories using the --exclude-dir and --include-dir parameters, e.g.:

grep --exclude-dir={dirA,dirB,*.gimp} -rnw 'foo' -e "bar"


# Rename all *.MOV to *.mov
for f in *.MOV; do
mv -- "$f" "${f%.MOV}.XYZ"
for f in *.XYZ; do
mv -- "$f" "${f%.XYZ}.mov"
rename "s/MOV/XYZ/" *.MOV
rename "s/XYZ/mov/" *.XYZ

DELETING through subdirectories...

find . -name *.CRW -type f -delete