#!/bin/bash
source="/home/user/work/tar/deneme"
source2="/home/user/work/tar/deneme1"
for i in {1..5}
do
tar -czvf $source2/$i/$i.tar.gz $source/$i/
done
I get this error message.
tar: Removing leading/' from member names`
this is my script and error. there are a lot of questions here but my problem doesn't solve. I run script than script create .tar.gz file. But if I unzip with tar -xzvf 1.tar.gzthis command, my file created in full path like home/user/work/tar/deneme/1/1-1.txt.
Do you have any idea?
I try some of ways.
For examle
Find /SED to convert absolute path to relative path within a single line tar statement for crontab
https://unix.stackexchange.com/questions/59243/tar-removing-leading-from-member-names/59244
This is because GNU tar remove leading / (by default). To avoid it you can rewrite your script on this way:
#!/bin/bash
cd /home/user/work/tar
source="deneme"
source2="deneme1"
for i in {1..5}
do
mkdir -p ${source2}/${i}
tar -czvf ${source2}/${i}/${i}.tar.gz ${source}/${i}/
done
Thank you for all your comments and answer.
I find the solution. I change some of codes. which is inside for loop
mkdir $source2/$i
cd $source/
tar -czvf $source2/$i/$i.tar.gz $i/*
Related
I am trying to tar a file and get it's output store in a variable.
I tried this but it is not working:
resulting_tar=$(tar -zcf "$(date '+%Y-%m-%d').tar.gz" folder)
Any idea how do I go about it?
By default, tar does not report the name of the file created. In fact, it doesn't say anything unless you tell it to, and the options given don't tell it to say anything.
Note that tar doesn't tell you what file it created. You tell tar what file to create.
You'll need to capture the name of the file in a variable and report it yourself:
file="$(date '+%Y-%m-%d').tar.gz"
tar -czf "$file" folder
echo "$file"
Try running tar -czf /dev/null folder; you won't see anything from (most implementations of) tar — and that's not because I specified /dev/null. Specify a name if you prefer: tar -czf junk.tar.gz folder and watch the (lack of) output — and remember to remove junk.tar.gz.
You might want to think about including the folder name in the tar file name, too.
folder="…whatever…"
file="$folder-$(date +'%Y-%m-%d').tar.gz"
tar -czf "$file" "$folder"
echo "$file"
EDIT: No longer applicable after further clarification. Leaving for posterity.
You're likely looking for both stdout and stderr. You can combine the two output streams by appending 2>&1 to your command:
resulting_tar=$(tar -zcf "$(date '+%Y-%m-%d').tar.gz" folder 2>&1)
I am trying to create a tar file of a folder, which has a lot of files to be excluded. So I wrote a script (mytar):
#!/usr/bin/env bash
# more files to be included
IGN=""
IGN="$IGN --exclude='notme.txt'"
tar --ignore-failed-read $IGN -cvf "$1" "$2"
# following command is working perfectly
# bash -c "tar --ignore-failed-read $IGN -cvf '$1' '$2'"
Test folder:
test/
notme.txt
test.txt
test2.txt
If I execute the script, it creates a tar file but doesn't exclude the files I have listed in IGN
Apparently, the command is:
tar --ignore-failed-read --exclude='notme.txt' -cvf test1.tar test
The command is working perfectly fine if it's directly executing in the shell. Also I have found a workaround for the script: using bash -c in script file
bash -c "tar --ignore-failed-read $IGN -cvf '$1' '$2'"
I am wondering and trying to figure out it,
Why this simple command is not working without bash -c?
Why it's working with bash -c?
Output:
First output shouldn't container notme.txt file like later
UPDATE 1 script updated
This has to do with the way bash expands variables in its shell.
When you set:
IGN="--exclude='notme.txt'"
it will be expanded as :
tar --ignore-failed-read '--exclude='\''notme.txt'\''' -cvf test1.tar test
And as such tar will look to exlcude a file named \''notme.txt'\'', which it won't find.
You may use:
IGN=--exclude='notme.txt'
which will be be interpreted correctly after shell expansion and tar will know it, but I would rather suggest you use your variable to only store the file name to be excluded:
IGN="notme.txt"
tar --exclude="$IGN" -cvf ./test1.tar ./*
in following command single quotes are syntactical (not literal, filename argument is not literaly surounded by quotes) to prevent shell for splitting argument in the case it contains a space or a tab
tar --ignore-failed-read --exclude='notme.txt' -cvf test1.tar test
the closest is to use array instead of string variable :
ign=( --exclude='notme.txt' )
tar --ignore-failed-read "${ign[#]}" -cvf test1.tar test
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
Im trying to tar a set of subfolders and then tar its parent folder afterwards via a ruby script.
The structure is as follows:
/x/y/z/ParentFolder/Subfolder1
/x/y/z/ParentFolder/Subfolder2
/x/y/z/ParentFolder/Subfolder3
/x/y/z/ParentFolder/Subfolder4
So what i want to end up with is Subfolder1.tar.gz,Subfolder2.tar.gz,Subfolder3.tar.gz,Subfolder4.tar.gz all contained in ParentFolder.tar.gz.
My problem at the moment is that im able to tar the parent folder with its subfolders but it structure remains as /x/y/z/ParentFolder/SubFolder1----4
tarParentFolder = "tar -zcvf /x/y/z/ParentFolder.tar.gz /x/y/z/ParentFolder 2>/dev/null"
`#{tarParentFolder}`
I have searched around but cannot seem to find a solution to this,
Anybody got any ideas?
Thanks
The answer to how to get the path to be relative to the right path is to use the -C tar option. That's a capital C. The parameter you pass is the directory from which you want the tar to start relative.
so you would do:
tar -zcvf /x/y/z/ParentFolder.tar.gz -C /x/y/z ParentFolder
But ... you should also probably think twice about putting tars in tars. You should be fine just tarring up the containing dir.
For creating tar archives containing multiple files/folders use this:
$ mkdir f1 f2
$ tar -czf tar.tgz f1 f2 # creates the tar
$ tar -tzf tar.tgz # lists tar contents
f1/
f2/
f3/
$
So you should write something like:
tar -zcvf /x/y/z/ParentFolder.tar.gz /x/y/z/Subfolder{1,2,3,4}
Okay so essentially what I'm doing, is I'm taking all the directories inside of the /servers/ folder, and moving them to a secondary hard drive mounted at /media/backupdrive/. This script is ran once a day, so it makes the directory with the name of the date, and should copy the folders directly over there (The reason I have to do it this way is because my client has limited disk space on his main hard drive and his worlds are upwards of 6-7gb each). Anyway, I can get them to copy the folders to /media/backupdrive/currentdate, but then when I try to compress it, it says it can't compress an empty directory or something along the lines of that.
Here's the code:
#!bin/bash
folderName=$(date +"%m-%d-%y")
mkdir "/media/backupdrive/$folderName"
for i in servers/*; do
cp -rf $i /media/backupdrive/$folderName/
cd /media/backupdrive/$folderName/
tar -C ${i:8} -czvf "${i:8}.tar.gz"
cd /root/multicraft/
done
Sorry for the image, it was on a virtual machine and I had to re-type it, because I couldn't copy and paste.
It looks to me like your tar command is missing its input (e.g., a final "."), and therefore says, "tar: Cowardly refusing to create an empty archive".
Your script appears to work for me with this tar command:
tar -C ${i#servers/} -czvf "${i#servers/}.tar.gz" .
I'd try a slightly different approach. tar by itself doesn't use temporary files, so you could tar the sources directly to the destination and compress them wizh gzip in a second step.
#!bin/bash
dst="/media/backupdrive/$(date +"%m-%d-%y")"
for d in servers/*; do
tarfile="$dst/${d#servers/}.tar"
tar -C "$d" -cvf "$tarfile" .
gzip -9 "$tarfile"
done