Skip to content

Commit c45da83

Browse files
examples for tar and gzip commands
1 parent fabc922 commit c45da83

File tree

1 file changed

+80
-16
lines changed

1 file changed

+80
-16
lines changed

Files_and_Directories.md

+80-16
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,8 @@ $ readlink -f words
634634
```bash
635635
$ touch foo.txt
636636
$ ln foo.txt baz.txt
637+
638+
$ # the -i option gives inode
637639
$ ls -1i foo.txt baz.txt
638640
649140 baz.txt
639641
649140 foo.txt
@@ -652,31 +654,93 @@ $ ls -1i foo.txt baz.txt
652654

653655
## <a name="tar-and-gzip"></a>tar and gzip
654656

655-
`tar` is archiving utility. The archived file is same size as combined sizes of archived files
656-
Usually so often combined with compression utility like `gzip` that there is a way to do it just using the `tar` command.
657+
* `tar` is an archiving utility
658+
* first, lets see an example of creating single archive file from multiple input files
659+
* note that the archive file so created is a new file and doesn't overwrite input files
657660

658-
**Examples**
661+
```bash
662+
$ ls -F
663+
backups/ low_power_adders/ word_lists/ words_ref.txt
659664

660-
Archive and Compression
665+
$ # -c option creates a new archive, existing archive will be overwritten
666+
$ # -f option allows to specify name of archive to be created
667+
$ # rest of the arguments are the files to be archived
668+
$ tar -cf bkp_words.tar word_lists words_ref.txt
661669

662-
* `tar -cvf backup_mar15.tar project results` create backup_mar15.tar of files/directories project and results
663-
* `-v` option stands for verbose, i.e displays all the files and directories being archived
664-
* `gzip backup_mar15.tar` overwrites backup_mar15.tar with backup_mar15.tar.gz, a compressed version
665-
* `tar -cvzf backup_mar15.tar.gz project results` create backup_mar15.tar and overwrite with backup_mar15.tar.gz
670+
$ ls -F
671+
backups/ bkp_words.tar low_power_adders/ word_lists/ words_ref.txt
672+
$ ls -sh bkp_words.tar
673+
2.3M bkp_words.tar
674+
```
666675

667-
Extract archive and Decompression
676+
* once we have an archive, we can compress it using `gzip`
677+
* this will replace the archive file with compressed version, adding a `.gz` suffix
668678

669-
* `gunzip backup_mar15.tar.gz` decompress and overwrite as backup_mar15.tar
670-
* `tar -xvf backup_mar15.tar` extract archived files to current directory
671-
* `tar -xzvf backup_mar15.tar.gz` decompress and extract archived files to current directory
679+
```bash
680+
$ gzip bkp_words.tar
672681

673-
z commands
682+
$ ls -F
683+
backups/ bkp_words.tar.gz low_power_adders/ word_lists/ words_ref.txt
684+
$ ls -sh bkp_words.tar.gz
685+
652K bkp_words.tar.gz
686+
```
674687

675-
* `zcat story.txt.gz` display file contents of compressed file on standard output
676-
* `zless story.txt.gz` display file contents of compressed file one screenful at a time
677-
* There are other commands as well like `zgrep`, `zdiff`, `zcmp` etc to work on compressed files
688+
* to uncompress, use `gunzip` or `gzip -d`
689+
* this will replace the compressed version with the uncompressed archive file
690+
691+
```bash
692+
$ gunzip bkp_words.tar.gz
693+
694+
$ ls -F
695+
backups/ bkp_words.tar low_power_adders/ word_lists/ words_ref.txt
696+
$ ls -sh bkp_words.tar
697+
2.3M bkp_words.tar
698+
```
699+
700+
* to extract the original files from archive, use `-x` option
701+
702+
```bash
703+
$ mkdir test_extract
704+
$ mv bkp_words.tar test_extract/
705+
$ cd test_extract/
706+
$ ls
707+
bkp_words.tar
708+
709+
$ tar -xf bkp_words.tar
710+
$ ls -F
711+
bkp_words.tar word_lists/ words_ref.txt
712+
$ cd ..
713+
$ rm -r test_extract/
714+
```
715+
716+
* the GNU version of `tar` supports compressing/uncompressing options as well
717+
718+
```bash
719+
$ ls -F
720+
backups/ low_power_adders/ word_lists/ words_ref.txt
721+
722+
$ # -z option gives same compression as gzip command
723+
$ # reverse would be: tar -zxf bkp_words.tar.gz
724+
$ tar -zcf bkp_words.tar.gz word_lists words_ref.txt
725+
$ ls -sh bkp_words.tar.gz
726+
652K bkp_words.tar.gz
727+
```
728+
729+
* there are loads of options for various needs, see documentation for details
730+
* `-v` for verbose option
731+
* `-r` to append files to archive
732+
* `-t` to list contents of archive
733+
* `--exclude=` to specify files to be ignored from archiving
734+
* `-j` and `-J` to use `bzip2` or `xz` compression technique instead of `-z` which uses `gzip`
735+
* there are commands starting with `z` to work with compressed files
736+
* `zcat` to display file contents of compressed file on standard output
737+
* `zless` to display file contents of compressed file one screenful at a time
738+
* `zgrep` to search compressed files and so on...
678739

679740
**Further Reading**
680741

681742
* [tar Q&A on unix stackexchange](https://unix.stackexchange.com/questions/tagged/tar?sort=votes&pageSize=15)
682743
* [tar Q&A on stackoverflow](https://stackoverflow.com/questions/tagged/tar?sort=votes&pageSize=15)
744+
* [superuser: gzip without tar? Why are they used together?](https://superuser.com/questions/252065/gzip-without-tar-why-are-they-used-together)
745+
* `zip` and `unzip` commands
746+

0 commit comments

Comments
 (0)