2025-3-8 Linux Literacy TAR

Initially, tar archives were used to store files conveniently on magnetic tape. The name "Tar" comes from this use; it stands for tape archiver. Despite the utility's name, Tar can direct its output to available devices, files, or other programs (using pipes), it can even access remote devices or files (as archives).

We will be starting with 4 file and 1 dir:

a - aaaaa

b - bbbbb

c - ccccc

d - a dir containing

  • e - eeeee
  • Script

tar -cvf 1.tar a b c d

-c, --create

Create a new archive with dir to be archived recursively

-f, --file

Specify the archive being used.

-v, --verbose

Show the files being worked on as tar is running.


tar -tf 1.tar

-t, --list

List the contents of an archive.


mkdir result
mv 1.tar result/
cd result

tar -xvf 1.tar

ls

-x, --extract, --get

Extract files from an archive. You can specify which file/dir you want to extract by giving its name.


tar -xvvf 1.tar

-vv, the double verbose

Show extra detail about the files being extracted.


cd ..
rm -r result

ls

tar -cvf 1.tar.gz a b c d

ls (flash the tar.gz)

Compression

-a, --auto-compress

will auto-detect

|Long|Short|Archive format|

|---|---|---|

|‘--gzip’|‘-z’|gzip|

|‘--bzip2’|‘-j’|bzip2|

|‘--xz’|‘-J’|xz|

|‘--lzip’||lzip|

|‘--lzma’||lzma|

|‘--lzop’||lzop|

|‘--zstd’||zstd|

|‘--compress’|‘-Z’|compress|


rm 1.tar.gx

ls

tar -cvf 1.tar a b 
tar -cvf 2.tar c d

ls

tar -Af 1.tar 2.tar

ls

tar -tf 1.tar

-A, --catenate, --concatenate

Append archives to the end of another archive.


tar -df 1.tar
---Nothing
nano c
---lets add a 5 at the end
tar -df 1.tar
---output:
---c: Mod time differs
---c: Size differs

-d, --diff, --compare

Find differences between archive and file system.


tar -rf 1.tar c

tar -tf 1.tar
---c is at the end! and twice!!!) 

-r, --append

Append files to the end of an archive.


tar --delete -f 1.tar c

tar -tf 1.tar
---c are gone

--delete

Delete from the archive.


rm 1.tar

tar -cvf 1.tar a b c d

nano b
---lets add the 5

tar -uvf 1.tar a b c d

b

tar -tf 1.tar
will put b at the end

-u, --update

Append files which are newer than the corresponding copy in the archive. Updated files will be tacked to the end of the archive instead of replaced.

Sources

https://www.gnu.org/software/tar/manual/tar.html