Copying files based on modification date in Linux

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 backticks, often in the upper left corner of the 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

One can also select the exact date and time other than going back to a certain number of days:

cp  `find . -type f -newermt '18 sep 2016 20:05:00'` FOLDER

The above copies all the files in the directory that were created after 18 September 2016 20:05:00 to the FOLDER (three 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. The first is for GNU, and the 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 {} +

Use this command:

for i in `ls -lrt | grep "jul" | awk '{print $9}' `; do cp $i* /some/folder/; done

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).

Tags:

Linux

Bash

Cp