0

So I'm new to linux and I have wrote a small script that is suppose to archive the contents that end with.log in /var/log to a tar file called log.tar in a new directory called archive. Then it's suppose to list the contents of the tar file and then extract it to a new file called backup.

#!/bin/bash
mkdir archive
mkdir backup
cd archive
tar -cvf log.tar /var/log/*.log
tar -tf tar.log
tar -xf tar.log -C ~/backup

So when I run the script I get this output.

tar: Removing leading `/' from member names
/var/log/alternatives.log
/var/log/auth.log
/var/log/bootstrap.log
/var/log/dpkg.log
/var/log/fontconfig.log
/var/log/gpu-manager.log
/var/log/kern.log
/var/log/vboxadd-install.log
/var/log/vboxadd-setup.log
/var/log/Xorg.0.log
var/log/alternatives.log
var/log/auth.log
var/log/bootstrap.log
var/log/dpkg.log
var/log/fontconfig.log
var/log/gpu-manager.log
var/log/kern.log
var/log/vboxadd-install.log
var/log/vboxadd-setup.log
var/log/Xorg.0.log

So I was wondering if it's possible to remove the /var/log/ part of the lines when archiving so I instead get this when listing what is in the tar file.

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

And when I do ls in the backup file I see the var directory and when I do las in the var directory and finally inside the log directory I get the log files. So how do I extract without the /var/log/ path name and directory?

1 Answer 1

0

you simply have to be in the directory /var/log at tar command

tar cf /path/archives/yourfile.tar *.log

then you have your files without path and your destination file is inside you archive directory.

just modify your script in this config to learn & train yourself on scripting.

1
  • you don't need to change directory first. that's what tar's -C (aka --directory) option is for. e.g. tar cf /path/to/logs.tar -C /var/log *.log. you can also use -C when extracting files from an archive. e.g. tar xf /path/to/logs.tar -C /path/to/extraction/directory.
    – cas
    Jan 7, 2018 at 1:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .