LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to archive (tar) every subdir (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-archive-tar-every-subdir-822238/)

rmcban 07-26-2010 10: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,
rmcb

unSpawn 07-26-2010 12:58 PM

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

?

DJ Shaji 07-26-2010 02:22 PM

You could try xargs

grail 07-26-2010 07: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 08:13 PM

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.

rmcban 07-27-2010 03: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 04: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:

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.

grail 07-27-2010 06:19 AM

This page should answer your questions.


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