2

I want to remove all the files in the directory I am in currently in that were created today.

So if today is April 3rd then the UNIX commond I am asking for would remove someFile2, someFile3, someFile4 and someFile5. This command would not remove someFile1.

someUser@:/someDirectory$ ls -lrt
-rw-r--r--    1 someUser someUser       4242 Mar 30 12:12 someFile1.dat
-rw-r--r--    1 someUser someUser          0 Apr 03 12:12 someFile2.dat
-rw-r--r--    1 someUser someUser   42424242 Apr 03 12:12 someFile3.dat
-rw-r--r--    1 someUser someUser          0 Apr 03 12:12 someFile4.dat
-rw-r--r--    1 someUser someUser     424242 Apr 03 12:12 someFile5.dat

My OS is AIX.

Currently, I am running the following four commands over and over in order to achieve this and this takes to much time because I am testing and need to remove this files often. The filenames are always different (timestamps are used in the real file names).

rm someFile2.dat
rm someFile3.dat
rm someFile4.dat
rm someFile5.dat
1
  • You may get a better answer at Unix.SE
    – warren
    Apr 3, 2014 at 19:34

2 Answers 2

1

You can take two different approaches in answering this question:

  • You say the file names contain a timestamp. You can remove files from the directory by using a combination of wildcards and the timestamp: $ TODAYDATE=`date '+%Y%m%d'` $ rm *${TODAYDATE}.dat

  • You could use find with the -newer option (you have to use the newer option, as AIX's built-in find doesn't have that much options): First create a file which has the begin time from when you want to search as mtime: $ TODAYDATE=`date '+%Y%m%d'` $ touch -m -t "${TODAYDATE}0000" /tmp/time_marker Find the files which are newer than that file $ find . -type f -newer /tmp/time_marker -exec rm {} \;

  • You could also use simpler version of find: $ find . -type f -ctime -1 -exec rm {} \;

This will find all files made in the last 24 hours. Useful if you run a cron job around 23:59 each day. However, it doesn't exactly answer your question.

2
  • 1
    This will work in my personal case rm *20140403* but what if the filenames did not contain the timestamp? I will accept your answer if no better answer comes in. Thanks! Apr 3, 2014 at 20:23
  • Well, you said in the question that the files contain a timestamp. In the 2nd bullet I explain how to find files that don't have a timestamp in their name.
    – mtak
    Apr 3, 2014 at 20:24
1

If you can use GNU findutils on the machine then the best option is this command to delete files in the current directory:

find -maxdepth 1 -daystart -ctime 0 -type f -delete

This command will show you what will be deleted:

find -depth -maxdepth 1 -daystart -ctime 0 -type f -exec ls -cld {} +

Being limited to the find command from AIX you can easily delete files from the last 24 hours:

find -maxdepth 1 -ctime 0 -type f -exec rm {} \;

Unfortunately the find utility in AIX does not have the -daystart option which causes the times to be compared to the day boundary instead of the current time.

Also in fact ctime is the inode modification time (not creation time) but it is the closest aproximation of the creation time in standard UNIX file attributes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .