1

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
Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148

3 Answers3

1

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

Christopher Karel
  • 6,442
  • 1
  • 26
  • 34
1
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.

Slartibartfast
  • 3,265
  • 17
  • 16
0

# 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

Abhishek A
  • 429
  • 5
  • 12
  • 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 'SO- stop being evil' 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. :) – Abhishek A Aug 21 '10 at 12:24
  • @Gilles - What can be the possible on-liner solution for this problem? – Abhishek A Aug 21 '10 at 12:27