Listing log files in some directory and create tar file - linux

i want to write a shell script in bash syntacs can do
listing all .log files in var/log directory and ask with y/n question for each file to create a tar from that file.
can anyone help me?
#!/bin/bash
log='find /var/log -name *.log'

If you want to get only files in /var/log (without subdirectories) you can do it on this way:
tar cvf tarfile.tar /var/log/*.log
if you want also the files in subdirectories you can do it on this way:
find /var/log -name *.log|tar cvf tarfile.tar
(this may not work if you have space or special symbols in filenames)

Related

Linux tar with pattern match and removing leading paths

I'm trying to archive all the .log files located in the /var/log directory and on creation remove all leading paths on files.
I have found I can archive all the .log files easily with:
tar -cvf ~/backup.tar /var/log/*.log
unfortunately, after searching online the way to remove leading paths is to use -C to change directory for the command only now it doesn't recognize the *.log and thinks * is literal.
using:
tar -cvf ~/backup.tar -C /var/log *.log
I get an error saying cannot find file *.log.
I imagine my syntax must be off and I've tried some changes to the syntax with no avail.
Using find to pass files to tar:
find /var/log -name *.log -printf '%P\n' |\
tar -C /var/log -czf backup.tar.gz -T -
Find will look for *.log files and printf format the output to show just filenames.
tar's '-T -' tells to read filenames from stdin

Archive all the files from source directory into a xyz.gz file and move that to target directory using UNIX shell script

Requirement: Archive files using UNIX shell script into .gz format without directory structure
I am using below command
tar -C source_dir -zcvf target_dir/xyz.gz source_dir
example:
tar -C /home/log -zcvf /home/archive/xyz.gz /home/log
here xyz.gz contains /home/log
It's creating xyz.gz file maintaining the directory structure. I want only files to be archive without directory structure.
You can try the following command:
$ cd /home/log
$ tar zcvf /home/archive/xyz.gz *
You can use the --transform option to strip leading path components from the archived file names using a sed espression:
tar -C /home/log -zcvf /home/archive/xyz.gz --transform 's_.*/__' /home/log
This however will also write an entry for each encountered directory. If you don't want that, you can use find to find only regular files and pass them to tar on stdin like this:
cd /home/log
find -type f -print0 | tar -zcvf /home/archive/xyz.gz --transform 's_.*/__' --verbatim-files-from --null -T -
Note that this may create multiple entries with the same name in the tar archive, if files with the same name exist in different subdirectories. Also you should probably use the conventional .tar.gz or .tgz extension for the compressed tar archive.

Staying in another folder, how can i tar specific files from another directory?

Thanks for your support,
I have the following folder structure on my linux laptop
/home
/A
/B
In folder "B", I have files of type *.csv, *.dat.
Now from folder A, How can I create a tar file containing files *.csv in folder B. I am running the command in folder A
Here is the command, I have tried but its not working,
In /home/A folder, I am running the following command
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 *.csv
and also tried with this,
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 --wildcards *.csv
For both of the commands, I get the following error,
tar: *.csv: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
In the tar file, I dont want to include the whole folder structure and this is the reason, I am using option -C (capital)
Moreover, the following command works but it tars all *.csv and *.dat files.
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 .
You can edit the names in the tar command to remove the path. (Assuming that you have GNU tar.)
tar -cf /home/A/Sample1.tar --transform 's,.*/\([^/]*\),\1,' /home/B/ZSBSDP4/*.csv
Note that if you specify more source directories on the command, you could accidentally put more than one file with the same name in the tar file. Then when unpacking, the last one will overwrite those with the same name that precede it.
You can use the --exclude=PATTERN option:
tar -cf /home/A/Sample1.tar -C /home/B/ZSBSDP4 . --exclude=*.dat
Other "local file selection" options listed in the man page: http://linux.die.net/man/1/tar

Backup directories in home with tar

I want to make a backup of each directory in /home separately and each directory tar (backup) files to enter into a specified directory. Under linux ubuntu.
You can do something like this in bash:
for d in */; do short=${d%/}; tar -cvf ${short}.tar $short ;done
Or, more verbosely:
cd # go home
for d in */ # for all directories
do
short=${d%/} # strip off trailing slash
tar -cvf ${short}.tar $short # tar up directory into file with same name but ".tar" extension
done
So, if you have doirectories $HOME/Documents and $HOME/Music, you will get 2 tarfiles in your home directory called Documents.tar and Music.tar
To clarify ... I want to make a backup of all directories, for example to /home/user file is named backup-2014.02.02.tar and is located in the directory /home/user /backups. I'm doing a backup of the entire /home directory with the following script:
#!/bin/bash
today=$(date '+%Y.%m.%d')
tar czf /var/backup/backup_"$today".tar.gz /home
Yes, but I want to go to backups in the following way ... If the directory was /home/user file batskup-user-2014.02.04.tar.gz to go to the directory /home/backups

tar/gzip excluding certain files

I have a directory with many sub-directories. In some of those sub-directories I have files with *.asc extension and some with *.xdr.
I want to create a SINGLE tarball/gzip file which maintains the directory structure but excludes all files with the *.xdr extension.
How can I do this?
I did something like find . -depth -name *.asc -exec gzip -r9 {} + but this gzips every *.asc file individually which is not what I want to do.
You need to use the --exclude option:
tar -zc -f test.tar.gz --exclude='*.xdr' *
gzip will always handle files individually. If you want a bundled archive you will have to tar the files first and then gzip the result, hence you will end up with a .tar.gz file or .tgz for short.
To get a better control over what you are doing, you can first find the files using the command you already posted (with -print instead of the gzip command) and put them into a file, then use this file (=filelist.txt) to instruct tar with what to archive
tar -T filelist.txt -c -v -f myarchive.tar

Resources