Batch delete files created as a result of unzip with bash

Question: Question:

In this situation

$ pwd
$ ls myzip*

Sometimes I accidentally do this in the hope that it will be unzipped under ~/Desktop/myzip/ :

$ unzip


$ tree
├── extracted_file0
├── extracted_file1
 . ... 
 ... # 元から ~/Desktop にあったファイルたちに混ざってしまっている

It has become a catastrophe, so I want to return to the original situation. If an existing file has already been overwritten, it can be ignored, and how can unzip erase the files that were unintentionally scattered in the directory?

As I have already tried, I found that unzip -l gives text in the following format.

  Length      Date    Time    Name
---------  ---------- -----   ----
      119  2015-01-02 16:05   hashas.hs
      274  2015-01-25 16:05
---------                     -------
      393                     2 files

For the time being, I wrote this to a file, formatted it into a list of file names with vim , and dealt with it with cat files.txt | xargs rm . I would like to know a method that is a little less ad hoc.

Answer: Answer:

The following is not an answer. It is a story about how to prevent such an accident.

Originally, the unzip command will extract the contents of the archive as it is. Therefore, if the files in the archive are not stored in a single directory, an accident like this may occur. However, even if it is contained in a single directory, it is the same if there is a directory with the same name in the current directory. .. ..
For the time being, unzip has a -d option that will extract the contents of the archive to the specified directory. If the specified directory does not exist, it will be created without permission.

$ unzip -d output
$ ls output
c001.txt  c002.txt  c003.txt ...

However, it is tiring to specify the directory every time, and if you forget to specify it, it will still be …

I wondered if there was any command other than unzip that would suit my purpose, and found unar (well, I've been using it for some time).

unar (1)

-d, -force-directory
By default, a directory is created if there is more than one top-level file or folder. By default, a directory is created if there is more than one top-level file or folder .

In the case of this time, it will create a myzip directory and extract it in it (create a directory with the basename of the archive).
There are other options like this.

-r, -force-rename
Always rename files when a file to be unpacked already exists on disk.

For example, if you already have a file called a.txt , it will rename it to a-1.txt .

Please for your reference.

Scroll to Top