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