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