Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

there are a bunch of filename.gif scattered in /home/ directories. I want all the filename.gif replaced by a /home/shared/filename_default.gif.

I tried something like

find /home/ -name "filename.gif" | xargs <cmd>

but I have no idea to reference what I want to. Wasnt sure to ask this here or stackoverflow

Edit: This works exactly like I explained it, except I not only wanted the data from filename_default to replace filename.gif, but also for filename.gif to be renamed filename_default.gif. So I did mv {} filename_default.gif and mv'ed all the files from their directories to /root/ under the name filename_default.gif ... lol. is there anyway to reference the changing variable in a directory..

for example find /home/ -name "filename.gif" was actually located like this...

/home/brian/dir/1/2/3/filename.gif
/home/beth/dir/1/2/3/filename.gif
/home/andrea/dir/1/2/3/filename.gif

How can I reference brian, beth, andrea... like a wildcard *.

cp /home/shared/filename_default.gif /home/*/dir/1/2/3/filename_default.gif
share|improve this question
add comment

3 Answers

How about this?

find /home/ -name filename.gif -exec mv -f /home/shared/filename_default.gif {} \;

The -exec flag has find execute a command on every found object. The {} indicates the name of the found file. You then have to terminate the shell command with \;. You could arguably use ln -s to symlink those .gifs. That might be easier if you ever want to change your shared .gif without redoing this sort of find and move.

EDIT Good points by Denilson below. Despite the question involving mv, it'll only work once, before the source file is gone. (moved) His advice on the echo command is a good way to double check what's going to happen, before it does.

find /home/ -name filename.gif -exec cp -p /home/shared/filename_default.gif {} \;


--Christopher Karel

share|improve this answer
1  
You should use cp instead of mv. Also, adding echo before the command would be a nice test before the actual run. –  Denilson Sá Aug 20 '10 at 19:11
    
Good advice. Added to answer. Thanks. –  Christopher Karel Aug 20 '10 at 19:20
add comment
find /home -name filename.gif -type f -printf "%h\0" | xargs -0 --max-args 1 cp -l  /home/shared/filename_default.gif

This isn't the wildcard you asked for, but it does accomplish the first part of your task, namely putting "filename_default.gif" in the target directories. If that works the way you want, you can then replace the 'cp -l /home/shared/filename_default.gif' portion of the command with 'rm', and replace '-printf "%h\0"' with '-print0' to remove the original file.

This presumes you want to take the contents of /home/shared/filename_default.gif and put those contents in a file named 'filename_default.gif' in all of the directories that contain 'filename.gif', removing 'filename.gif' (and thus effectively replacing 'filename.gif' named files with an identical image in a file named 'filename_default.gif')

If you want to replace 'filename.gif' but leave the name the same, then replace '-printf "%h\0"' with '-print0' and omit the rm step.

share|improve this answer
add comment

# find -type f -name 'filename.gif' -exec cp -v /home/shared/filename_default.gif {} \;

This will replace all your "filename.gif" files with the contents of "filename_default.gif" but won't rename "filename.gif" to "filename_default.gif"

To rename use following command

# rename 's/filename.gif/filename_default.gif/' $(find -type f -name 'filename.gif')

HTH

share|improve this answer
    
Don't parse $(find ...), this will fail if a file name contains a space. Use -exec the second time as well: find -type f -name 'filename.gif' -exec rename 's/filename.gif/filename_default.gif/' {} +. Also, note that this is using the rename provided by Debian and Ubuntu, which comes from the Perl examples package; there are different rename commands floating around. –  Gilles Aug 21 '10 at 10:16
    
@Gilles - Thanks for the clarifications. I will remember your comments about find. Also I didn't know that rename is available only on Debian/Ubuntu. Thanks for that too. :) –  greenmang0 Aug 21 '10 at 12:24
    
@Gilles - What can be the possible on-liner solution for this problem? –  greenmang0 Aug 21 '10 at 12:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.