Monday, April 8, 2019

UNIX tar command with examples

Creating a tar archive

To create an archive file from a file or list of files you would use the ‘-c’ and ‘-f’ options. The ‘-c’ option tell tar to create the archive, while the ‘-f’ tells tar what the resulting archive name should be and you will use this options with most of the other options you supply to tar.

tar -cf outputfile.tar file1 file2 file3...
sam@asus:~/unix% tar -cf output.tar 5MB.zip 
sam@asus:~/unix% ls -la output.tar 
-rw-rw-r-- 1 sam sam 5253120 Apr  4 10:07 output.tar
sam@asus:~/unix% 

With the tar command you can also archive whole directories and not just specific files. To compress a whole directory you would do the same as before except specify the directory instead of single files.

tar -cf outputfile.tar dir1 dir2 dir3...
sam@asus:~/unix% tar -cf directory.tar docs/
sam@asus:~/unix% ls -l directory.tar 
-rw-rw-r-- 1 sam sam 10240 Apr  4 10:12 directory.tar
sam@asus:~/unix% 

Extracting from a tar archive

To extract file from a archive you would use the ‘-x’ option. This tells tar to extract all the files in the archive to the current directory. The ‘-v’ option tell tar to be verbose and output the file it is extracting to the directory.

tar -xvf file.tar
sam@asus:~/unix% tar -xvf output.tar
5MB.zip
docs/
docs/script.sh
docs/file.txt
sam@asus:~/unix%  

Tar allows you to choose the directory where you want to extract the contents of the archive with the ‘-C’ option.

sam@asus:~/unix% mkdir test
sam@asus:~/unix% tar -xf output.tar -C /home/sam/unix/test
sam@asus:~/unix% ls -l /home/sam/unix/test
total 5124
-rw-rw-r-- 1 sam sam 5242880 Jun  2  2008 5MB.zip
drwxrwxr-- 2 sam sam    4096 Apr  1 19:20 docs
sam@asus:~/unix% 

Be sure and create the directory before extracting in to it. If you do not create the directory first you will get a ‘No such file or directory’ error.

Viewing the contents of a tar file

You can also view the contents of a tar archive without having to decompress the file by using the ‘-t’ option. The ‘-t’ option tells tar list all files in the archive for viewing.

tar -tf file.tar
sam@asus:~/unix% tar -tf output.tar 
5MB.zip
sam@asus:~/unix% 

Append a new file to an archive

To append a new file to the end of the archive you would use the ‘-r’ option. The ‘-r’ option tells tar to append the listed file or directory to the END of the archive.

tar -rf file.tar file1 file2 file3...
sam@asus:~/unix% tar -rf output.tar file1.txt
sam@asus:~/unix% tar -tf output.tar
5MB.zip
file1.txt
sam@asus:~/unix% 

Delete a file from a archive

Deleting a file from a archive is done by using the ‘--delete’ option plus the file or directory to be removed.

tar -f file.tar --delete file.txt
sam@asus:~/unix% tar -f output.tar --delete file1.txt
sam@asus:~/unix% tar -tf output.tar
5MB.zip
sam@asus:~/unix% 

Append a tar file to another tar file

Tar gives you the ability to append another tar file to the current tar archive with the ‘-A’ option. Tar does not appended the whole archive file itself, but the contents of the archive. The ‘directory.tar’ archive contained file from the docs/ directory.

tar -Af destination.tar source.tar
sam@asus:~/unix% tar -Af output.tar directory.tar
sam@asus:~/unix% tar -tf output.tar 
5MB.zip
docs/
docs/script.sh
docs/file.txt
sam@asus:~/unix% 

Update newer files within the archive

You can update files within a tar archive with a newer version of the file by specifying the ‘-u’ option.

tar -uf file.tar file1 file2 file3...
sam@asus:~/unix% echo "AAABBBCCC" >docs/file.txt
sam@asus:~/unix% tar -uf output.tar docs/file.txt
sam@asus:~/unix% tar -tf output.tar
5MB.zip
docs/
docs/script.sh
docs/file.txt
docs/file1.txt
sam@asus:~/unix% 

No comments:

Post a Comment

Exploiting Weak WEBDAV Configurations

The server we are going to audit has the following fingerprint. 80/tcp open http Apache httpd 2.2.8 ((Ubuntu) DAV/2) Next we need t...