File Commands

From rbachwiki
Revision as of 15:24, 24 July 2020 by Bacchas (talk | contribs) (Created page with "== Find files x days old and delete them== #!/bin/bash find /path/to/files/ -type f -name '*.jpg' -mtime +10 -exec mv {} /path/to/archive/ \; find /path/to/archive/ -type f...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Find files x days old and delete them

#!/bin/bash
find /path/to/files/ -type f -name '*.jpg' -mtime +10 -exec mv {} /path/to/archive/ \;
find /path/to/archive/ -type f -name '*.jpg' -mtime +30 -exec rm {} \;

Find files and rsync them to a dir

find . -type f -name '*.jpg' -exec rsync -zavhP {} /dirctorytocopyto/ \;

Find Multiple filenames

find . -type f \( -name "*.nef" -o -name "*.jpg" -o -name "*.tif" \)

RSYNC copy directories with spaces

find . -type f -name '*177*' -exec rsync -zavP {} "/var/tmp/my dir" \;

Count Files in Directory

cd /var/www/

count=`ls-l *.sh | wc -l 2> /dev/null`
if [ $count -ge 1 ]; then
mv *.sql /var/www/backups/dirname
fi

Find and delete files with certain names

find -type -f -name '*one*' -exec rm {} \;

Find specific files and copy them to another folder

find -type f -name "*app1*" -exec cp {} /var/www/test/ \;

Rename files adding the creation date as prefix

Get the date from the file

date -r $(stat -f %B filename.jpg) +%Y-%m-%d