tar list files only

I don't see a way to do it from the man page, but you can always filter the results. The following assumes no newlines in your file names:

tar tzf your_archive | awk -F/ '{ if($NF != "") print $NF }'

How it works

By setting the field separator to /, the last field awk knows about ($NF) is either the file name if it's processing a file name or empty if it's processing a directory name (tar adds a trailing slash to directory names). So, we're basically telling awk to print the last field if it's not empty.


Utilizing Joseph R.'s suggestion one can use the regex [^/]$ to grep for the files by looking for lines not ending with /.

tar tzf archive.tar.gz | grep -e "[^/]$"


Assuming none of the file names contain newlines:

tar -tf foo.tar | sed -e 's#.*/##' -e '\#.#!d'

The first sed command removes everything before the last / on a line, so that only the file name part is printed. The second command deletes the lines which are now empty, i.e. the lines that ended in a /, which are directories.

Tags:

Tar