1

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?

  • 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 your cron. – wens Sep 11 '13 at 5:28
  • I am looking for a single command to find and remove – user2552235 Sep 11 '13 at 5:55
  • It is something like this "find /BACKUP/ -type d -mtime +1 -exec mv -i {} /BACKUP/upload_testlink/ \;" – user2552235 Sep 11 '13 at 6:56
1

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

source: http://content.hccfl.edu/pollock/unix/findcmd.htm

| improve this answer | |
  • It is something like this "find /BACKUP/ -type d -mtime +1 -exec mv -i {} /BACKUP/upload_testlink/ \;" – user2552235 Sep 11 '13 at 6:55
-1

You can use piped commands to achieve this. Hope this helps.

rmdir `ls -lt | grep -v "\`date '+%h %d'\`" | awk '{print $NF}'` 
| improve this answer | |
  • This breaks if a directory has whitespace in its name. Also it's depending on the date variant and time formatting. – slhck Sep 11 '13 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy