Difference between revisions of "File Commands"
Line 15: | Line 15: | ||
== Find Directory sizes == | == Find Directory sizes == | ||
du -h --max-depth=2 | sort -hr | du -h --max-depth=2 | sort -hr | ||
== Search multiple files for a keyword, then list the file names == | |||
find . -type f -name "*.php" -print0 | xargs -I {} -0 grep -r -l "edcas" "{}" | |||
== Find files and rsync them to a dir == | == Find files and rsync them to a dir == | ||
Line 85: | Line 87: | ||
===Rename Prepend Mac Only === | ===Rename Prepend Mac Only === | ||
rename -A 'xxxx-' *.jpg | rename -A 'xxxx-' *.jpg | ||
[[Category:Bash]] |
Revision as of 15:44, 31 July 2020
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 over a certain size
$ find . -type f -size -4G
You can use size switch for other formats, such as
- `c’ for bytes
- ‘w’ for two-byte words
- `k’ for Kilobytes
- `M’ for Megabytes
- `G’ for Gigabytes
Find Directory sizes
du -h --max-depth=2 | sort -hr
Search multiple files for a keyword, then list the file names
find . -type f -name "*.php" -print0 | xargs -I {} -0 grep -r -l "edcas" "{}"
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
Read exif data from file
mdls filename.jpg
find creation date of file in exif data
mdls filename | grep -i kMDItemContentCreationDate -m 1 | awk '{print $3}'
- the m1 will return the 1 result
exiftool filename.jgp
Using rename command
If its not installed in MacOS, use brew to install it
rename 's/dem/rob/g' * jpg
Will change dem and replace it with rob /g is global which will replace all occurrence of dem in the file name
rename 's/\+/_/g' *.tif
use the \ escape character with the + sign
Replace multiple characters
rename 's/[_,-,a,b,c,]//g'
rename '/.jpg/.tif/' *.jpg
Change jpg extension to tif
Deleting part of a filename
rename 's/dem//' *.jpg
Searching with Groupings
rename 's/(dem|rob)ng/bac/' *.jpg
The central expression of this rename command will search for strings within filenames that have the character sequence “dem” or “rob” where those sequences are immediately followed by “ng”. In other words, our search term is going to look for “string” and “demng”. The substitution term is “bacng”.
Example: demng.jpg, robmg.jpg
Changing case of filename
rename 'y/a-z/A-Z/' *.jpg
Use the -n option to print names without renaming them
rename -n 's/.html/.php/' *.html
Rename files adding sequential numbers Mac Only
rename -N 03000 's/find/$N/gi' *.jpg
You have to add the 0 at the beginning otherwise it wont work
Rename Delete string Mac Only
rename -d '0' *.jpg
Result
030020-abc.jpg 30020-abc.jpg
Find and Rename
find . -type f -name '*2019*' -print0 | rename -0 's/2019/2020/gi'
Rename Prepend Mac Only
rename -A 'xxxx-' *.jpg