Extract a Single File from a Tarball
Suppose I have a tarball (.tar.gz file) which is large and I only want to extract a specific file from it. If I know the name of the file all I have to do is pass the file’s relative path that it is stored under to the command line.
Here is an example of the error you will get if you pass the incorrect file specification:
$ tar zxvf dirtree-tarball.tar.gz file-7-30003.txt
tar: file-7-30003.txt: Not found in archive
Since I don’t have the full path, I can just search for it:
$ tar tf dirtree-tarball.tar.gz | grep 'file-7-30003.txt'
./dir_2/file-7-30003.txt
Now I can pass the full path and extract the file:
$ tar zxvf dirtree-tarball.tar.gz ./dir_2/file-7-30003.txt
./dir_2/file-7-30003.txt
$ ls
dir_2 dirtree-tarball.tar.gz
$ tree
.
├── dir_2
│ └── file-7-30003.txt
└── dirtree-tarball.tar.gz
1 directory, 2 files
Note that it extracts it to the same directory tree but it will only extract the file(s) specified on the command line.
Copying a Directory Tree Recursively Using tar
You can use tar
to copy a directory tree to another
directory in one shot along with preserving ownership,
permissions and timestamps. This also avoids making an intermediary
tarfile which may cause problems if the size of the file copy
is large and the storage resources are low. Just cd
to the
top of the directory that you want to copy and begin.
Let’s assume that you want to copy the contents of the source directory to a target directory:
$ cd /path/to/source
$ tar cf - * | (cd /target; tar xfp - )
Using Tar with a Text Input File
If you have a lot of files in a directory and you only need to
tar
a subset of them you can create a list of the files you
want in a text file and pass it to the tar
command like
this:
$ tar -cvf tarball.tar -T filelist.txt
or
$ tar cvf tarball.tar $(cat filelist.txt)
Archive Only Files In a Directory
If you want to create a tar archive of only the files of a directory and exclude any subdirectories you can use the ls -la
command and pipe the output to awk
. However you need to remove the first 8 fields from the output and leave all of the remaining parts of the line in case there are spaces in the filename. One quick and dirty way of doing that is to set each of the 8 fields to a blank and then use sed
to trim the leading spaces. You can optionally add quotation marks around the filename in your output too.
$ ls -al | awk '$0!~/^d/ {$1=$2=$3=$4=$5=$6=$7=$8=""; printf("%s\"\n", $0)}' | sed 's/^[[:space:]]*/"/' | xargs tar cvf archive-name.tar
Tags: cli, tar, awk, xargs, sed, motd
Untar a Tarball to a Remote Directory
Sometimes you may need to copy an entire directory structure to another system using the command line. Here is a quick way to do it using the tar command:
cat myfile.tgz | ssh user@host "tar xzf - -C /some/dir"