1

I've been tasked to archive certain files into a new directory at /home/archive/.

I used this command:

mkdir /home/archive
cd /home/archive
tar -cvf log.tar \
    /var/log/auth.log /var/log/alternatives/log \
    /var/log/bootstrap.log /var/log/cron.log    \
    /var/log/dpkg.log /var/log/kern.log /var/log/mail.log

Sorry for the long command line but I only need those files from the entire /var/log directory.

The issue I have is that when I execute the long command as stated above, it works but it shows the entire file path when I do less log.tar (to view contents without extracting just yet).

I want it to display this:

alternatives.log 
auth.log 
bootstrap.log 
cron.log 
dpkg.log 
kern.log 
mail.log 

Instead, it has the /var/log path before the file name. Sorry if it's confusing to read all this but I would greatly appreciate the help!

1

The quick solution would be to run running your command differently. Run it from within /var/log.

cd /var/log
tar -cvf /home/archive.log.tar \
    auth.log  alternatives/log bootstrap.log cron.log    \
    dpkg.log  kern.log         mail.log
cd /home/archive
  • Thanks a lot! this literally gave me the exact thing i needed thanks man – hxx07 Dec 2 '18 at 18:59
1

I would follow the instructions given here: https://stackoverflow.com/a/18681628/10693721

tar -cjf site1.tar.bz2 -C /var/www/site1

tar -cvf log.tar -C /var/log/auth.log /var/log/alternatives/log /var/log/bootstrap.log /var/log/cron.log /var/log/dpkg.log /var/log/kern.log /var/log/mail.log
  • Your first tar command adds a compression that was not requested and uses another directory, your second is wrong (-C /var/log/auth.log would try to change to a directory that isn't one). Thanks for the edit, but we edited at more or less the same time. – Law29 Dec 1 '18 at 22:30
1

tar will record the paths of the files you indicated. I am not quite sure just why it is a problem?

If you just wish to remove the path when you display the files, you can execute tar tf /home/archive/log.tar | sed 's,.*/,,' That will not change the file, so someone doing tar tf /home/archive/log.tar or less /home/archive/log.tar will still see the paths.

Do you want to modify the tar file? First problem, tar files are not intended for modification, modifying one is essentially untarring it, changing what you want, and retarring it. I'll suppose that repacking it from the originals is OK.

Second problem, you might have a typo. Are you really tarring /var/log/alternatives/log as you write in your command, or /var/log/alternatives.log as you want in your output? I'll assume the second.

If so,

cd /var/log && tar cf /home/archive/log.tar \
    auth.log alternatives.log bootstrap.log \
    cron.log dpkg.log kern.log mail.log

or

tar cf /home/archive/log.tar                \
    auth.log alternatives.log bootstrap.log \
    cron.log dpkg.log kern.log mail.log     \
    -C /var/log

should do it nicely.

If you really wish to change the tar file you have, then make sure you have enough space in /home (the way I've done it you'll need the space for two more files), and do

cd /home/archive
mv -i log.tar log_with_paths.tar
tar xf log_with_paths.tar
cd var/log
tar cf /home/archive/log.tar *
cd /home/archive
rm -r /home/archive/var
tar tf log.tar
1

This is an exercise in regular expressions by stripping out relative file paths from the output of the tar file list.

Start out by creating a tar archive and list full paths to the folders:

tar -cf archive.tar /path1/to/folder1 /path2/to/folder2 /path3/to/folderN

Then tell tar to show only path output. Pipe its output as stdout into grep as stdin. Show the final output with only filenames:

tar -tf archive.tar | grep -o [^/]*$

Keep the two operations separate. This grep command:

grep -oP [^/]*$

can be applied to any stdout or text file that ends the line with paths to filenames. It is best to constrain it to just that alone. tar -tf archive.tar outputs paths to filenames and is well suited format for the grep above. Its output looks like this:

/path/to/folder1/filename1(newline)
/path/to/folder2/filename2(newline)
/path/to/folderN/filenameN(newline)

Breaking it down: [^/]*$ = find the last forward slash in the line. Return zero or more characters from that point onward (excluding the forward slash) to the end of the line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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