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.
Using python
:
#!/usr/bin/env python2
import glob, re, os, datetime
os.chdir('/path/to/dir')
for f in glob.glob('*.dat'):
ini_time = datetime.datetime.strptime(re.search(r'(?<=_)(?:\d|_)+(?=.dat$)', f).group(), '%Y_%m_%d_%H%M')
fin_time = (ini_time + datetime.timedelta(minutes=30)).strftime('%Y_%m_%d_%H%M%S')
os.rename(f, 'Filename_' + str(fin_time) + '.dat')
os.chdir('/path/to/dir')
will change the current directory to the directory containing the .dat
files. Replace /path/to/dir
with the actual path.
glob.glob('*.dat')
will find the files ending in .dat
ini_time
variable will at first cut out the date-time from the original file name using re
module and then sort out which entry represents what in the string that is taken out so that we can add the required time to this
fin_time
will contain the resultant time i.e. ini_time
plus 30 minutes
os.rename
will rename the file accordingly.
Also note that, with successive file names (differed by 30 minutes) the renamed file will overwrite the next one, hence it it is better to add the seconds to the renamed file name so that it remains safe. Otherwise you need to save the renamed files to a different directory and then replace them with the original ones later.
Best Answer
As long as you are the owner of the file (or root), you can change the modification time of a file using the
touch
command:By default this will set the file's modification time to the current time, but there are a number of flags, such as the
-d
flag to pick a particular date. So for example, to set a file as being modified two hours before the present, you could use the following:If you want to modify the file relative to its existing modification time instead, the following should do the trick:
If you want to modify a large number of files, you could use the following:
You can change the arguments to
find
to select only the files you are interested in. If you only want to update the file modification times relative to the present time, you can simplify this to:This form isn't possible with the file time relative version because it uses the shell to form the arguments to
touch
.As far as the creation time goes, most Linux file systems do not keep track of this value. There is a
ctime
associated with files, but it tracks when the file metadata was last changed. If the file never has its permissions changed, it might happen to hold the creation time, but this is a coincidence. Explicitly changing the file modification time counts as a metadata change, so will also have the side effect of updating thectime
.