how to delete the files that created on Aug 7 with the name DBG_A_sql* under /tmp

as the following example:

-rw-r--r-- 1 root    root   51091 Aug  7 11:22 DBG_A_sql.2135
-rw-r--r-- 1 root    root   15283 Aug  7 11:22 DBG_A_sql.2373
-rw-r--r-- 1 root    root   51091 Aug  7 11:22 DBG_A_sql.2278
-rw-r--r-- 1 root    root    9103 Aug  7 11:22 DBG_A_sql.2485
-rw-r--r-- 1 root    root    9116 Aug  7 11:22 DBG_A_sql.2573
-rw-r--r-- 1 root    root    9140 Aug  7 11:22 DBG_A_sql.2679
-rw-r--r-- 1 root    root   15695 Aug  7 11:22 DBG_A_sql.2897

4 Answers 4


You can use find. Calculate date according to your requirement and use,

find /tmp -maxdepth 1 -mtime -1 -type f -name "DBG_A_sql*" -print

After confirming it delete them,

find /tmp -maxdepth 1 -mtime -1 -type f -name "DBG_A_sql*" -delete
  • 1
    -newerct "08-07" -not -newerct "08-08" if you'd like because ctime is a difference between file date and now.
    – Costas
    Aug 8, 2016 at 8:53
  • find / -newerct 'Aug 7' -print | grep filename or find / -newerct "08-07" -print | grep filename I think he meant this.
    – AReddy
    Aug 8, 2016 at 9:45
  • 2
    Note that -newrct and -ctime have nothing to do with creation time. They're for change time. Aug 8, 2016 at 10:17
  • You can't pass the output of ls -l to xargs like that, that doesn't make any sense. (note that ls -l doesn't output the creation time either, your find approach was a lot better than that, only you'd probably want -mtime/-newermt) Aug 8, 2016 at 10:32
  • -mtime -8 is for files last modified within the last 8 days. Aug 8, 2016 at 10:43

For a start, finding files created on a certain time is a bit hard, since the creation time isn't usually saved anywhere or is hard to get at. What you have is mtime, or the last modification time, and ctime which is the "change" time, updated on any changes to the inode. I'll assume you want the modification time.

Finding files modified on a given date turned out to be mildly interesting, since find appears to make it a bit hard to get it right with files created on exactly midnight.

If we know the relative time (i.e. it was yesterday), we could use find -daystart -mtime 1, but it finds the file modified on the wrong midnight, Aug 8 00:00. However, this seems to work:

find dir/ -daystart -mtime +0 \! -mtime +1 -ls

If don't want to calculate the relative time, and your find has -newerXY:

find dir/ -newermt 'Aug 7 00:00' \! -newermt 'Aug 8 00:00' -ls

Again, this gets the files created exactly on midnight wrong, because the comparison is "newer", not "newer or as old as". Though if your system has subsecond precision for timestamps, it might be hard to hit that, but happens if you test with files created by touch...

A hairy workaround to that would be something like this:

find dir/ -newermt 'Aug 6 23:59:59.999999999' \! -newermt 'Aug 7 23:59:59.999999999' -ls

In any case, add the necessary -name "DBG_A_sql*" to only take the files with the correct name. You can replace the -ls at the end with -delete to delete the files instead of listing. (-ls, -delete and -newerXY exist at least in GNU find and the BSD find on OS X.)

Of course you could actually parse the text representation of the date, but ls makes it hard to get right if some joker creates files with unprintable characters in them. Sure, the example files don't have any such, but in general, anyone could create them, especially in /tmp.

(Though with | xargs rm you'd just miss those files, and since file names can't contain slashes, it would be hard for anyone to point your rm to another directory.)

  • creation time or birth time is often stored nowadays, but on some systems like Linux-based ones, hard to retrieve. Aug 8, 2016 at 10:26

Just bash loop

for file in /tmp/DBG_A_sql* ; do
    [ "$(date -I -r "$file")" == "2016-08-07" ] && rm "$file"
  • 1
    How is that pure bash? bash has no date or rm builtin as far as I know. Note that it implies the GNU implementation of date. See zsh or ksh93 for shells that can do it with only builtins (though I wouldn't see the point here). Aug 8, 2016 at 10:29
  • 1
    @StéphaneChazelas. Agree. Have edited.
    – Costas
    Aug 8, 2016 at 17:25

Assuming that by created, you mean last modified (which is the time reported by ls -l), with zsh:

autoload age # best in ~/.zshrc
rm /tmp/DBG_A_sql*(e:age 2016-08-07:)

You must log in to answer this question.

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