Don’t fear the command line : Tar. What is that?

When I first heard of the tar command I was confused and could not quite understand it’s use. How is that command useful in the day-to-day use of Linux and how often does it actually get used? Not to mention that it has all these flags to go alongside …

When I first heard of the tar command I was confused and could not quite understand it’s use. How is that command useful in the day-to-day use of Linux and how often does it actually get used? Not to mention that it has all these flags to go alongside it, it was very confusing to me at first sight.



What is Tar?

The tar command stands for tape archive and is used to group many files and directories into a single compressed archive. That way it can be moved from disc to disc, from machine to machine and sent over to someone else. The same command is also used to extract that archive that we create.
It is essentially a zip file.

expand on that

The general syntax looks something along these lines:
tar [flags] [archive-file] [file or directory to be archived]



The absolute basics to get started

To put a folder into an archive (let’s say in this case a folder named folder-very creative naming) we would do the following:

tar -cvf archive.tar folder
Enter fullscreen mode

Exit fullscreen mode

Above we created a single file(archive.tar) that contains the folder.

  • The -c flag stands for create and is used to create the tar file.
  • The v flag stands for verbose and displays the progress of the action.
  • The f flag creates archive with given filename(archive.tar)

While experimenting with the flags and changing their order I noticed that when I used -cfv I got an error whereas when I used -cvf like I showed in the example, it worked.

how?

I guessed that the order must be very important.After some research with my best friend lately(stackoverflow) I learnt that when the flags are used together , the dash in front of them is optional. However, if we use the dash we must use the flags in the accurate order otherwise it won’t work.

pat

So, we created our archive and now we want to extract it.
To extract our archive.tar file we switch the -c flag which is used to create and we instead use the -x flag which stands for extract.

tar -xvf archive.tar
Enter fullscreen mode

Exit fullscreen mode

The archive we created earlier was not compressed, and most times we want to compress it as it it significantly smaller in size as it gets run through gzip .
To create an archive that is compressed we use the -z flag which stands for compress the tar file using gzip

tar -czvf archive.tar.gz folder
Enter fullscreen mode

Exit fullscreen mode

Again,If using the - in front of the flags it’s important to remember the right order.
If we type ls -ls to see a list with of the files we have and information about, including their size( that’s what the s stands for) then we’ll notice than archive.tar.gz is significantly smaller in size than archive.tar.

To extract it we then use:

tar -xzvf archive.tar.gz
Enter fullscreen mode

Exit fullscreen mode

We could use the -C flag and a destination folder for where we want the files to go. That folder must exist already before this action.If we leave that flag of it’ll extract the archive where ever we are .

tar -xzvf archive.tar.gz -C destination-folder
Enter fullscreen mode

Exit fullscreen mode

These are the absolute basics to get started with tar
Thanks for reading ?

dog coding


Print Share Comment Cite Upload Translate
APA
Dionysia Lemonaki | Sciencx (2024-03-28T10:28:24+00:00) » Don’t fear the command line : Tar. What is that?. Retrieved from https://www.scien.cx/2021/02/16/dont-fear-the-command-line-tar-what-is-that/.
MLA
" » Don’t fear the command line : Tar. What is that?." Dionysia Lemonaki | Sciencx - Tuesday February 16, 2021, https://www.scien.cx/2021/02/16/dont-fear-the-command-line-tar-what-is-that/
HARVARD
Dionysia Lemonaki | Sciencx Tuesday February 16, 2021 » Don’t fear the command line : Tar. What is that?., viewed 2024-03-28T10:28:24+00:00,<https://www.scien.cx/2021/02/16/dont-fear-the-command-line-tar-what-is-that/>
VANCOUVER
Dionysia Lemonaki | Sciencx - » Don’t fear the command line : Tar. What is that?. [Internet]. [Accessed 2024-03-28T10:28:24+00:00]. Available from: https://www.scien.cx/2021/02/16/dont-fear-the-command-line-tar-what-is-that/
CHICAGO
" » Don’t fear the command line : Tar. What is that?." Dionysia Lemonaki | Sciencx - Accessed 2024-03-28T10:28:24+00:00. https://www.scien.cx/2021/02/16/dont-fear-the-command-line-tar-what-is-that/
IEEE
" » Don’t fear the command line : Tar. What is that?." Dionysia Lemonaki | Sciencx [Online]. Available: https://www.scien.cx/2021/02/16/dont-fear-the-command-line-tar-what-is-that/. [Accessed: 2024-03-28T10:28:24+00:00]
rf:citation
» Don’t fear the command line : Tar. What is that? | Dionysia Lemonaki | Sciencx | https://www.scien.cx/2021/02/16/dont-fear-the-command-line-tar-what-is-that/ | 2024-03-28T10:28:24+00:00
https://github.com/addpipe/simple-recorderjs-demo