LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
 
LinkBack Search this Thread
Old 07-26-2010, 10:15 AM   #1
rmcban
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Rep: Reputation: 0
How to archive (tar) every subdir


Hello everyone,

I am stuck trying to write a script that does the following :
1. loops through all subdirectories of a given directory
2. for each found subdirectory, first create an archive carrying the same name as the subdir itself
3. then moves the actual subdirectory to a different path

Here is what I have so far
my base dir is /home/bob/Bureau, and it contains two sub dirs, "florissant 86 a" and "saule 84"
I would like to create one archive for each subdir in /media/public/atelierPhotoArchive and then move the folder entirely to /tmp/photo.write

everything goes well until I have to either tar or loop through the file with spaces in names

#version 1
find /home/bob/Bureau/* -type d -fprintf lst %f\\n
for t in `cat lst`; do echo $t; done
This last statement outputs different lines after each space in my lst file
Question 1: Is it possible to make it output once for each line in the file ?

Question 2: Is it possible to do some exec tar in the find command? I had difficulties extracting the "short" name for the archive ("saule 84") without /home/bob/Bureau, it is possible only with the printf %f, but how in the world can I get this value in to the exec option ?

Thans in advance,
rmcb
 
Old 07-26-2010, 12:58 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,539
Blog Entries: 51

Rep: Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604
Code:
find /home/bob/Bureau/* -maxdepth 1 -type d | while read DIRNAME; do ARCHNAME="${DIRNAME//*\//}"
 tar -cf "/media/public/atelierPhotoArchive/${archname// /_}.tar" "${dir}" && mv "${dir}" /tmp/photo.write/
done
?
 
Old 07-26-2010, 02:22 PM   #3
DJ Shaji
Member
 
Registered: Dec 2004
Location: India
Distribution: Debian 6.0 (Squeeze) Linux 3.2.9-rt15 #2 PREEMPT RT Sat Mar 10 22:55:52 IST 2012 i686 GNU/Linux
Posts: 260
Blog Entries: 15

Rep: Reputation: 33
You could try xargs
 
Old 07-26-2010, 07:48 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,202

Rep: Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796
@unSpawn - ? is right ... i am struggling to follow your short script:

ARCHNAME="${DIRNAME//*\//}" <-> ${archname// /_} : Are these not the same variable?? If so the case is not going to work.

"${dir}" : Where does this variable come from??
 
Old 07-26-2010, 08:13 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You could simply use:
for dir in */; do
tar -cf "${dir}.tar" "$dir"
done
Since tar descends the directory tree for you.

Adding the date to the tarball name would be a good idea.

You could also use:
for dir in */; do
tar -cf "${destdir}/${dir}.tar" "${dir}"
done
to create the tarball in the destination directory instead of moving it afterwards.

If the destination directory is on another machine you reach via ssh or rsh:
for dir in */; do
tar -cf - logs/ | ssh jschiwal@netcow 'cat - >Documents/'"${dir}.tar"
done

You want to use pubkey authentication and use ssh-agent & ssh-add to save your passphrase before running this command or script.

If you want to handle files from a more complicated find search criteria, use the -print0 option for find and the -0 option for xargs. This will take care of the spaces in filename problem.

If you process filenames listed in a file, use the `tr' program to convert returns to nulls:
cat filelist | tr '\n' '\0' | xargs -0 tar cf backup.tar

For more complicated commands, you can use finds -printf command to create a script to run.

Last edited by jschiwal; 07-26-2010 at 08:15 PM.
 
Old 07-27-2010, 03:52 AM   #6
rmcban
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks to all who replied, I will give a try to all solutions and keep you posted as to what will come out of this, and mark the thread as solved.
 
Old 07-27-2010, 04:56 AM   #7
rmcban
LQ Newbie
 
Registered: Apr 2010
Posts: 4

Original Poster
Rep: Reputation: 0
unSpawn: Thanks a lot, your solution appears to be working (I have not gotten to the tar and mv part yet, but so far so good).

Now, I would like to dig deeper into this, as the whole purpose is as much to make this thing work as to acquire the knowledge of how to do it.

So this si the simplified unSpawn code:

Code:
find /home/bob/Bureau/* -maxdepth 1 -type d |
	 while read myString; do 
		echo ${myString} 
		echo ${myString//*\//}
	done
I think I grasped the pipe, the read and the placeholders in the first three lines.
What puzzles me beyond saying is this: myString//*\//
which gives this "saule 84" out of this "home/bob/Bureau/saule 84"

What is this operation called? Substring replacement ? Where can I find more about this, the characters are too generic to search...

Once again than you all in advance for the time and energy that you put in educating and helping others.
 
Old 07-27-2010, 06:19 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,202

Rep: Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796
This page should answer your questions.
 
  


Reply

Tags
find, script, shell, tar


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in TAR - tar: GNU features wanted on incompatible archive format. kuldeep.k Linux - General 3 08-14-2009 11:09 AM
tar this does not look like a tar archive exit depalyed from previous error tasay Linux - Software 1 07-01-2009 03:34 PM
Piping tar bzcat to add a file to a tar.bz2 archive DaveQB Linux - Software 0 06-02-2008 08:28 PM
Delete specific archive names in subdir infonlinebr Linux - Newbie 2 05-28-2008 10:19 PM
Tar gives error when creating a tar file archive davidas Linux - Newbie 10 04-13-2004 12:35 AM


All times are GMT -5. The time now is 02:45 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration