Shell Script Snippets

Useful Bash shell scripts and one-liners for managing files.

Rename multiple files:

for i in *.avi
do
j=`echo $i | sed 's/find/replace/g'`
mv "$i" "$j"
done

Can also be written on a single line as

for i in *.avi; do j=`echo $i | sed 's/find/replace/g'`; mv "$i" "$j"; done

Finding Multiple Files in a directory
Find and count how many files were modified more than 365 days ago:

find . -mtime +356 | wc -l

Find and delete files that were modified more than 365 days ago:

find . -mtime +365 -exec rm {} \;

Or move them to a different directory:

find . -mtime +365 -exec mv {} /path/to/destination/ \;

Bulk rename some files:

for file in *.pdf; do mv $file `echo $file | sed 's/\(.*\)2011-09-29\(.*\)/\12011-10-01\2/'`; done

Technology:

Recent Updates

  • 2 years 16 hours ago
  • 2 years 16 hours ago
  • 2 years 3 days ago
    php 8.x
  • 2 years 4 days ago
    10.6.7
  • 2 years 5 days ago
    Drop Centos 5/6 stuff