Shell batch rename objects by date

Source: Internet
Author: User

In the case of a lot of service logs or a lot of scattered files that need to be sorted together in batches, the shell script can be quickly used to solve the problem:

 
Date = $ (date + % Y _ % m _ % d-% H % m % s); find *-type F | while read file; do MV $ File $ {file }. $ {date}; done

The preceding statement can quickly find all files in the directory and rename them.

How can I find the file with the specified suffix and rename it? For better understandingCodeSeparate execution:

 

 
Find./log-name"*. Log"|WhileRead FileDoDate= $ (Date + % Y % m % d _ % H % m % S _ %N) mV $ file./Temp_log/$ {Date}. logdone

 

The first line of the statement: Find all files under the current log directory with the suffix *. log.

Statement of the third line: import the current time variable into the date variable (generate a random number ).

Row 4 Statement: Move and rename the searched file.

Next we will explain the date variable:

% Y year

% M month

% D Date

% H hour

% M minutes

% S seconds

% S get the timestamp, current to: seconds between 00:00:00

% N: obtain the data in the current time, accurate to one second in

Therefore, we can see that the output of the date = $ (date + % Y % m % d _ % H % m % S _ % N) variable is basically a random number, it is difficult for % N to show the same result during CPU execution. In this way, the file will not be overwritten during renaming.

There is also a system variable $ random for the generation of random numbers:

For example, input echo $ random on the terminal.

Return Value: 26885

We can also perform MD5 encryption for it: Echo $ random | md5sum

Returned value: 5ebc600b4d19e86719a491d7dd365880

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: info-contact@alibabacloud.com 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.