Adding Individual Files or Folders to an Archive

The following steps will walk you through how to create an archive, or tarball, of specific files. This is handy when you don’t necessarily want to archive an entire directory, but would rather just have subset of files spread across different directories. Open a terminal and we’ll begin with this example of grabbing files within our home directory ending in .php.

Step 1: Create a blank/empty tarball in our home directory to add to

tar cvf ~/php_backups.tar --files-from /dev/null

Step 2: Add/append folders to tarball

The following will search for all files ending in .php, pipe those results into xargs which feeds into the tar command to add to our directory.

find ~/ -type f -name "*.php" | xargs tar rvf ~/php_backups.tar

Step 3: Compress the archive

gzip ~/php_backups.tar

Step 4: # Take a look at the archive to verify the directory structure

tar tzvf backup.tar.gz

From here, you can untar the file to whatever directory you want. Keep in mind that it will overwrite any existing files. You can combine other commands (like for or ls) to get a list of files that you’re interested in. You could also save all of the files with the directory path into a file and use cat to pipe into the xargs command.

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:

There's 0 Comment So Far

Share your thoughts, leave a comment!