Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sometimes I will do a command such as

unzip tons_of_files.zip

And I will forget to put a -d to point to a subdirectory. This causes the current folder to get filled with tons of files that are intermixed with the existing files.

What is the best way to remove all these new files and/or move them to a new directory? I want to avoid having to manually examine the directory and determine if the file was part of the archive or was already present.

share|improve this question
add comment

3 Answers 3

A quick way might be the following:

unzip -qql tons_of_files.zip | while read -r l d t n ; do rm -fr "$n" ; done

As this does automatic erasures: use it at your own risk. To see a preview replace ... rm ... with ... echo rm ....

share|improve this answer
add comment

If you extracted the files in a directory where you hadn't modified or moved any files for a few minutes before the extraction, you can tell the extracted files by their very recent ctime. This isn't perfect (if a directory contains an extracted file, you can't tell whether it was created by unzipping or it existed before (perhaps being empty)) but works reasonably well in practice.

The following GNU find command moves files and directories in the current directory whose ctime is less than 2 minutes ago to /other/dir. ls -lctr may help find a suitable cutoff time.

find . -mindepth 1 -maxdepth 1 -cmin -2 -exec mv {} /other/dir \;

Zsh equivalent:

mv *(cm-3) /other/directory

The following GNU find command moves files with a similarly recent ctime in the directory tree rooted at the current directory to a similar path under /other/dir.

find . -mindepth 1 -cmin -2 -type f -exec sh -c '
    for x; do
      mkdir -p "$0/${x%/*}"
      mv "$x" "$0/$x"
    done
  ' /other/dir {} +

Zsh equivalent (almost: this one reproduces the entire directory hierarchy, not just the directories that will contain files):

autoload zmv
mkdir -p ./**/*(/cm-3:s"|.|/other/dir|")
zmv -Q '(**/)(*)(.cm-3)' /other/dir/'$1$2'
share|improve this answer
add comment

$ for i in `unzip -qql tons_of_files.zip | tr -s " "| cut -d" " -f 5 | sed 's/^.*\///'`; do echo $i; done

Change echo to rm.

What it is doing (inside the backticks):

  1. list files (one-bye-one)
  2. squeeze spaces
  3. select 5th field
  4. delete everything that is folder name
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.