Summary
Linux Commands: Using the tar
Command
In this lesson, we will learn about the tar
command in Linux, primarily focusing on how to create archives, list contents of archives, and extract files from archives.
What is an Archive?
An archive combines multiple files into a single file, making it easier to send a group of files together. Archiving is often followed by compression to reduce file size, but this lesson will focus only on the archive creation and management using tar
.
Learning Objectives
- Create an archive using
tar
- List the contents of an existing archive without extracting
- Extract files from an existing archive
Steps Involved
1. Creating an Archive
To create an archive, use the following command:
tar --create --file=archive.tar notes.txt sample.jpg
--create
(or-c
for the shortcut) indicates that you want to create an archive.--file=archive.tar
(or-f
) specifies the name of the archive file.- The last part lists the files to include in the archive.
To check if the archive was created successfully, use:
ls -l
2. Listing the Contents of an Archive
To view the contents of an existing archive without extracting it, use:
tar --list --file=archive.tar
Or with shortcuts:
tar -tf archive.tar
The -t
option (long option: --list
) is used to list the files in the archive.
3. Extracting Files from an Archive
To extract files from an archive, you can use the command:
tar --extract --file=archive.tar
Or with shortcuts:
tar -xf archive.tar
- The
-x
option (or--extract
) is used for extracting files.
To extract specific files, specify them at the end:
tar -xf archive.tar notes.txt
Additional Options
- The
-v
option can be added to commands to enable verbose mode, which displays the files being processed. - Always include the
-f
option to specify the archive name in any operation.
Conclusion
In this lesson, we covered the basics of the tar
command in Linux for creating, listing, and extracting archives. The tar
command has many more options, which can be explored using:
man tar
Feel free to dive deeper into the capabilities of tar
based on your specific needs.
Thank you for watching! Please like and subscribe for more content.