Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sometimes I will do a command such as


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

A quick way might be the following:

unzip -qql | 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

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"
  ' /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

$ for i in `unzip -qql | 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

Your Answer


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.