问题
It may be a duplicate question but i could not find the solution for this i want to copy a last 3 months files from one directory to another directory but i could find only to listing the files by using the following command.
find . -mtime -90 -ls
I really don't know how to copy the files by using -mtime
. I'm new to linux please help me.
回答1:
Use the -exec
option for find
:
find . -mtime -90 -exec cp {} targetdir \;
-exec
would copy every result returned by find
to the specified directory (targetdir
in the above example).
回答2:
One can also select the exact date and time other than going back to certain amount of days
cp `find . -type f -newermt '18 sep 2016 20:05:00'` FOLDER
Above copies all the files in the directory that were created after 18 september 2016 20:05:00 to the FOLDER (3 months before today :)
Be careful with the symbol for the find command, it is NOT this one: ' it is this, a backtick: ` date selection is with this: '
If you have files with spaces,newlines, tabs or wildcards in their names, you can use either of the solutions from Stéphane Chazelas, first is for GNU, second is for GNU or some BSDs:
find . -type f -newermt '18 sep 2016 20:05:00' -exec cp -t FOLDER {} +
find . -type f -newermt '18 sep 2016 20:05:00' -exec sh -c 'cp "$@" FOLDER' sh {} +
回答3:
Use this command:
for i in `ls -lrt | grep "jul" | awk '{print $9}' `; do cp $i* /some/folder/; done
回答4:
I guess I would first store the list of files temporarily and use a loop.
find . -mtime -90 -ls >/tmp/copy.todo.txt
You can read the list, if it is not too big, with
for f in `cat /tmp/copy.todo.txt`;
do echo $f;
done
Note: the quotes around cat... are backquotes, often in upper left corner of keyboard
You can then replace the echo command with a copy command:
for f in `cat /tmp/copy.todo.txt`;
do cp $f /some/directory/
done
回答5:
Ex: select day 09/08/2017
ls -l
-rw-rw-rw- 1 root system 943 Aug 09 02:59 File
for j in `ls -l |awk '{ if ($7 == "09") print $9}'`
do
mv $j $Destination;
done
来源:https://stackoverflow.com/questions/18179643/copying-the-files-based-on-modification-date-in-linux