Shell script to copy particular file from directories recursively


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to copy particular file from directories recursively
# 1  
Old 01-21-2013
Shell script to copy particular file from directories recursively

I have directory path in which there are several sub directories. In all these sub dir there will be one env.cnf file. I want to copy this env.cnf file from
each sub dir's and place them in destination path by creating same filename as sub dir_env.cnf.

After copying env.cnf files from source to destination dir, I have to run few unix commands(grep) on each files and print the grep command o/p in result files in output dir.

I have seen that we can use the below command to do the recursive file copy part but don't know how to process grep command on each files

Code:
 
find /src_path/to/ -iname '*.cnf' -exec cp {} /new/directory/ \;

Ex:
path : /tmp/config/ has 3 sub dir's
Code:
SubA ---- has env.cnf
SubB ---- has env.cnf
SubC ---- has env.cnf
.
.
.

after running the script, it should place the env.cnf file in input dir in /tmp/Config/input as name:
Code:
SubA_env.cnf
SubB_env.cnf
SubC_env.cnf
.
.
.

now run the same script again on each files present in the input dir to perform grep command and put the output in output dir in /tmp/Config/output/
Code:
SubA_env.txt
SubB_env.txt
SubC_env.txt

ManyThanks,
# 2  
Old 01-21-2013
One solution is to pipe the find output to a read loop and process pathname within loop:

Code:
find /src_path/to/ -iname '*.cnf' -print | while read src
do
   dest=$(dirname "$src" | sed 's:.*/::')
   dest="${dest}_$(basename "$src")"
   log=$(basename "$dest" .cnf).txt
   cp "$src"  "/tmp/Config/output/$dest"
   grep SEARCH_STRING "/tmp/Config/output/$dest" > "/tmp/Config/output/$log"
done

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-23-2013
Thanks Chubler. your solution did work as expected but am trying change the ouput file name as *_result.txt. I tried to update this line like this but didn't work
Code:
log=$(basename "$dest"_result .cnf).txt

Could you please tell me what am doing is correct in above stmt and if possible can you tell me how to acrchive this generated file (input/output) to someother path. so that if we run this script again it should genreated the new set of input/output file then it should generate new compare result file by comparing pervious result and new result file.

like :
1. generate input/result file (already done by above script)

2. If we run the above script again, it should check if there is any files present in output folder if yes, then move this genereated files to someother location with datetime stamp in the filename(archive) . if no files found then just generate files.

3. from step 2, if new set of results files generated (if files archived), then should compare archived result file to newly genereated result file.
ie, something like this
Code:
sdiff -s old_result_file new_result_file

please do help me on this.

ManyThanks,
# 4  
Old 01-23-2013
Use:
Code:
log=$(basename "$dest" .cnf)_result.txt

for 2 you could do this:
Code:
[ -d /tmp/Config/output ] && mv /tmp/Config/output /tmp/Config/output_$(date +%Y%M%d%H%M)
mkdir -p /tmp/Config/output

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 01-24-2013
Thanks Chubler. it worked as i was expecting. Can you please advise me on how to achive the point 3.

from step 2, if new set of results files generated (if files archived), then should compare archived result file to newly genereated result file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generic script to recursively cd into directories and git pull

Hi all, I'm trying to write a script to recursively cd into my Git projects and pull them, and will later expand it to build my projects as well. I'm having a bit of trouble with my current script, as I want to supply a command line argument to tell it which branch to check out. I can hard... (2 Replies)
Discussion started by: Cows
2 Replies

2. Shell Programming and Scripting

Recursively Searcing file in the directories

i have directory dgf in the dgf( some other Sub-dir are there) 00 01 02 03 04 in all the Sub directory there is a SG.csv .. i want the scripts should run one by one Sub-dir and print the result for that particular Sub-dir ..then go to next Sub-Dir and print the result....... please... (6 Replies)
Discussion started by: Aditya.Gurgaon
6 Replies

3. Shell Programming and Scripting

Script to copy certain info from several directories

Hi, I am writing a script to copy certain file name in txt file . It is working fine if I provide a single directory name (for example "/eos/uscms/store/user/pooja04//analysis2012/525/data/doubleele/2012/datav1/" ) where those specific files are present ending with *root . But I want to modify... (14 Replies)
Discussion started by: nrjrasaxena
14 Replies

4. Shell Programming and Scripting

ftp script to copy folders recursively

hi all I want a script that will use ftp to copy folder and sub folders from source server to current server. if i use -r switch then it just copies folders for 5 level. (1 Reply)
Discussion started by: kashif.live
1 Replies

5. Shell Programming and Scripting

Deleting all files recursively from directories while ignoring one file type

Hi, Seems like I need help again with a problem: I want to delete all files from my lets say "Music" Directory inkluding all of the subfolders except for .mp3 and .MP3 files. I tried it with globalignoring mp3 files, finding and deleting all other files, which resulted in all files... (3 Replies)
Discussion started by: pasc
3 Replies

6. Shell Programming and Scripting

Shell script to modify file in several directories

Hi, I want a script shell to automate modifying httpd.conf file for several instances of apache, save httpd.file before changing it, after modifying it and then restart apache. - Replace ServerRoot "xxxx" by ServerRoot "yyyy" of all directories : "... (4 Replies)
Discussion started by: bras39
4 Replies

7. Red Hat

Copy certain file types recursively while maintaining file structure on destination?

Hi guys, I have just been bothered by a fairly small issue for some time now. I am trying to search (using find -name) for some .jpg files recursively. This is a Redhat environment with bash. I get this job done though I need to copy ALL of them and put them in a separate folder BUT I also... (1 Reply)
Discussion started by: rockf1bull
1 Replies

8. Shell Programming and Scripting

Shell script to copy file

Dear all, I have a database with thousands of files with the structure of name is: Filename_hour_year.abc Filename_hour_year_1.abc .............. So what I need is how to write a script that all file with contain the character "_1" will copy to "_2" For example: file name:... (7 Replies)
Discussion started by: hainguyen1402
7 Replies

9. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

10. UNIX for Dummies Questions & Answers

Copy file into directories and sub-directories

Hello- I need to copy a file into multiple directories, and each directory's sub-directories (of which there are 5) Currently, the parent directory is set up like this: dir1 sub-dir1 sub-dir2 sub-dir3 sub-dir4 sub-dir5 dir2 sub-dir1 sub-dir2 sub-dir3 ... (1 Reply)
Discussion started by: penlok
1 Replies
Login or Register to Ask a Question