Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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
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
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>
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.
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".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.