Linux unzip all files in directory

Unzip files in particular directory or folder under Linux or UNIX

Q. I’m new to Linux command line. I’ve a command called unzip and a file called package.zip. I can extract file using command:
unzip package.zip

All files are extracted into current directory (it is making a directory called package). I want all files and subdirectory extracted into /opt directory. How do I extract files in /opt?

A. unzip command will list, test, or extract files from a ZIP archive, commonly found on MS-DOS systems. The default behavior (with no options) is to extract into the current
directory (and subdirectories below it) all files from the specified ZIP archive.

By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the directory). This option need not appear at the end of the command line; it is also accepted before the zipfile specification (with the normal options), immediately after the zipfile specification, or between the file(s) and the -x option. The option and directory may be concatenated without any white space between them, but note that this may cause normal shell behavior to be suppressed.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

For example extract package.zip into /opt, enter:
# unzip package.zip -d /opt
# cd /opt
# ls

If you want to rename package directory use mv command:
# mv package newname

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to extract tar.xz files in Linux and unzip all files

H ow do I extract tar.xz files in Linux? Can you tell me command to unzip linux-5.6.13.tar.xz file? How can I uncompressing (or decompressing) all files in the current directory?

The tar command and xz command provides support for extracting and uncompressing tar.xz files and .txz archives under Linux operating systems.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements tar and xz commands on Linux
Est. reading time 6 minutes

How do I extract tar.xz files in Linux?

  1. Install xz using the dnf install xz on a CentOS/RHEL/Fedora Linux.
  2. Debian/Ubuntu Linux users try apt install xz-utils command.
  3. Extract tar.xz using the tar -xf backup.tar.xz command
  4. To decompress filename.tar.xz file run: xz -d -v filename.tar.xz

Installation

XZ is a set of open-source software for lossless data compressors, including LZMA and xz formats. These formats are popular among open source developers and projects due to higher compression rates than alternatives tools like gzip and bzip2. The tar command works if xz installed on the system. Hence, we must install it.

Debin/Ubuntu Linux install xz

Use the apt command/apt-get command:
$ sudo apt install xz-utils
Sample outputs:

CentOS/RHEL/Fedora Linux install xz

Open the terminal app and then run NA command/dnf command as follows:
$ sudo dnf install xz
## On an older version of CentOS/RHEL try yum ##
$ sudo yum install xz
From my CentOS 6.x box:

Installing xz on OpenSUSE/SUSE Enterprise Linux

We can use the zypper command to install xz:
$ sudo zypper install xz

OpenSUSE installing xz package for extraction purposes

Examples

Now we install xz. We can now directly use the tar command as follows for extracting a file named linux-5.6.13.tar.xz:
$ tar -xf linux-5.6.13.tar.xz
$ tar -xvf linux-5.6.13.tar.xz
$ tar -Jxvf linux-5.6.13.tar.xz
## extract tar.xz files aka .txz file ##
$ tar —xz -xf archive.txz

Extracting specific files from a tar.xz file

We extract a given file names from a backup.tar.xz file by using the following syntax:
$ tar -xf backup.tar.xz resume.pdf
$ tar -xf backup.tar.xz babys-3rd-cake-day.jpg sales.txt
Sometimes we don’t know file names in advance. Hence, we list the contents of an archive by passing the -t as follows and then unzip tar.xz files:
$ tar -tvf backup.tar.xz
## filter out tar listing option using the grep command/egrep command ##
$ tar -tvf backup.tar.xz | grep file1
$ tar -tvf backup.tar.xz | egrep ‘ file.txt | data.doc | cake.jpg ‘

Want to extract all Perl or Python source code files? Try the following option:
$ tar —wildcards ‘*.py’ -xvf webroot.tar.xz
$ tar —wildcards ‘*.pl’ -xvf centos-7-sysadmin-scripts.txz

Understanding tar command options

  • -x : Extract/get/unzip files from an archive.
  • -f archive.tar.xz : Use this archive file or device archive for extracting files
  • -J OR —xz : Filter the archive through xz command. Hence, we install xz using package manager.
  • -v : Verbose. Show progress.
  • -t : List file stored inside .tar.xz/.xz archive.
  • —wildcards : By default, wildcards don’t work as they treated or processed to by your current shell. Therefore, to extract files, use the —wildcards options. For example, extract all .webp images I would pass —wildcards ‘*.webp’ option to the tar.

How to extract .xz files using the xz command

So far, we learned to use tar for extracting. But, we can use xz command directly for as compressor and decompressor too. Tar internally calls xz itself or using API provided by libs. Let us see some tips and tricks for xz command.

Decompress .tar.xz file

$ xz —decompress filename.tar.xz
$ xz -v -d archives.tar.xz
Decompress file named mysql.sql.xz and replaced by mysql.sql:
$ xz -v -d mysql.sql.xz

Compress tarball as .tar.xz file

Compress a file named mysql.sql and replace it by mysql.sql.xz for saving disk space:
$ xz mysql.sql
The syntax is as follows for compressing per-existing tarball:
$ xz -v -z filename.tar
Alternatively, use the tar command for convenience purposes
$ tar -cv J f file1 file2
$ tar -cv J f /path/to/dir1 ./dir2
$ sudo tar -cv J f /efs/backups/14-05-2020/production-webroot.tar.xz /var/www/
$ sudo tar —xz -cvf /efs/backups/aws-us-west-www-prod-42-etc.txz /etc/

Gif 01. xz in action

Compress from stdin

$ mysqldump nixcraft-blog | xz > nixcraft-blog.sql.xz
Of coruse we can uncompress and send content to stdout for the mysql command too
$ xzcat nixcraft-blog.sql.xz | mysql nixcraft-blog
Let us uncompress tar directory from stdin sent from the nc, enter (assuming that target server-ip protected by VPN else use the ssh command):
$ nc -l -p 4242 | tar -xJvf —
On other side compress directory to stdout and send to the nc, run:
$ tar -cJvz — /var/www/html | nc server-ip-here 4242
Our final example use ssh instead of nc:
$ tar —xz -cf — /path/to/dir | ssh vivek@server1.cyberciti.biz «cat > filename.txz»
See “How To Use tar Command Through Network Over SSH Session” for more info.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Conclusion

This quick tutorial explained how to extract and unzip popular archive formats such as .tar.xz and .txz under Linux operating systems. Remember, tar command only works if you install the xz command via the package manager. See the xz project home page and docs here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How do I unzip multiple / many files under Linux?

The problem with multiple zip files on Linux

Assuming that you have four file in a /disk2/images/ directory as follows:

Let us verify it with the ls command:
$ ls
Sample outputs:

To unzip all files, enter:
$ unzip *.zip
Sample outputs:

Above error indicate that you used the unzip command wrongly. It means extract invoices.zip, pictures.zip, and visit.zip files from inside the data.zip archive. Your shell expands the command ‘unzip *.zip’ it as follows:
unzip data.zip invoices.zip pictures.zip visit.zip
The solution is pretty simple when you want to unzip the file using the wild card; you have two options as follows.

#1: Unzip Multiple Files Using Single Quote (short version)

The syntax is as follows to unzip multiple files from Linux command line:

Type the following command as follows:
$ cd /disk2/images/
$ unzip ‘*.zip’
$ ls -l

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Note: *.zip is put in between two single quotes so that shell will not recognize it as a wild card character.

#2: Unzip Multiple Files from Linux Command Line Using Shell For Loop (Long Version)

See also

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

# If files are password protected, and we are not sure which password

for Z_FILE in *.zip; do
for PASSWD in [ pass123, PASS123, abc123, ABC123 ]; do
unzip -P $PASSWD $Z_FILE;
if [ $? = 0 ]; then # successful unzip
break
fi
done
done

This is an excellent hack.

Appreciate your post.

> Appreciate your post.

No probs, copyleft (c) 🙂

Actually I’ve seen yours:

$ for z in *.zip; do unzip $z; done

But, it was not enough for me, ’cause we have password protected ones… So, can say that idea comes from you 🙂 I just added a new feature (validation)…

I think it should/can be improved further to check for other exit status codes…

Maybe later some day…

I used to use for i in *.zip ; do unzip $i ; done as well but then I found out about the following command and use it all the time now.

escape the asterik and you are good to go. 🙂

I tried unzip -P $PASSWD $Z_FILE; command but it is not working and for same zip file it is working in US with same password. I read in one of the web sites that, non USA system needs to install a patch for running above command. If yes Please let me know where can I get this patch else please let me know how to run this command.

Thanks, just what I needed! Very straitghtforward. +1 virtual cookie for you.

If you have spaces in your filenames, you can also use the following:

for z in *.zip; do unzip “$z”; done

find -name \*.zip -exec unzip <> \;

find -name \*.zip | xargs -t -i unzip <>

If you like to extract multiple tar or tar.gz use the following command
for f in *.tar.gz; do tar zxf $f; done

Thanks all for sharing, very useful information about Unzip.

Question: and if I want do delete a file in a batch of different compressed archives? Lets say all copies of “info.txt” or “logo.jpg” in a.zip, b.zip, c.zip(…) z.zip, etc. There is a way to do it?

excellent post …. short method is really awesome..

Exactly what I was looking for! THANK YOU!

Time saver, life saver! Thank you!

thanks a lot …..4 help

if u want to display some msg then…

Thanks everyone for the answers. If space is an issue, you can do something like (from some dude) to remove the archive files as they are unpackaged:
for z in *.zip; do unzip $z; rm $z; done

I have 1..10 zip files in one directory …i want to unzip all files at a time..
can any one help how to do that

Thank you very much.

Thank you very much, on my Debian option #2 works very well!

I had a pile of zip that each contained a index.html file and the archive structure had no folder.. They obviously had to be extracted in separate folders so as to not overwrite the so precious files. Since it was a temporary “view and delete” kind of thing and with well over a hundred files (not needing to be unzipped in a specific folder, current folder was just fine), i came up with this;

for z in *.zip; do q=$(echo $z | cut -f 1 -d ‘.’); unzip $z -d ./$q; done;

It basically extracts all the zip files to the current directory, into a folder named after the zip filename. Greatly inspired (if not totally ripped from!) NixCraft and http://stackoverflow.com/questions/12152626/how-can-remove-the-extension-of-a-filename-in-a-shell-script

Couple seconds after i figured “oh well”, i saved that much time so i might as well make something out of it… So instead of just ‘hardcoding’ it into a simple bash alias that doesn’t take parameters, I took a couple more minutes to have it let me at least input a base directory for extraction. Here’s what i came up with (function in .bashrc file);

unzipALL() <
dir=’.’;
[[ ! -z «$<1>» ]] && dir=$1;
for z in *.zip; do
q=$(echo $z | cut -f 1 -d ‘.’);
unzip $z -d $dir/$q;
done;
>

From the command line just use unzipAll to unzip to the current folder or pass it a folder name: unzipAll /tmp extracts all to /tmp/%filename%.

It does perform a single basic check on the passed parameter but nothing too fancy (strip spaces before/after then checks against null). One could spend more time and cutomize it further, adding bad directory check and all that…

I figure i’d share… thanks and hopefuly it can be of use to someone!

I hope i didn’t break any rules posting a url in here, sorry if so heeh

Awesome job and thanks for sharing it with us.

unzip “*.zip” should also be OK.

unzip *.zip does not work in bash.

blah@blahblah:

/tmp-zip$ unzip *.zip
Archive: test1.zip
caution: filename not matched: test2.zip
blah@blahblah:

unzip \*.zip DOES work, still doesn’t solve the problem i faced of multiple same-name files…

blah@blahblah:

/tmp-zip$ unzip \*.zip
[. ]
inflating: index.html
inflating: portfolio.html
inflating: services.html
Archive: test2.zip
replace index.html? [y]es, [n]o, [A]ll, [N]one, [r]ename:

unzip “*.zip” results in the same, btw.

Hence the quick function. If you have a better way please share
Also, while testing you assumptions, i found out that unzip tries to look for .ZIP files on it’s own – pretty neat… unzip \* would work (still not help with the issue…)

I just pulled a lot of files (over 400K) all of them were in zip, i’m trying to make huge archive off free documents at http://uploadcoins.com , so for me worked # unzip \*.zip , all that 400K went ok 🙂 , so again, You can use unzip \*.zip , Good luck

I want a script to extract a particular file from the multiple zip folder.

Источник

Читайте также:  Приоритет приложений windows 10 что это
Оцените статью