Linux tar adding files

10 “tar” Command Examples in Linux to Create and Extract Archives

The tar (tape archive) command is used to create and extract archives in Linux. It can compress and store multiple files in a single archive.

The commonly seen file extensions are .tar.gz and .tar.bz2 which is a tar archive further compressed using gzip or bzip algorithms respectively.

In this tutorial we shall take a look at simple examples of using the tar command to do daily jobs of creating and extracting archives on linux desktops or servers.

Using the tar command

The tar command is available by default on most linux systems and you do not need to install it separately.

With tar there are 2 compression formats, gzip and bzip. The "z" option specifies gzip and "j" option specifies bzip. It is also possible to create uncompressed archives.

1. Extract a tar.gz archive

Well, the more common use is to extract tar archives. The following command shall extract the files out a tar.gz archive

Here is a quick explanation of the parameters used —

  • x — Extract files
  • v — verbose, print the file names as they are extracted one by one
  • z — The file is a «gzipped» file
  • f — Use the following tar archive for the operation

Those are some of the important options to memorise

Extract tar.bz2/bzip archives

Files with extension bz2 are compressed with the bzip algorithm and tar command can deal with them as well. Use the j option instead of the z option.

$ tar -xvjf archivefile.tar.bz2

2. Extract files to a specific directory or path

To extract out the files to a specific directory, specify the path using the «-C» option. Note that its a capital C.

$ tar -xvzf abc.tar.gz -C /opt/folder/

However first make sure that the destination directory exists, since tar is not going to create the directory for you and will fail if it does not exist.

3. Extract a single file

To extract a single file out of an archive just add the file name after the command like this

$ tar -xz -f abc.tar.gz "./new/abc.txt"

More than once file can be specified in the above command like this

$ tar -xv -f abc.tar.gz "./new/cde.txt" "./new/abc.txt"

4. Extract multiple files using wildcards

Wildcards can be used to extract out a bunch of files matching the given wildcards. For example all files with «.txt» extension.

$ tar -xv -f abc.tar.gz --wildcards "*.txt"

5. List and search contents of the tar archive

If you want to just list out the contents of the tar archive and not extract them, use the «-t» option. The following command prints the contents of a gzipped tar archive,

$ tar -tz -f abc.tar.gz ./new/ ./new/cde.txt ./new/subdir/ ./new/subdir/in.txt ./new/abc.txt .

Pipe the output to grep to search a file or less command to browse the list. Using the «v» verbose option shall print additional details about each file.

Читайте также:  Linux посмотреть свой айпи

For tar.bz2/bzip files use the «j» option

Use the above command in combination with the grep command to search the archive. Simple!

$ tar -tvz -f abc.tar.gz | grep abc.txt -rw-rw-r-- enlightened/enlightened 0 2015-01-13 11:40 ./new/abc.txt

6. Create a tar/tar.gz archive

Now that we have learnt how to extract existing tar archives, its time to start creating new ones. The tar command can be told to put selected files in an archive or an entire directory. Here are some examples.

The following command creates a tar archive using a directory, adding all files in it and sub directories as well.

$ tar -cvf abc.tar ./new/ ./new/ ./new/cde.txt ./new/abc.txt

The above example does not create a compressed archive. Just a plain archive, that puts multiple files together without any real compression.

In order to compress, use the «z» or «j» option for gzip or bzip respectively.

The extension of the file name does not really matter. "tar.gz" and tgz are common extensions for files compressed with gzip. ".tar.bz2" and ".tbz" are commonly used extensions for bzip compressed files.

7. Ask confirmation before adding files

A useful option is «w» which makes tar ask for confirmation for every file before adding it to the archive. This can be sometimes useful.

Only those files would be added which are given a yes answer. If you do not enter anything, the default answer would be a «No».

# Add specific files $ tar -czw -f abc.tar.gz ./new/* add ‘./new/abc.txt’?y add ‘./new/cde.txt’?y add ‘./new/newfile.txt’?n add ‘./new/subdir’?y add ‘./new/subdir/in.txt’?n # Now list the files added $ tar -t -f abc.tar.gz ./new/abc.txt ./new/cde.txt ./new/subdir/

8. Add files to existing archives

The r option can be used to add files to existing archives, without having to create new ones. Here is a quick example

Files cannot be added to compressed archives (gz or bzip). Files can only be added to plain tar archives.

9. Add files to compressed archives (tar.gz/tar.bz2)

Its already mentioned that its not possible to add files to compressed archives. However it can still be done with a simple trick. Use the gunzip command to uncompress the archive, add file to archive and compress it again.

$ gunzip archive.tar.gz $ tar -rf archive.tar ./path/to/file $ gzip archive.tar

For bzip files use the bzip2 and bunzip2 commands respectively.

10. Backup with tar

A real scenario is to backup directories at regular intervals. The tar command can be scheduled to take such backups via cron. Here is an example —

$ tar -cvz -f archive-$(date +%Y%m%d).tar.gz ./new/

Run the above command via cron and it would keep creating backup files with names like —
‘archive-20150218.tar.gz’.

Ofcourse make sure that the disk space is not overflown with larger and larger archives.

11. Verify archive files while creation

The «W» option can be used to verify the files after creating archives. Here is a quick example.

$ tar -cvW -f abc.tar ./new/ ./new/ ./new/cde.txt ./new/subdir/ ./new/subdir/in.txt ./new/newfile.txt ./new/abc.txt Verify ./new/ Verify ./new/cde.txt Verify ./new/subdir/ Verify ./new/subdir/in.txt Verify ./new/newfile.txt Verify ./new/abc.txt

Note that the verification cannot be done on compressed archives. It works only with uncompressed tar archives.

Читайте также:  Astra linux sudo права

Conclusion

Besides the tar command, there are many other commands available on the linux command line that can be used to compress and pack files in an archive. For example the zip and unzip command can also be used to create compressed archives.

The zip command also supports the zipcloak command that can be used to create password protected .zip archives. The tar format does not support any kind of encryption. If you want to create password protected tar archives then use the ccrypt command to encrypt the .tar or .tar.gz file.

Thats all for now. For more check out the man page for tar command, with «man tar». Or run the «tar —help» to see the list of all options supported by the tar command.

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

7 Comments

Источник

This post and this website contains affiliate links. See my disclosure about affiliate links.

how to add new files into an archive file (tar, gz, zip) in linux (also delete files)

There are several programs, both command line and GUI based that will allow you to view and extract the contents of an archive. But sometimes, you will need to modify an existing archive file, such as add files to the archive, delete an existing file in the archive or rename a file in the archive.

Most of the GUI applications, like Ark support adding and deleting files using the drag and drop. This is accomplished by extracting the archive in memory and performing the modification operation. The archive is then re-archived from memory and saved to the disk. A GUI interface may not always be available and you may want to modify the archive from command line without extracting it completely.

You can always do it the long way. First extracting the archive to the hard disk and then modify the contents and re-create the archive from the modified content. This requires space on the disk and extra time for extraction and archiving.

Most of the widely used archive formats are usually the combination of two different process: archiving and compression. There are a couple of caveats that you should be aware of while modifying the archives.

compressed archive: You can directly modify an archive but not a compressed archive. Modifying the compressed file without uncompressing it first will corrupt the file. For example, this means that you can modify a .tar file, but not a .tar.gz file. The .tar.gz should first be uncompressed to .tar.

file position: There is really no way to “replace” a file inside the archive while also maintaining its position. You can always extract the whole archive to a temp location, modify or replace the file and then archive it back up. But we are only dealing with the scenario of doing the operations in place without extracting the entire archive.

Читайте также:  Hp laserjet p1102w linux driver

Add files to archive

tar archive

Assuming that you have a archive with .tar extension, you can use the -r (or –append) option of the tar command to add/append a new file to the end of the archive.

bash$ tar rvf /path/to/archive.tar /path/to/newfile.txt

You can use the -v option to have a verbose output to verify the operation. The other option that can be used with the tar command is -u (or –update).

The update refreshes the files that are newer than the ones in the archive, so it works slightly different than the –append (or -r) option but provides the same functionality for the addition of a new file.

bash$ tar uvf /path/to/archive.tar /path/to/newfile.txt

Another feature of the archive is that it allows for multiple files with the same name. If you append a file with the same name, then the file is just appended to the end of the archive without deleting the previous one. But when you extract the archive, it will extract the latest version (by its order inside the archive) of the file.

zip archive

The zip archive can be modified using the zip command. The -r option of the zip command allows you to add new files to the archive.

bash$ zip -rv zipfile.zip newfile.txt newfile1.txt

where the zipfile.zip is the name of an existing zip file and the newfile.txt and newfile1.txt are the files that you want to add to the zip archive.

jar archive

In case of the jar files, you can use the jar command. The –update (or -u) option allows you to add or update files into a jar archive.

bash$ jar -uvf jarfile.jar newfile.txt

Again, you can add multiple files in a single command just like the zip command.

Delete files from archive

tar archive

You can use the –delete option of the tar command to delete existing files from inside an archive.

bash$ tar -dvf archive.tar filename.txt

zip archive

You can use the –delete (or -d) option of the zip command to delete files from the archive. The zip command take multiple arguments for file names and supports regular expressions in the argument. This allows for deletion of multiple files with a single command.

bash$ zip -d zipfile.zip filename.doc \*.txt

The above command will delete the file named filename.doc and also all the files with an extension of .txt.

jar archive

Unfortunately there is no option with the jar command to delete the files. But jar files are essentially zip files with .jar extension, hence you can use the zip command to delete the files, just as you would to a zip file.

bash$ zip -d jarfile.jar file1.txt file2.txt

Renaming files inside the archive essentially amounts to deleting the existing file and adding the new file. However, this will also cause the file to lose its position inside the tar archive. If the order of the files is important, then you should extract the archive and rename the file on the disk first.

Источник

Оцените статью
Adblock
detector