On recent versions of find
(e.g. GNU 4.4.0) you can use the -newermt
option. For example, to find all files that have been modified on the 2011-02-08
$ find /var/www/html/dir/ -type f -name "*.php" -newermt 2011-02-08 ! -newermt 2011-02-09
Also note that you don't need to pipe into grep to find php files because find can do that for you in the -name
option.
Take a look at this SO answer for more suggestions: How to use 'find' to search for files created on a specific date?
I found this example on SO, titled: Terminal - Delete All Folders Not Conatining .mp3 Files.
#! /bin/bash
find -depth -type d | while read -r D
do
v=$(find "$D" -iname '*.mp3')
case "$v" in
"" )
echo "$D no mp3"
# rm -fr "$D" #uncomment to use
;;
esac
done
Example
Sample data.
.
|-- 1
| |-- 1.mp3
| `-- 1.txt
|-- 2
| `-- 2.mp3
|-- 3
| `-- 3.txt
|-- 4
| `-- 4.txt
|-- 5
| `-- 5.mp3
|-- 6
| `-- 61
| `-- 61.mp3
|-- 7
| `-- 71
| `-- 71.txt
`-- deletenomp3.bash
Sample run
If I were to run it it would delete the following:
$ ./deletenomp3.bash
./7 no mp3
./7/71 no mp3
./4 no mp3
./3 no mp3
Other file types
You can simply extend this by adding more -name
arguments to the 2nd find command in the script. Example, to add .wav
files:
v=$(find "$D" -iname '*.mp3' -o -iname '*.wav');
That says *.mp3
OR *.wav
. To add more or others:
v=$(find "$D" -iname '*.mp3' -o -iname '*.flac' -o -iname '*.m4a');
I'm sure this block could be condensed if you had a lot more file types using alternative switching to find
.
Best Answer
This will delete all files older than 5 days, you can put a
-name '*log'
in there too to be more precise and you might want to specify amaxdepth
in the find command too.find /some/dir -type f -mtime +5 -delete