Copy files from find command output [duplicate] - linux

This question already has an answer here:
Solution to error 'find: missing argument to -exec' with find -exec cp {} TARGET_DIR + [closed]
(1 answer)
Closed 5 months ago.
i am using below command to enlist the direcoties with name containing linuxS_x86 .
" find src/ -type d -name 'linuxS_x86' "
it is giving correct output also.
Now i want to copy the files from the all the directories enlisted with above command .
Please help me out ..?

use find with -exec
find src/ -type d -name 'linuxS_x86' -exec cp -rf {} <some destination> \;
{} is the placeholder to pass a single file name to -exec called command, and \; is to finish -exec.
or else you could use ls instead of find. Then pass found files to xargs to do cp command.
ls linuxS_x86/* | xargs -n 1 -I{} cp {} ./bbb

Related

Rename all files containing a specific string [duplicate]

This question already has answers here:
Bash command-line to rename wildcard
(2 answers)
Closed 7 months ago.
I want to rename files of the type file_name (1).extension to file_name.extension. How can i use rename to delete the " (1)" string ?
you can use the find [path_to_dir] -type f -name "*file_extension*" -exec sh -c 'x="{}"; mv "$x" "${x}_renamed"' \;
In my case, i find all .txt files in a folders, and rename it to filename_renamed.
find . -type f -name '*txt*' -exec sh -c 'x="{}"; mv "$x" "${x}_renamed"' \;

linux how to move listed files? [duplicate]

This question already has answers here:
Find and copy files
(5 answers)
Closed 3 years ago.
I tried to redirect the result in a directory but it won't let me do it. What am I doing wrong ?
find Documents/ -type f -regextype posix-egrep -regex ".+\.(doc|err|out)$" > Poubelle/
bash: Poubelle/: Is a directory
What I'm trying to do is move the listed files in Poubelle.
Thank you in advance
Like #Barmar said in the comments you need to use exec command with mv and cp.
you can try using below command :
Moving files to Dir :
find Documents/ -type f -regextype posix-egrep -regex ".+\.(doc|err|out)$" -exec mv {} Poubelle/ \;
Copying files to Dir :
find Documents/ -type f -regextype posix-egrep -regex ".+\.(doc|err|out)$" -exec cp {} Poubelle/ \;
And you can also use rsync. A simpler form is :
rsync source destination
For other options read here.

Stuck trying to copy files to a new directory under specific guidelines

I'm trying to copy all files from a folder that start with a capital letter into another folder.
So far i've used the find command to actually find the files
find /examplefolder -type f -name "[[:upper:]]*"
and i'm able to find them no problem
I tried to just replace the find command with cp and that does not work then i tried to pipe into the cp command and I failed yet again
Use the -exec option for find
find /examplefolder -type f -name "[[:upper:]]*" -exec cp {} /my/new/shiny/folder/ \;
I was able to do it by piping in to cp with xargs and the -I argument
find /examplefolder -type f -name "[[:upper:]]*" | xargs -I % cp % /copied_directory

How to copy the recent updated multiple files in another directory in Solaris

I want to copy the recently updated multiple file into another directory.
I am having 1.xml,2.xml,3.xml.... in this directory recently someone updated file or added new file into the directory,So i want to copy those files into the destination directory ..Its like synchronization of 2 directories.
For that I have tried below commend
find home/deployment/server/services/ -type f -mtime 1 | xargs cp /home/application/
and below one also
find home/deployment/server/services/ -type f -mtime 1 -exec cp /home/application/
I am not getting any file into destination after updating 1.xml file,So I have added new file 4.xml even that also not updating in destination directory.
How to process recently updated or newly added multiple files.
Thanks in advance.
Short answer:
use xargs to mv the "find" directory into another directory
Long answer: As I recall (not tested) for exec syntax is
find . -type f --mtime 1 -exec cp {} /destination/path/ +
"{}" is an argument which came from command "find"
For xargs
find . -type f --mtime 1 | xargs -0 -I {} cp {} /destination/path/
I do this often but use \; instead of + and usually -cnewer rather than -mtime.
\; executes the cp command on files individually instead of as a group.
+ executes as a group with as many paths as xterm will take. It may do this multiple time if there are a lot of files.
the \ in front of the ; option is required or bash will think it is the end of the command.
find ./ -mtime -1 -exec cp {} /path/ \; -print
Use the -print at the end to get a list of the files that were copied.

find and copy file using Bash [duplicate]

This question already has answers here:
Find and copy files
(5 answers)
Closed 4 years ago.
Anybody has an alternate way of finding and copying files in bash than:
find . -ctime -15 | awk '{print "cp " $1 " ../otherfolder/"}' | sh
I like this way because it's flexible, as I'm building my command (can by any command) and executing it after.
Are there other ways of streamlining commands to a list of files?
Thanks
I would recommend using find's -exec option:
find . -ctime 15 -exec cp {} ../otherfolder \;
As always, consult the manpage for best results.
I usually use this one:
find . -ctime -15 -exec cp {} ../otherfolder/ \;
If your cp is GNU's:
find . -ctime 15 -print0 | xargs --no-run-if-empty -0 cp --target-directory=../otherfolder
You can do it with xargs:
$ find . -ctime 15 -print0 | xargs -0 -I{} cp {} ../otherfolder
See also grep utility in shell script.
Use this for copy and many other things:
for f in $(find /apps -type f -name 'foo'); do cp ${f} ${f}.bak; cmd2; cmd3; done;
-exec is likely the way to go, unless you have far too many files. Then use xargs.

Resources