Search code examples
linuxrm

How to delete all files that were recently created in a directory in linux?


I untarred something into a directory that already contained a lot of things. I wanted to untar into a separate directory instead. Now there are too many files to distinguish between. However the files that I have untarred have been created just now (right ?) and the original files haven’t been modified for long (at least a day). Is there a way to delete just these untarred files based on their creation information ?


Solution

  • Tar usually restores file timestamps, so filtering by time is not likely to work.

    If you still have the tar file, you can use it to delete what you unpacked with something like:

    tar tf file.tar --quoting-style=shell-always |xargs rm -i
    

    The above will work in most cases, but not all (filenames that have a carriage return in them will break it), so be careful.

    You could remove the directories by adding -r to that, but it's probably safer to just remove the toplevel directories manually.