recursive saving of files and folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting recursive saving of files and folders
# 1  
Old 12-14-2010
recursive saving of files and folders

Hi all

I have a bash script, that loops through a folders files and all subfolders for .shtml files.

It looks for a string and replaces it, creating a backup of the original file.

This all works great, but I'd like, when the backup is done, that the files are then also created in their respective folders.

So if example.shtml is found in /var/www/files/example/example.shtml
And the backup folder is /var/www/files/backup

Then the file should be in /var/www/files/backup/example/example.shtml

My current code is:

PHP Code:
#!bin/bash
OLD="This is a"
NEW="I am a"
BPATH="../backup"

find . -name '*.shtml' -type f |
while 
read filename
do
    /
bin/cp -f $filename $BPATH
    sed 
-"s/$OLD/$NEW/g" $filename
done 
All help muchly welcome

Regards
# 2  
Old 12-14-2010
Code:
#!bin/bash
OLD="This is a"
NEW="I am a"
BPATH="../backup"

find . -name '*.shtml' -type f |
while read filename
do
    RPATH="`dirname $filename`"
    mkdir -p "$BPATH/$RPATH"
    /bin/cp -f $filename "$BPATH/$RPATH"
    sed -i "s/$OLD/$NEW/g" $filename
done

# 3  
Old 12-14-2010
Perfect!

Thank you

On another note, I want to replace:
PHP Code:
OLD="This is a" 
with a URL, but I keep getting the following error:

PHP Code:
sedcouldn't open file ww.google.co.uk//g: No such file or directory 
Do i need some kind of regex or can sed handle urls in terms of strings?

Thanks

Last edited by terrid; 12-14-2010 at 08:17 AM.. Reason: Need additional help
# 4  
Old 12-14-2010
Quote:
Originally Posted by terrid
PHP Code:
sedcouldn't open file ww.google.co.uk//g: No such file or directory 
Do i need some kind of regex or can sed handle urls in terms of strings?
Think you should another separator than '/' for sed, something that isn't anyway in an url. You can try with '|' so sed -i "s/$OLD/$NEW/g" $filename would become sed -i "s|$OLD|$NEW|g" $filename
# 5  
Old 12-14-2010
Ah, I wasn't sure i could use another separator other than '/'.

But replacing '/' with '|' worked a charm!

Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy files/folders and show the files/folders?

Hi, So i know we use cp -r as a basic to copy folders/files. I would like this BUT i would like to show the output of the files being copied. With the amazing knowledge i have i have gone as far as this: 1) find source/* -exec cp -r {} target/ \; 2) for ObjectToBeCopied in `find... (6 Replies)
Discussion started by: Imre
6 Replies

2. UNIX for Beginners Questions & Answers

Recursive remove files

Hi folks, I have several directories with multiple files of all kinds in them. For example directory ###### contains: ######B1.TIF ######B2.TIF ........... ######B8.TIF ######.tar ######.txt ######.jpg ######r ######r.hdr ######rSVD ######rSVD.hdr ######t ######t.hdr How do I... (8 Replies)
Discussion started by: spirospap
8 Replies

3. Shell Programming and Scripting

Saving files with file name as output

Hi, i need help with a file creation of an output program. I've got a program that with #find creates an output for each files in a directory. If i give this command : -o spec$(date -u +%Y%m%dt%H%M) it creates just one file, overwriting all the others since it is the creation date .... (2 Replies)
Discussion started by: Board27
2 Replies

4. Shell Programming and Scripting

Input not saving correctly in both files

If the user inputs two variants separated by a comma, the below command is supposed to write both variants to the GJ-53.txt and then to out.txt. Both are written to the GJ-53.txt, however only one is written to out.txt and I'm not sure why. Thank you :). gjb2() { printf "\n\n" ... (1 Reply)
Discussion started by: cmccabe
1 Replies

5. HP-UX

Recursive copy of Folders with files

Dear All, I will appreciate any help received. Our system is running on hpux v1 My problem is as follows: We have many customer folders with name fd000100, fd000101 and so on e.g. (Testrun)(testsqa):/>ll /TESTrun/fd000100 total 48 drwxrwx--- 2 fq000100 test 96 Jun 27 2004... (17 Replies)
Discussion started by: mhbd
17 Replies

6. UNIX for Advanced & Expert Users

Recursive grep with only certain types of files

Can I please have some ideas on how to do a recursive grep with certain types of files? The file types I want to use are *.c and *.java. I know this normally works with all files. grep -riI 'scanner' /home/bob/ 2>/dev/null Just not sure how to get it to work *.c and *.java files. (5 Replies)
Discussion started by: cokedude
5 Replies

7. UNIX for Dummies Questions & Answers

Searching for folders/parent folders not files.

Hello again, A little while back I got help with creating a command to search all directories and sub directories for files from daystart of day x. I'm wondering if there is a command that I've overlooked that may be able to search for / write folder names to an output file which ideally... (2 Replies)
Discussion started by: Aussiemick
2 Replies

8. Shell Programming and Scripting

Saving files

Hi all, I need to save my files at c, d or any drive location via script. Requirement. Say for example i have 10 files at location /usr/bi/ci location. 10 files naming a.ksh b,ksh c.ksh and so on I want to save the files and its content at some location (any drive on local... (4 Replies)
Discussion started by: j_panky
4 Replies

9. Shell Programming and Scripting

Recursive Replication of Unix folders to Windows

Requirements: ftp files recursively from unix to windows. Replicate directory paths on unix (source) to windows (destination) and place files in their respective folders. There are no set number of files per directory nor fix number of dirA or dirB etc.... Source OS: Solaris... (5 Replies)
Discussion started by: mlv_99
5 Replies

10. UNIX for Dummies Questions & Answers

Renaming file in Recursive folders

I have UWin version of Unix for Destop I have files with extension *.abc. I want to rename it to *.csv. Some of the filenames have spaces. I also to rename the files with extension *.abc in the corresponding subfolders ex == abc def.abc ghi jkl.abc mnopqr.abc uvwxyz.abc rename as... (4 Replies)
Discussion started by: bobbygsk
4 Replies
Login or Register to Ask a Question