How to Copy or Move Files Based on Date Modified on Linux

In Linux, copying or moving files based on the date modified is a common task that can save time and effort. This is especially useful when dealing with a large number of files that need to be sorted according to a specific time frame. This can be accomplished using the find command along with other Linux commands such as cp and mv.

In this guide, we will go through the steps needed to copy or move files based on their date modified using the find command and cp or mv commands. We will be using the command line interface on a Linux system.

Step 1: Locate the Files to be Moved or Copied

The first step in copying or moving files based on their date modified is to locate the files that need to be moved or copied. We can do this using the find command. For example, to locate all files modified in the last 7 days, we can use the following command:

find /path/to/files -type f -mtime -7

This will list all files modified in the last 7 days in the specified directory.

See also  How to Clear The Dns Cache in Microsoft Windows

Step 2: Copy or Move the Files

Once we have located the files that need to be moved or copied, we can use the cp or mv command to perform the operation. For example, to copy all files modified in the last 7 days to a new directory, we can use the following command:

find /path/to/files -type f -mtime -7 -exec cp {} /path/to/new/directory \;

This will copy all files modified in the last 7 days to the specified directory.

Alternatively, to move all files modified in the last 7 days to a new directory, we can use the following command:

find /path/to/files -type f -mtime -7 -exec mv {} /path/to/new/directory \;

This will move all files modified in the last 7 days to the specified directory.

See also  How to Check Memory Usage on a Linux VPS Server

Alternative Option

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

Commands Mentioned:

  • find – search for files in a directory hierarchy based on specified criteria
  • cp – copy files and directories from one location to another
  • mv – move or rename files and directories
See also  How to See How Many Inodes on Linux Servers?

Conclusion

In this guide, we have shown how to copy or move files based on their date modified using the find command along with the cp or mv commands. This method can save time and effort when dealing with a large number of files that need to be sorted according to a specific time frame. By following the steps outlined in this guide, you can easily copy or move files based on their date modified on a Linux system.

If you have any comments or suggestions for improvement, feel free to leave them below.

Comments

1 Comment

  • Avatar for Sushil Sharma Sushil Sharma says:

    How i copy based on month and year to destination folder, i have 1 folder in which .tar files present in much more quantity(like:- 1000000 files), i want to copy the files like :- 2014 and 2015 year modified files, please help

Leave a Reply

Your email address will not be published. Required fields are marked *