-   Linux - Newbie (
-   -   How to archive (tar) every subdir (

rmcban 07-26-2010 11:15 AM

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,

unSpawn 07-26-2010 01:58 PM


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/


DJ Shaji 07-26-2010 03:22 PM

You could try xargs

grail 07-26-2010 08:48 PM

@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??

jschiwal 07-26-2010 09:13 PM

You could simply use:
for dir in */; do
tar -cf "${dir}.tar" "$dir"
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}"
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"

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.

rmcban 07-27-2010 04:52 AM

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.

rmcban 07-27-2010 05:56 AM

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:


find /home/bob/Bureau/* -maxdepth 1 -type d |
        while read myString; do
                echo ${myString}
                echo ${myString//*\//}

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.

grail 07-27-2010 07:19 AM

This page should answer your questions.

All times are GMT -5. The time now is 03:36 PM.