Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When using tar for backups I'd like to create a separate list of the files that is included in the archive for storing alongside the backup tarball.

I also want to be able to store the error messages from tar in a log.

Solutions that's not so good:

  • tar --verbose ... &> filelist.txt But this would give errors + file list in one file.
  • tar --list after the tarball has been created. But this takes a long time.

Is there some way to get the file list and store it in a separate file at the same time as I'm rolling the ball for the first time?

share|improve this question
    
how do you specify the files to tar? –  Jasper Apr 15 '14 at 9:33
    
Is getting a list separately before using tar an option? –  edvinas.me Apr 15 '14 at 9:36
    
Do you want only files or directories as well ? (tar -v is showing both) –  Ouki Apr 15 '14 at 9:38
1  
What makes --verbose a not-so-good option? Did you consider --index-file=FILE –  Johan Apr 15 '14 at 12:30
1  
I don't understand what the problem is. On my GNU tar 1.26, the only output of tar -cvf foo.tar * is the list of files. What is the "all output" that you want to avoid? –  terdon Apr 15 '14 at 14:00

3 Answers 3

You could append each file to the tar file:

for f in file ...; do 
    echo "$f"
    tar rf result.tar $f
done
share|improve this answer

You mention you don't like tar --list because it's slow. I'm guessing this is because it's a large tarball, and it has to re-scan the whole thing. If this is indeed the case, you can get better performance out of this by scanning as it's being created:

tar -c /input/directory | tee output.tar | tar -t > filelist.txt

This uses tee to split the resulting tarball, one going to a file, the other going to tar -t.

If you want to gzip the tarball:

tar -c /input/directory | tee >(gzip > output.tar.gz) | tar -t filelist.txt

We don't have the first tar do the gzip as it'll waste CPU cycles when the second tar has to decompress it again. Instead we only gzip the tarball as it's being written to disk.

share|improve this answer
up vote 0 down vote accepted

This would put the error messages (stderr) in one file and file list (stdout) in one file:

$ tar -cvf archive.tar test nonexisting 1> stdout.txt 2> stderr.txt

$ cat stdout.txt
test/
test/05.txt
test/06.txt
test/00.txt
test/01.txt
test/10.txt
test/07.txt
test/03.txt
test/02.txt
test/04.txt
test/08.txt
test/09.txt

$ cat stderr.txt
tar: nonexisting: Funktion "stat" misslyckades: Filen eller katalogen finns inte
tar: Avslutar med felstatus på grund av tidigare fel

Using --index-file as suggested by Johan in the comment to my question would also work good:

tar --create \
    --verbose \
    --index-file=$fileList \
    --file $archiveFile \
    $filesystemToArchive \
    &>> $logFilename

From tar manpages:

--index-file=FILE

send verbose output to FILE

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.