Can you tell me any command to sort / copy / remove the directories which have been modified (or created, although Linux does not show it) on a particular date i.e. yesterday and they are inside another directory?
2 Answers
I would use find
and specify -mtime
so for example
cd /var/log/apache2
find *.log -mtime -1 -exec ls -l --time-style=iso {} \;
would output the following:
-rw-r----- 1 root adm 440 09-10 20:00 error.log
-rw-r----- 1 root adm 3944602 09-11 02:08 other_vhosts_access.log
if I wanted to copy them I would do the following:
find *.log -mtime -1 -exec cp -v {} /tmp/ \;
would output:
error.log' ->
/tmp/error.log'
other_vhosts_access.log' ->
/tmp/other_vhosts_access.log'
more on mtime:
For example:
find . -mtime 0 # find files modified between now and 1 day ago
# (i.e., within the past 24 hours)
find . -mtime -1 # find files modified less than 1 day ago
# (i.e., within the past 24 hours, as before)
find . -mtime 1 # find files modified between 24 and 48 hours ago
find . -mtime +1 # find files modified more than 48 hours ago
find . -mmin +5 -mmin -10 # find files modified between
# 6 and 9 minutes ago
-
It is something like this "find /BACKUP/ -type d -mtime +1 -exec mv -i {} /BACKUP/upload_testlink/ \;"– user2552235Sep 11, 2013 at 6:55
You can use piped commands to achieve this. Hope this helps.
rmdir `ls -lt | grep -v "\`date '+%h %d'\`" | awk '{print $NF}'`
-
This breaks if a directory has whitespace in its name. Also it's depending on the
date
variant and time formatting.– slhckSep 11, 2013 at 7:28
ls -t
gives you a list of directory contents sorted by time modified.cp
to copy.rm
to remove files/folders. you can create a shell script if you'd like to do that and add it on yourcron
.