R
G

Back to list2023-238

Handle existing files when unpacking files on Linux

Imagine you're faced with the task of unpacking a large number of zip files. Instead of unpacking each of them separately you just feed your computer a single command line:

find -maxdepth 1 -iname "*.zip" -exec unzip {} \;

This would find all zip files in the current directory and feed it as the parameter to the unzip command.

As the computer starts to inflate your data you attend other things. It'll likely take many hours so why bother hanging around? And then it happens. You're gone and the steady stream of unzip status messages rushing across your terminal stops. An existing file with the same name was detected. What should be done?

For hours the computer just sits there waiting for input wasting valuable time.

What you need is a way to handle these incidents automatically. Here's a few ways to do that so that your next large unarchiving session goes through uninterrupted.

Zip

  • unzip -n <FILE> will skip all existing files. You can rest assured that none of your existing data will be deleted or overwritten.
  • unzip -o <FILE> will overwrite all existing files. Very handy if you're updating something.

7Zip

  • 7z x -aos <FILE> will skip existing files. Notice the s at the end
  • 7z x -aoa <FILE> will overwrite existing files. The a at the end makes all the difference.

Tar

  • tar xf <FILE> --skip-old-files - will skip existing files.
  • tar xf <FILE> --overwrite - will overwrite existing files.
  • tar xf <FILE> --overwrite --unlink-first - will first delete existing files and then put the new one in it's place instead. Can be shortened using -U

Rar

  • unrar x -o- <FILE> or unrar x --skip <FILE> - will skip existing files.
  • unrar x -o+ <FILE> or unrar x --overwrite <FILE> - will overwrite existing files.