Use a shell script or PHP to move a file under a folder to a directory of its own date

Source: Internet
Author: User

The background is this: the site at the beginning of a smaller number of visits, people will all the picture files uploaded to a directory (such as/data/images/). Later visits were large, and there were many pictures, which affected the efficiency of reading. So there is a need to move these image files to a number of directories, this directory is the upload date of the picture (that is, the file attributes of the modified date filemtime) composed. such as the 2012-12-20 revision date, it should now be placed in the/DATA/IMAGES/2012/12/20 directory.

PHP has a very easy to manipulate the file functions, can be easily completed. Of course, you can also use shell scripts to complete.

Using the shell idea: Traverse this folder/data/images, find out the date of each file modification, then set up the folder for this date, and then move the file to the date folder below.

To query the modified date of the command, there is a stat, as follows: can find modify date


Like I'm going to find this hehe.log. Modified: 2016-03-04 (Get the second column on line sixth)

# stat Hehe.log | awk ' Nr==6{print '

Next, I will make this date into this format, 2016/03/04-f is a delimiter, can also be written in the command to write fs= "-", but must have a begin

#  Stat hehe.log | awk ' Nr==6{print $ ' | awk-f-' {print $ '/' $ '/' $ '
# stat Hehe.log | awk ' Nr==6{print ' | awk ' begin{fs= '-"}{print $"/"$ $"/"$ $"

Given this date, the shell script will naturally come out:

1#/bin/Bash2 3  for file inch./* 4  Do5 if [[-F $file]]6 Then str=$ (stat $file | awk ' Nr==6{print-$} ' | awk-f-' {print $ '/' $ '/' $ ')7 if [[!-D $str]]8 Then mkdir-p $str9 fiTen MV $file $str One fi A  Done

By the way, the shell script note several points: [[]] This judgment command, left and right to leave a space. If write down then. $ () is a variable assigned to the execution result. ${} is the value that gets the variable. For example, the 7th line of $str can be written ${str}.

When you do the test, you want to restore the files in the new directory. Can do: Find recursively find the file under the folder, and then move to the original directory,-I is a line of execution

Find Xargs -i  mv {}.


Use a shell script or PHP to move files under a folder to their own date in a directory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.