Q. How to copy or Move the files and folder based on date modified on linux ?
A. Follow this steps :
1. Check the files modified date :
# ls -lrt
Output :
drwxr-xr-x 2 507 root 4096 Mar 3 21:00 ioncube -rw-r--r-- 1 root root 5641209 Mar 3 21:04 ioncube_loaders_lin_x86-64.tar.gz -rw-r--r-- 1 root root 635678 Mar 8 11:28 facilemanager-complete-1.3.1.tar.gz -rw-r--r-- 1 root root 0 May 12 00:08 file1 -rw-r--r-- 1 root root 0 May 12 00:08 file2 -rw-r--r-- 1 root root 0 May 12 00:08 file3 -rw-r--r-- 1 root root 0 May 12 00:08 file4 -rw-r--r-- 1 root root 0 May 12 00:08 file5
2. Create May directory under tmp, where the destination of the listed files :
# mkdir -p /tmp/May
3. Run the following command to display the files for date “May 12” and Move it to /tmp/May folder :
# for i in `ls -lrt | grep "May 12" | awk '{print $9}' `; do mv $i* /tmp/May; done
or
4. Run the following command to display the files for date “May 12” and copy it to /tmp/May folder :
# for i in `ls -lrt | grep "May 12" | awk '{print $9}' `; do cp -p $i* /tmp/May; done