find and copy file using Bash
I would recommend using find
's -exec
option:
find . -ctime 15 -exec cp {} ../otherfolder \;
As always, consult the manpage for best results.
Find and copy files
If your intent is to copy the found files into /home/shantanu/tosend
, you have the order of the arguments to cp
reversed:
find /home/shantanu/processed/ -name '*2011*.xml' -exec cp "{}" /home/shantanu/tosend \;
Please, note: the find
command use {}
as placeholder for matched file.
How to move or copy files listed by 'find' command in unix?
Adding to Eric Jablow's answer, here is a possible solution (it worked for me - linux mint 14 /nadia)
find /path/to/search/ -type f -name "glob-to-find-files" | xargs cp -t /target/path/
You can refer to "How can I use xargs to copy files that have spaces and quotes in their names?" as well.
In bash, Find, Sort and Copy
Try deconstructing your pipeline to see what is happening.
find . -type f -name '*.mov' -print0 | xargs -0 ls -dtl | head -20 |
gives you a list of 20 newest mov
files. The lost looks like:
-rw-r--r-- 1 ljm users 12449464 Jan 10 16:24 ./05ED-E769/DCIM/215___01/IMG_5902.mov
-rw-r--r-- 1 ljm users 14153909 Jan 10 16:00 ./05ED-E769/DCIM/215___01/IMG_5901.mov
-rw-r--r-- 1 ljm users 13819624 Jan 10 15:58 ./05ED-E769/DCIM/215___01/IMG_5900.mov
So, your xargs|cp
will get this as input.
It will be
cp -rw-r--r-- 1 ljm users 13819624 Jan 10 15:58 ./05ED-E769/DCIM/215___01/IMG_5900.mov /Volume/New_Directory/
If we look at your error message,
cp: illegal option -- w
cp -r
is ok, cp -rw
will produce this message. So that is consistent with what I said.
So, the question is why the -l
in the copy. If you remove the long format, you get exactly what you need.
As a side note why ls -d
, if your find
ensures -type f
?
find . -type f -name '*.mov' -print0 | xargs -0 ls -t | head -20 | xargs -I{} cp {} /Volume/New_Directory/
should do what you want, but remember that you are parsing the output of ls
, which is considered not a good idea.
Personally, I would
find . -type f -printf "%T@ %p\n" |
sort -n |
cut -d' ' -f 2- |
tail -n 20 |
xargs -I{} cp {} /Volume/New_Directory/
Bash script to find files in a list, copy them to dest, print files not found
Assuming that your list is separated by newlines; something like this should work
#!/bin/bash
dir=someWhere
dest=someWhereElse
toCopyList=filesomewhere
notCopied=filesomewhereElse
while read line; do
find "$dir" -name "$line" -exec cp '{}' $dest \; -printf "%f\n"
done < "$toCopyList" > cpList
#sed -i 's#'$dir'/##' cpList
# I used # instead of / in sed to not confuse sed with / in $dir
# Also, I assumed the string in $dir doesnot end with a /
cat cpList "$toCopyList" | sort | uniq -c | sed -nr '/^ +1/s/^ +1 +(.*)/\1/p' > "$notCopied"
# Will not work if you give wild cards in your "toCopyList"
Hope it helps
How to copy files found with grep
Try this:
find . -type f -exec grep -q '^beginString' {} \; -exec cp -t /home/user/DestinationFolder {} +
or
grep -lir '^beginString' . | xargs cp -t /home/user/DestinationFolder
But if you want to keep directory structure, you could:
grep -lir '^beginString' . | tar -T - -c | tar -xpC /home/user/DestinationFolder
or if like myself, you prefer to be sure about kind of file you store (only file, no symlinks), you could:
find . -type f -exec grep -l '^beginString' {} + | tar -T - -c |
tar -xpC /home/user/DestinationFolder
and if your files names could countain spaces and/or special characters, use null terminated strings
for passing grep -l
output (arg -Z
) to tar -T
(arg --null -T
):
grep -Zlir '^beginString' . | xargs --null cp -t /home/user/DestinationFolder
or
find . -type f -exec grep -lZ '^beginString' {} + | tar --null -T - -c |
tar -xpC /home/user/DestinationFolder
Copy files path from a specific directory onwards - Bash
If it's not possible to do cd first
before find
, try to do it in -exec
:
find . -type f -name "*.txt" -exec bash -c 'cd first; echo cp --parent "${1#./*/}" ../destination_folder/' _ {} \;
Related Topics
Linux: Remove File Extensions for Multiple Files
How to Find the Maximum Stack Size
Best Way to Find Os Name and Version in Unix/Linux Platform
Is /Usr/Local/Lib Searched for Shared Libraries
Making Cmake Print Commands Before Executing
"Max Open Files" for Working Process
How to Check If Sed Has Changed a File
How to Decode /Proc/Pid/Pagemap Entries in Linux
Sorting on the Last Field of a Line
Grep Inside All Files Created Within Date Range
Linux Bash, Camel Case String to Separate by Dash
How to Get Position of Cursor in Terminal
Running 32 Bit Assembly Code on a 64 Bit Linux & 64 Bit Processor:Explain the Anomaly
How to Prepend a String to the Beginning of Each Line in a File
How to Set a Custom Baud Rate on Linux
What Is the Purpose of Map_Anonymous Flag in Mmap System Call