How to Archive Files Using TAR on Linux
An archive is a special kind of file that can store a myriad of folders and files within it. What makes archives so effective is that the files and folders they contain retain the file-structure they had outside the archive, and this structure remains intact when an archive is un-archived. You can archive files and folders in a number of different formats, and one of these happens to be the TAR (Tape Archive) format. Commonly used on Linux and Unix systems, a TAR file (also known colloquially as a tarball) is a file in the Consolidated Unix Archive format.
The TAR file format is used exclusively to store large amounts of data – not compress it. TAR files are, however, usually compressed after they’re created, but they are then no longer TAR files and turn into TGZ files instead.
On Linux and Unix, creating a TAR file is as simple as one and two – one being opening a Terminal or console, and two being typing in the necessary command.
Step 1: Open a Terminal
If you’re using a Linux system, press Ctrl + Alt + T to launch a fresh instance of the Terminal. Alternatively, you can also:
- Click on the Dash button in the top-left corner of your screen to open the Dash (or press the Windows logo key on your keyboard to achieve the same result).
Click on the “Dash” button - Search for “terminal” and press Enter.
Search for “terminal” - In the search results, click on the search result titled Terminal.
Click on “Terminal” in the results
On the other hand, if you are using a computer running on Unix, you need to:
- Click on Applications or Accessories in the toolbar at the top of your screen.
- In the context menu that appears, click on Terminal. Doing so will immediately open a new Terminal window for you.
Click on “Terminal” in the resulting context menu A Terminal on a Unix system
Step 2: Type the necessary command into the Terminal
Once you have a fresh instance of the Terminal up and running on your screen, all that is left for you to do is type in the required command. The command you need to type into the Terminal in order to create a TAR file will depend on whether you want to create an archive of an entire folder and all of the sub-directories and files it contains, or an archive containing only one or more specific files.
If you would like to TAR a complete directory and everything it contains, type the following into the Terminal window and press Enter:
tar -cvf X.tar /path/to/folder
Note: In the command above, replace X with what you want the TAR file created to be named, and replace /path/to/folder with the complete path to the folder (or file) you want to be archived. For example, if you want to create an archive of the /myfiles/ folder named Files, the final command you type in will look something like this:
tar -cvf Files.tar /usr/local/myfiles
If, on the other hand, you want to archive one or more specific files using the TAR file format, type the following into the Terminal window and press Enter:
tar -cvf X.tar /path/to/file1 /path/to/file2
Note: In the command above, replace X with what you want the TAR file created to be named. Also, replace /path/to/file1 with the complete path to the first file you want to be archived, /path/to/file2 with the complete path to the second file you want to be included in the archive, and so on. You may add the directories to as many individual files as you want to be added to the archive – just make sure that each directory is separated with its predecessor by a space.