The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to use ZFS and rsync to create a backup solution with versioning iBot Solaris BigAdmin RSS 0 03-17-2009 08:20 AM
how to find a file named vijay in a directory using find command amirthraj_12 UNIX for Dummies Questions & Answers 6 10-25-2008 12:37 PM
Create a backup server with Restore iBot UNIX and Linux RSS News 0 02-19-2008 03:40 PM
unable to create any directory that uses numbers as the directory name fed.linuxgossip UNIX for Advanced & Expert Users 16 01-13-2008 08:54 PM
create a ~/www directory? pradnoir UNIX for Dummies Questions & Answers 2 04-28-2002 03:00 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2009
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
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 (permalink)  
Old 05-27-2009
in2nix4life's Avatar
in2nix4life in2nix4life is offline
Registered User
  
 

Join Date: Oct 2007
Location: East Coast
Posts: 58
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 (permalink)  
Old 05-29-2009
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
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 (permalink)  
Old 06-02-2009
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
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 (permalink)  
Old 06-03-2009
cbo0485 cbo0485 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 87
Quote:
Originally Posted by cbo0485 View Post
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.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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




All times are GMT -4. The time now is 07:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0