16

Is there a way (preferrable via GUI, but may be via command line) to extract a zip file and delete the zip after extracted, all in a single command?

(I remember I saw someone doing something like this in the command line one day)

The Student
  • 11,926

3 Answers3

14

For a GUI I'd say the easiest way is a nautilus script. The main line of which would be:

unzip "$item" && trash "$item"

It works in bash/dash just as easy. In nautilus the whole thing would look like this:

unzip delete nautilus script

#!/bin/bash
# Nautilus script to unzip and then remove a zip archive.
# Nautilus script usually go in "$HOME/.gnome2/nautilus-scripts"

IFS='
'
for item in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do
    if [ -n "$(file -b "$item" | grep -o 'Zip')" ]; then
        unzip "$item" && trash "$item"
        # use trash instead of rm to move them to trash
        # (trash-cli package installed)
    fi
done
con-f-use
  • 18,813
  • In case we have a memory storage issue, how can we unzip a huge zip file and erase the unzipped parts at the same time to avert a memory overflow ? – YellowishLight Dec 17 '20 at 11:09
11

You could simply write a bash script. It will look something like this:

unzip $1 && rm $1

where $1 is the argument with a value of your zip file's filename. Then alias unzip command in ~/.bashrc file to run this script. And after typing in terminal:

unzip test.zip

you will get:

unzip test.zip && rm test.zip
Mateusz
  • 111
4

It's pretty easy through a shell command:

unzip <filename>.zip && rm <filename>.zip

Perhaps, if you're using nautilus, you could create a relevant nautilus-action in order to automate the command through a GUI selection.

Pavlos G.
  • 8,844
  • 1
    yeah, actually these are 2 commands in a single line.. I'm really trying to remember the single command I saw someone use.. – The Student Aug 17 '11 at 13:45
  • I don't see how it matters whether it's one or two command. You sure he did use an alias or was in fact compressing and used tar --delete-files? – con-f-use Aug 17 '11 at 14:14
  • It matters if you are going to use the command as entrypoint for a container. – glaux Oct 12 '23 at 04:46