LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash - execute on each directory within a directory (https://www.linuxquestions.org/questions/linux-newbie-8/bash-execute-on-each-directory-within-a-directory-910120/)

Dizieze 10-25-2011 03:45 PM

bash - execute on each directory within a directory
 
Hello all,
I need some help writing a bash script.

I need to execute a svn-dump on each directory within my svn-directory and then name the dumpfile whatever the directory was called with ".dump" as the extension.

Here is what I have so far:

Code:

#!/bin/bash

directory=/var/svn
extension=".dump"

for file in $( ls $directory )
do
rename=$directory$extension
svnadmin dump $directory > $rename
done

Within the /var/svn directory I have about 50 svn repositories such as /var/svn/IT.

You can probably tell I have no idea what I'm doing a little help would go a long way. =)

Julian Andrews 10-25-2011 05:55 PM

Your script seems pretty close to the mark to me!

A couple minor notes:

It's best to avoid using 'ls' when you can just use globbing - it's faster and easier to read.

Also, you should always use quotes around filenames in case they have spaces in them.

You can check if a file is a directory with the if [ -d "$file" ] construct.

Working on the assumption that you want the .dump files in the /var/svn folder, here's what I came up with.

Code:

#!/bin/bash

directory=/var/svn
extension=".dump"
for file in "$directory"/*
do
    if [ -d "$file" ]
    then
        svnadmin dump "$file" > "$file$extension"
    fi
done

Let me know if this works for you!

spankbot 10-25-2011 06:10 PM

Also, have a look at svnbak.pl. It takes care of backing up and rotating/archiving...

http://www.zulius.com/freebies/perl-...-repositories/

Dizieze 10-26-2011 12:02 PM

Thanks Julian! That worked great. Glad to know I was kinda close myself =P

I will look into svnbak.pl spankbot. Thanks.


All times are GMT -5. The time now is 04:27 AM.