LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-26-2013, 11:18 AM   #1
313
Member
 
Registered: Oct 2011
Location: Cardiff
Distribution: Debian 6.0.5
Posts: 39

Rep: Reputation: Disabled
Mass file replacement.


I'm looking for a way to replace all instances of "aubergine.txt" with a new version of that file with the same name (although it can be different if necessary).

The file is in every folder in my music directory (a few hundred folders), eg. "/music/artist/album/aubergine.txt".

I've tried "sed -i 's/old-word/new-word/g' *.txt" but it doesn't seem to work for replacing large amounts of text.
 
Old 07-26-2013, 12:41 PM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
If you have rename from util-linux, you could replace files in the form of rename part_to_replace replacement file1 [file2] ....
With find and xargs you could rename files in one call.
Code:
find -type f -name 'aubergine.txt' -print0 | xargs -0 rename "aubergine" "newname"
Or rename with multiple calls:
Code:
find -type f -name 'aubergine.txt' -exec rename "aubergine" "newname" '{}' \;
Or just rename it through loops and variables with the mv command:
Code:
find -type f -name 'aubergine.txt' -print0 | { IFS=''; while read -rd $'\0' FILE; do mv "$FILE" "${FILE%/*}/newname.txt"; done; }
 
Old 07-26-2013, 02:17 PM   #3
313
Member
 
Registered: Oct 2011
Location: Cardiff
Distribution: Debian 6.0.5
Posts: 39

Original Poster
Rep: Reputation: Disabled
Just to confirm, these will actually replace and not just rename the file?
 
Old 07-26-2013, 02:19 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
if the contents of the File are the same, but you have many copies then you should use a symbolic link

save you new file someplace sensible

For this I'm going to use /music/aubergine.txt

Now to create the links

Code:
find /music/*/ -type f -name aubergine.txt -exec ln -sf /music/aubergine.txt {} ';'
all the aubergine.txt files now point to your "master" /music/aubergine.txt
any changes to /music/aubergine.txt ( apart from filename! ) will also be true of the links
Saves space to

-s is create symlink, -f force overwriting existing files/links
 
Old 07-26-2013, 02:26 PM   #5
313
Member
 
Registered: Oct 2011
Location: Cardiff
Distribution: Debian 6.0.5
Posts: 39

Original Poster
Rep: Reputation: Disabled
The point is the contents are different, all my folders have a file called 'blah.txt' containing outdated information.

I want to insert a new, completely different 'blah.txt' into every folder with current info.

Also, it's important for various reasons that the files be independent of each folder, not symbolic.

Even a way to bulk remove the old and copy the new file would do the trick.
 
Old 07-26-2013, 02:43 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by 313 View Post
The point is the contents are different, all my folders have a file called 'blah.txt' containing outdated information.
do you mean '*.txt'
i.e. couldbeanything.txt

Quote:
Originally Posted by 313 View Post
I want to insert a new, completely different 'blah.txt' into every folder with current info.

Also, it's important for various reasons that the files be independent of each folder, not symbolic.
Sorry, from your initial wording I got the impression you wanted to replace all aubergine.txt files with a new aubergine.txt file
Quote:
Originally Posted by 313 View Post
Even a way to bulk remove the old and copy the new file would do the trick.
hmm

/path/to/NewTxtFiles/
blah.txt
foo.txt
bar.txt


Like that?


untested
Code:
for File in  /music/*/*/*.txt;do
   echo cp -v /path/to/NewTxtFiles/$(basename $File) $File
done
Will just print the commands
If they look right,
copy'n'paste them ( if only a few )
If *lots*
remove the echo
or
add | sh to the end
e.g. use UpArrow get get last command from history, and just add |sh<enter>
 
Old 07-26-2013, 02:59 PM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
less noise version

Code:
for File in /path/to/NewTxtFiles/*.txt;do
    find /music/*/ -type f -name "$(basename $File)" -exec echo cp -v $File {} ';'
done
only tries to replace what you want to replace

Ohh, assumes no spaces in filenames
 
Old 07-26-2013, 09:22 PM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
The question is, how do you produce this new info? If they are new contents to replace the old contents, where do you get them? If they are files of same name but new contents that would replace (by copying or moving and overwrite, and not creating new) the old ones, where would they come from? Is it necessary to replce the old files with new files or do you intend to just update the information in the old files? Lastly would the new update files would really be of the same name from the old files or would they be new? Please decide about that.

Btw sorry my previous code was meant to rename the files only.
 
Old 07-26-2013, 10:07 PM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
First rename the new version of file with some other name, let's say newversion.txt, then a simple script can do your job:
Code:
#!/bin/bash
find /music -name 'aubergine.txt' -exec ls {} \; > /tmp/list.txt
while read -r file
do
cat newversion.txt > $file
done < /tmp/list.txt
rm /tmp/list.txt

Last edited by shivaa; 07-26-2013 at 10:10 PM.
 
Old 07-27-2013, 03:41 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by shivaa View Post
Code:
#!/bin/bash
find /music -name 'aubergine.txt' -exec ls {} \; > /tmp/list.txt
With process substitution in Bash you could save the use of temporary files.
Code:
while read -r file; do
    cat newversion.txt > $file
done < <(exec find /music -name 'aubergine.txt' -exec ls {} \;)
Also why do you have to add -exec ls {} \;? And quoting may also be a better idea for preventing implementation-variant expansions when spaces on filenames are encountered: "$file".
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File server replacement moog7 Linux - Server 3 03-19-2013 06:42 PM
replacement with sed on a large .sql db file duzap Linux - General 3 04-19-2010 04:40 PM
/dev/floppy file replacement queuecumber Linux - General 2 02-10-2004 06:35 PM
file replacement selfsck Linux - General 4 12-19-2003 08:56 AM
buged file & replacement lynxgogo Linux - Software 1 08-23-2003 07:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration