delete - UNIX command for deleting / removing files on today's date

08
2014-07
  • javaPlease42

    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
    
  • Answers
  • mtak

    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.

  • pabouk

    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.


  • Related Question

    bash - Why can't certain files on my external drive be deleted with Unix utilities?
  • jackthecoiner

    I have an external drive (WD My Book World) mounted on my Ubuntu system which I've used for backups over the last few years. Some of the backups included SVN repositories which now include files which I can't appear to delete. I can mv them and view their contents, but that seems to be it. My question is: how can I remove these files from this external drive?

    Here's a sample of what I'm dealing with:

    root@zodiac:/mnt/mybook# mount | grep mybook
    //192.168.1.4/PUBLIC on /mnt/mybook type cifs (rw,mand)
    
    root@zodiac:/mnt/mybook# ls -lh entries 
    -r-xr--r-- 1 www-data www-data 1.2K 2008-03-26 21:29 entries
    
    root@zodiac:/mnt/mybook# file entries 
    entries: ASCII text
    
    root@zodiac:/mnt/mybook# head -n5 entries 
    8
    
    dir
    1
    file:///home/svn/gt_data
    
    root@zodiac:/mnt/mybook# stat entries 
      File: `entries'
      Size: 1201        Blocks: 8          IO Block: 16384  regular file
    Device: 18h/24d Inode: 2149510     Links: 1
    Access: (0544/-r-xr--r--)  Uid: (   33/www-data)   Gid: (   33/www-data)
    Access: 2008-03-26 21:29:34.000000000 -0700
    Modify: 2008-03-26 21:29:34.000000000 -0700
    Change: 2010-01-09 08:59:06.000000000 -0800
    
    root@zodiac:/mnt/mybook# rm entries 
    rm: cannot remove `entries': No such file or directory
    
    root@zodiac:/mnt/mybook# cat > entries 
    -su: entries: No such file or directory
    
    root@zodiac:/mnt/mybook# lsattr entries
    lsattr: Inappropriate ioctl for device While reading flags on entries
    

    @Tobu,

    root@zodiac:/mnt/mybook# grep mybook /proc/self/mountinfo
    33 18 0:24 / /mnt/mybook rw - cifs //192.168.1.4/PUBLIC rw,mand,unc=\\192.168.1.4\PUBLIC,username=root,posixpaths,acl,rsize=16384,wsize=57344
    

  • Related Answers
  • Jakob Borg

    The errors indicate that the file system is corrupted. This might be from not unpluggingn the disk correctly, or it might be defective. You need to run a file system checker (fsck) on the partition or reformat it and see if it is reliable after that.

  • user23307

    Looks like a server configuration error. The fact that the share name is PUBLIC is a red flag. I wouldn't necessarily expect a PUBLIC share to be writable.