4

How can I copy files in a folder based on when the file was last edited? Lets say I want to copy all my files that I last edited today from c: to my folder bak. How can I achieve that?

I know that a regular copy command can be done like this:

cp source destination

eg.

cp *.c bak

Any suggestions?

2 Answers 2

7

Using find, the files (and only the files) modified in the last day are found by:

find . -type f -mtime 1 

so you can copy them with

find . -type f -mtime 1  -exec cp {} bak/ \; 

Meaning: find all entities under the current directory (.), of type "file" (-type f), modified at least 1 day from now (-mtime 1, but it's subtle, follow the link to learn more), and for each one of them execute the command cp followed by the name of the file that match the previous conditions and by a literal bak/ --- in the exec clause, the closing semicolon (escaped to avoid the shell eating it) closes the command and additionally means that the command is to be executed once for every match.

Notice that the directory tree will be flattened in the bak/ folder, so maybe using an archive format would be better.

For example, this is my script that makes a backup of all files in my working directories modified today and since two days in tar files and then move them to my Dropbox directory:

#! /bin/zsh 
#
cd $HOME
today="today-$(hostname)".tar
twodays="twodays-$(hostname)".tar
mydirs=(bin Documents Templates texmf Desktop) # list your top-level working dirs here  
rm -f $today $twodays
echo -n "Starting today and twodays backups... " 
find $mydirs -type f -mtime -1 -exec tar rf $today {} +
find $mydirs -type f -mtime -2 -exec tar rf $twodays {} +
echo "backups done:" 
ls -lh $today $twodays
echo "Moving to Dropbox" 
mv $today $twodays $HOME/Dropbox
sleep 2
dropbox status

it needs zsh because I'm lazy and didn't try to adapt to the array structure of bash, but surely someone here can do that (hint, hint)...

5
  • Thank you for your answer, and the example script code. It is very helpful! I ended up using find . -type f -mtime 1 -exec cp PathToMySourceFolder/* pathToMyDestinationFolder/ \; . PS: why do you need the last \; in the snippet you wrote?
    – John
    Nov 24, 2015 at 13:55
  • See find manpage; the -exec options need to be closed by a ; or a +, but ; is interpreted by the shell and if you do not quote it, the find command will never see it. --- BTW, I do not understand your command in the comment; you will copy all of your files in the source folder as soon as one of it matches the find condition... and the * is expanded before find runs!
    – Rmano
    Nov 24, 2015 at 14:09
  • Seems like I was a bit quick with my code there. I was not aware that all the files would be copied if one match was found. I only want to copy all the files that matches the query. Using the {} snippet will make sure that only the files that are found will be copied?
    – John
    Nov 24, 2015 at 14:24
  • 1
    Yes. {} has no meaning for the shell, and it's interpreted by find directly. I think you should also understand how the shell works --- may I suggest tutorialspoint.com/unix/unix-what-is-shell.htm and around?
    – Rmano
    Nov 24, 2015 at 14:26
  • Footnote: don't forget to use the -p flag, i.e. cp -p if you want to preserve the original timestamps of the file.
    – s.k
    Sep 16, 2021 at 17:47
0

You can first specify which file that you modified last in your current folder with command

ls -lt

Or, you want to specify which file that you access last with command

ls -ltu

After that, you can copy the file with cp command. For copying multiple files, see this.

4
  • not exactly what I was looking for. This method is useful for manually copying files, but does it does not work if I want to make an automated process using a script.
    – John
    Nov 24, 2015 at 13:49
  • @John then, you could try Rmano answer :)
    – adadion
    Nov 24, 2015 at 13:50
  • I gave Rmano's answer a try, and it works for my case. Your answer is a nice option for quick and simple steps to move files on the go ;)
    – John
    Nov 24, 2015 at 14:02
  • That's great as long as you finally got working answer :)
    – adadion
    Nov 24, 2015 at 14:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .