Linux Administrator

Copy Files Based on Date Modified in Linux

copy-files
mm
Written by Santosh Prasad

Question : I have lots of file in my one directory and want to copy the files based on date modified. How can I copy the files based on modified date and time.

Answer : Follow the below command to copy the files based on date modified in Linux.

Copy  Files Based on Date Modified in Linux

1. First check the files modified date using below command.

# ls -lrt
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. Now create a directory such as /opt/somedir which would be destination of the listed files.

# mkdir /opt/somedir

3. Run the below command to display the files for date “may 12” and copy to /opt/somedir directory.

# for i in `ls -lrt | grep "May 12" | awk '{print $9}' `; do mv $i* /opt/somedir; done

OR

4. Run the following command to display the files for date “May 12” and copy it to /opt/somedir folder.

# for i in `ls -lrt | grep "May 12" | awk '{print $9}' `; do cp -p $i* /opt/somedir; done
Thank you! for visiting Look Linux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at Look Linux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment