LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-17-2007, 09:21 AM   #1
klavuzkarga
LQ Newbie
 
Registered: Dec 2007
Posts: 11

Rep: Reputation: 0
bash - tar command


i have a final question ...

i have a list of directories inside a string.
i want to tar these directories. i wrote this code;

Code:
directories="/home/cumhur/code/scripts /home/cumhur/Desktop/vim71/src"
destination="/home/cumhur/Desktop/compressed"

for i in $directories
do
	tar -cvzf "$destination$date-$i.tar" "$i"
done
what is wrong in the code ?
it gives me this result;

Code:
tar: /home/cumhur/Desktop/compressed-/home/cumhur/code/scripts.tar: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
tar: Removing leading `/' from member names
/home/cumhur/code/scripts/
/home/cumhur/code/scripts/deneme.sh
/home/cumhur/code/scripts/untitled folder/
/home/cumhur/code/scripts/untitled folder/deneme
/home/cumhur/code/scripts/untitled folder/program2.c
/home/cumhur/code/scripts/untitled folder/qwer.sh
.
.
edit: these directories exist.

Last edited by klavuzkarga; 12-17-2007 at 09:23 AM.
 
Old 12-17-2007, 09:34 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You are simply trying to do something like
Code:
tar -cvzf "/home/cumhur/Desktop/compressedMon Dec 17 15:28:00 CET 2007-/home/cumhur/code/scripts" "/home/cumhur/code/scripts"
which is obviously wrong. You can first extract the name of the directory from its absolute path and eventually format the date to avoid blank spaces inside the file name. For example
Code:
directories="/home/cumhur/code/scripts /home/cumhur/Desktop/vim71/src"
destination="/home/cumhur/Desktop/compressed"

for i in $directories
do
        dir=$(basename $i)
	tar -cvzf "${destination}/$(date +%Y-%m-%d)-${dir}.tar" "$i"
done
Also, when you are testing scripts you can put an echo in front of some commands: in this way the commands are not executed but will be echoed after the shell has done all the variable expansions (and you will immediately know if the results are what you expected or not).

Last edited by colucix; 12-17-2007 at 09:38 AM.
 
Old 12-17-2007, 09:49 AM   #3
wanghao
Member
 
Registered: Mar 2007
Distribution: centos
Posts: 126

Rep: Reputation: 15
i think the dir should me more detail define,
you can try this dir=`ls -R youdir`;And i'm unable to describle it
clearly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BackUp & Restore with TAR (.tar / .tar.gz / .tar.bz2 / tar.Z) asgarcymed Linux - General 5 12-31-2006 03:53 AM
need some help with muli-volume tar (ubuntu, bash, gnu tar) MattCarp Linux - General 4 06-05-2006 09:15 AM
bash syntax when envoking tar command in python script tangle Programming 4 02-04-2006 04:22 PM
bash/tar bokini Programming 2 07-30-2005 05:50 PM


All times are GMT -5. The time now is 01:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration