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)...
find . -type f -mtime 1 -exec cp PathToMySourceFolder/* pathToMyDestinationFolder/ \;
. PS: why do you need the last\;
in the snippet you wrote? – John Nov 24 '15 at 13:55find
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, thefind
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 beforefind
runs! – Rmano Nov 24 '15 at 14:09{}
snippet will make sure that only the files that are found will be copied? – John Nov 24 '15 at 14:24{}
has no meaning for the shell, and it's interpreted byfind
directly. I think you should also understand how the shell works --- may I suggest http://www.tutorialspoint.com/unix/unix-what-is-shell.htm and around? – Rmano Nov 24 '15 at 14:26-p
flag, i.e.cp -p
if you want to preserve the original timestamps of the file. – s.k Sep 16 '21 at 17:47