The command GNU find
is the way to go. For example, to delete all files in the current directory between 1 and 5 august, you can use the following command
find . -maxdepth 1 -type f -newermt 2011-08-01 ! -newermt 2011-08-06 -delete
It is better to execute the command without the -delete
action, first, to see the listing of interested files (a good substitute could be -ls
that produce an ls-like listing).
Removing the -maxdepth 1
specification will traverse all subdirectories, too.
You can also specify hours, for example
find . -maxdepth 1 -type f -newermt '2011-08-01 10:01:59' \
! -newermt '2011-08-06 23:01:00' -delete
Be warned to not remove single quotes, that protect spaces between date and time.
The character !
is a negation, it should be read: newer that this date but not newer that this other date.
To remove everything in a directory without removing the directory, type in:
rm -rfv dontDeleteMe/*
Please note, the /*
part is very important. If you put a space before the *
, it will delete all your files in your current directory.
Also, be very careful playing with rm
, -r
and *
all in the same command. They can be a disastrous combination.
Update: Okay, I realized if you do have hidden/dot files [filenames with dots at the beginning, e.x. .hidden
] then this will leave those files intact.
So really, the simplest solution to the original question is:
rm -rfv dontDeleteMe && mkdir dontDeleteMe
Another one would be to use find
's -exec
option or pipe to xargs
(below):
find dontDeleteMe/* -print0 | xargs -0 rm -rv
Best Answer
Linux doesn't keep record of creation time, there are only 3 time records for files: last access, last modification of contents and last modification of the inode. So you are left with 3 options:
To delete all files modified before 04/29/2018 at 4:00 pm:
To delete all files accessed before 04/29/2018 at 4:00 pm:
To delete all files which had their permission changed before 04/29/2018 at 4:00 pm:
You probably wouldn't want to run the above commands as
root
, and remember to backup any important files.Important note!
You should treat date values with caution. Even though I did a complete format to my hard drive last month, I have some files in my home directory dating back to 2014!
Sources: [1][2][3]