20

I guess in most cases, when extracting a tar archive, we will get a directory with the same name as the archive file but different suffix. But in some unlucky case, as I met today, after extracting a tarball, I find lots of files spread in the working directory, which is a nuisance.

So what I want to learn from you is that - how can I move those newly created files? I know it should be some "find plus rm" fancy approach there, but I don't know exactly how.

3 Answers 3

39

I wonder what man find would say. Then you can even show your efforts.

find . -type f -cmin -1
find . -type f -cmin -1 -delete
  • The -type f is used to specify only regular files are selected.
  • The -cmin -1 the file's status was last changed in less than the past (one) minute.
  • The -delete flag tells find to deletes all your electronics files in the known universe or something like that, so use caution when using it.

First doing a test run without the -delete flag is strongly recommended.

Note -delete is a non-portable extension.

I recommend reading the find manpage before using this, because it covers various gotchas and warnings that may apply in your situation.

2
  • Nice! -delete looks easier than -exec rm {} \;
    – PJ Brunet
    Oct 9, 2013 at 11:56
  • +1 for taking the time to explain the option choices
    – Jad S
    Jan 16, 2017 at 14:33
5
find . -mtime -60s -exec mv {} /dest/dir \;

Finds every file in the current directory (and sub directories) that has been modified in the last 60 seconds and move them to /dest/dir.

2
  • 3
    Careful, extracted archives files may preserve original modification date. Best to use creation time. Jan 6, 2015 at 8:45
  • @CemKalyoncu great point.
    – skarface
    Jan 23, 2015 at 4:24
0

The easies way (provided you don't have graphical interface) will be to use mc. It can sort files by date.

You can also delete the files which were in the tar from command line:

> rm $( tar -ztf mytar.tar.gz )

(-ztf) is for tar.gz files. You can use (-tf) for normal tars, or (-jtf) for tar.bz2 files.

If the list of the files is too big, and the above command chokes, you can use xargs:

> tar -ztf mytar.tar.gz | xargs rm
1
  • wow, my bad - I did not read the question really - you need to move them, not delete (pls, fix the title, its misleading). I guess you still can delete them, then create a new dir, and untar there :). After all, move = copy + delete.
    – Sunny
    Mar 4, 2010 at 17:27

You must log in to answer this question.

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