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
Best Answer
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 useln -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