The following examples illustrate typical uses of the command zip for packaging a set of files into an “archive” file, also called “zip file”. The command uses the standard zip file format. The archive files can therefore be used to tranfer files and directories between commonly used operating systems.
zip -r myfile.zip ./* # archive all the files and folders in current directory # into myfile.zip －r is short for recurse zip -d myfile.zip smart.txt # delete smart.txt from myfile.zip zip -m myfile.zip ./rpm_info.txt # add rpm_info.txt into myfile.zip zip -r filename.zip file1 file2 file3 /usr/work/school # archive file1 file2 file3 and /usr/work/school into filename.zip
unzip -o -d /home/sunny myfile.zip # extract myfile.zip to /home/sunny/ # -o: overwrite files WITHOUT prompting # -d: extract files into exdir
On Unix platform, tar command is the primary archiving utility. Understanding various tar command options will help you master the archive file manipulation.
In this article, let us review various tar examples including how to create tar archives (with gzip and bzip compression), extract a single file or directory, view tar archive contents, validate the integrity of tar archives, finding out the difference between tar archive and file system, estimate the size of the tar archives before creating it etc.
Note: thegeekstuff.com is Out of the WALL.
1. Creating an archive using tar command
$ tar cvf archive_name.tar dirname/ $ tar cvzf archive_name.tar.gz dirname/ # .tgz is same as .tar.gz $ tar cvfj archive_name.tar.bz2 dirname/ # .tbz and .tb2 is same as .tar.bz2
gzip vs bzip2: bzip2 takes more time to compress and decompress than gzip. bzip2 archival size is less than gzip.
In the above command:
- c – create a new archive
- v – verbosely list files which are processed.
- f – following is the archive file name
- z – filter the archive through gzip
- j – filter the archive through bzip2
2. Extracting (untar) an archive using tar command
$ tar xvf archive_name.tar $ tar xvfz archive_name.tar.gz $ tar xvfj archive_name.tar.bz2
- x – extract files from archive
Note: In all the above commands v is optional, which lists the file being processed.
3. Listing an archive using tar command
$ tar tvf archive_name.tar $ tar tvfz archive_name.tar.gz $ tar tvfj archive_name.tar.bz2
4. Extract a single file from tar, tar.gz, tar.bz2 file
$ tar xvf archive_file.tar /path/to/file $ tar xvfz archive_file.tar.gz /path/to/file $ tar xvfj archive_file.tar.bz2 /path/to/file
5. Extract a single directory from tar, tar.gz, tar.bz2 file
$ tar xvf archive_file.tar /path/to/dir/ $ tar xvf archive_file.tar /path/to/dir1/ /path/to/dir2/ $ tar xvfz archive_file.tar.gz /path/to/dir/ $ tar xvfj archive_file.tar.bz2 /path/to/dir/
6. Extract group of files using regular expression
$ tar xvf archive_file.tar --wildcards '*.pl'
7. Adding a file or directory to an existing archive using option -r
$ tar rvf archive_name.tar newfile $ tar rvf archive_name.tar newdir/
Note: You cannot add file or directory to a compressed archive. If you try to do so, you will get “tar: Cannot update compressed archives” error as shown below.
$ tar rvfz archive_name.tgz newfile tar: Cannot update compressed archives Try 'tar --help' or 'tar --usage' for more information.