Find directory and create backup


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find directory and create backup
# 1  
Old 05-27-2009
Find directory and create backup

What I'm attempting to do is create a script that will do a search for directories that meet the following criteria:

Code:
find . -name "config" -type d

this comes back with:

./dir1/anotherDir/test_dir/config
./dir1/anotherDir/test_dira/config
./dir2/test/test_dir/config

The results could be 10 directories or only 2, but once it comes back with the results I need to change directories to the parent directory of the config directory, then run

Code:
tar czf config.tgz config
/dir/to/another/script.sh config.tgz
mv config.tgz_new_file_name /backup/dir

I got most of it down, just the part where I want to CD to the parent directory of each result.
# 2  
Old 05-27-2009
You could try wrapping the find command in a for loop. Not sure which OS or shell you're using but under Bash on Linux:

Save the starting directory:
export startdir="/dir"

Then loop:
for dir in `find . -name 'config' -type d`;do cd ${dir//.\//};cd ..;do your thing;cd $startdir;done

Breakdown:
${dir//.\//} will remove the leading ./ from the find output
cd.. will back up to the parent directory
cd $startdir will return to starting directory for the next iteration through the loop

Hope this helps point you in the right direction.
# 3  
Old 05-29-2009
That works perfectly, now what if the search I'm doing is for a file.

find . -name file.txt;do ....;do my stuff;cd $startdir;done
# 4  
Old 06-02-2009
I understand the {dir//.\//} removes the leading ./, but what would that need to be changed to when I'm searching for a file to remove the file name and the leading ./?
# 5  
Old 06-03-2009
Quote:
Originally Posted by cbo0485
I understand the {dir//.\//} removes the leading ./, but what would that need to be changed to when I'm searching for a file to remove the file name and the leading ./?
Forgot to mention it but I'm using SLES with Bash shell.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find if create time of last created file in a directory is older than 5 minutes

A process xyz is running and creating file1, file2, file3, .... filen. how do i know if the process has stopped and createtime of the last file (filen) is older than 5 minutes? OS is AIX (3 Replies)
Discussion started by: malaika
3 Replies

2. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

3. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

4. SCO

Help about create backup of SCO openserver 5.0.7

hi guys im beginner in unix and have many problem with this. i have one old machine that Sco unix OpenServer 5.0.7 installed on it. i wana backup from all partition of hard disk and restore it on another unix machine. how can i do that ? thing like hard to hard for windows !!! i only know how... (14 Replies)
Discussion started by: farzad226
14 Replies

5. Shell Programming and Scripting

Create database using Backup file

Hi, I have backup file of database in my server. I want to create a that database in the same Mysql Server. How can I do that? Please send the steps to create the database using backup file? Thanks a lot, (1 Reply)
Discussion started by: aish11
1 Replies

6. Homework & Coursework Questions

Create script to add user and create directory

first off let me introduce myself. My name is Eric and I am new to linux, I am taking an advanced linux administration class and we are tasked with creating a script to add new users that anyone can run, has to check for the existence of a directory. if the directory does not exist then it has... (12 Replies)
Discussion started by: pbhound
12 Replies

7. UNIX for Dummies Questions & Answers

find mv and create directory structure

Hi there, I'm trying to pull all my flacs out of my Music collection. I can do it with following command find b/ -name *.flac -exec mv {} flac/ \; which works great except it moves all the flac files to the flac folder. I want it to recreate the original folder the flacs were found in and mv... (8 Replies)
Discussion started by: fistikuffs
8 Replies

8. Shell Programming and Scripting

Create a backup utility

hi there, looking for a tad assistance if at all possible, im trying to create a backup that will backup all files with a particular .ext to a new folder with the same file name but also with the last modified time and date in the file name ie so if the file information.txt was last modified at... (3 Replies)
Discussion started by: jgraham95
3 Replies

9. Filesystems, Disks and Memory

How to create mondo backup for size > 4.5 GB

Hello Friends, I use mondoarchive to take a bootable backup of my system on a DVD. But whenever there are large files on the system, i.e. the size of the entire backup increases beyond 4.5 GB, the mondoarchive utility does not take any backup. This is quite obvious because the size of DVD is... (1 Reply)
Discussion started by: shamik
1 Replies
Login or Register to Ask a Question