Linux shell sed how to use a batch file to change the name Comments [Reserved]

 Demand Background:

A directory used to store packets generated apk file automated, we have now developed a modified package naming names: the name of the environment (pro | uat) -release- Date -v version .apk

The original idea was the expression matching is a new package name by positive, but found that the use of regular expressions in the shell script, I present to verify how the regular expression is in force in shel script, the reason is not clear yet.

By universal search engine, has found another way: more straightforward

Direct access apk name in the specified directory and rename it to a fixed name, so I used the convenient (to upload apk dandelions by python script)

 

Original Subject:

pro-release-20191220-v2.0.8.apk

[root@67 release]# ls
output.json  pro-release-20191220-v2.0.8.apk  test.sh

Script realize:

Apk file will be renamed to end with newNameStr :

for file in `ls | grep .apk`
do
 newfile='newNameStr'
 mv $file $newfile
done

Script effects:

[root@67 release]# sh test.sh
[root@67 release]# ls
newNameStr output.json test.sh

 

Content extension:

Foreword

This paper to introduce relevant content about Linux shell change the file name with sed volume, share learning for your reference, the following did not talk much to say, to take a look at the detailed introduction.

Examples

The removal of specific characters

Goal: 2017-01-01.jpg, 2018-01-01.jpg changed 20170101.jpg, 20180101.jpg

Methods: All - Replace empty

?
1
2
3
4
5
for file in ` ls | grep .jpg`
do
  newfile=` echo $ file | sed 's/-//g' `
  mv $ file $newfile
done

Sed herein using standard output string replacement, its general form:

?
1
stdout | sed 's/pattern/replace/'

In the above example, added to the end g for replacing all occurrences, rather than replacing the first match.

Intervening characters

Goal: book01.txt, paper02.txt changed book01.txt, paper02.txt

Method: acquiring position into the sides of the string to be matched packet respectively, and then reverse achieved by replacing the reference

?
1
2
3
4
5
for file in ` ls | grep .txt`
do
  newfile=` echo $ file | sed 's/\([a-z]\+\)\([0-9]\+\)/\1-\2/' `
  mv $ file $newfile
done

analysis

Ls obtained by the above-described first exemplary grep command and a list of files to be renamed and replaced with sed command string, and finally use the mv command to complete the change of the file name.

There are many ways to obtain the list of files to be renamed, by the find command, string can also be provided directly, we will refer hereinafter.

Note that for loop back  ls | grep .txt, this single two counter command in quotes, the  $(ls | grep .txt) same effect will be surrounded by a string as a command, and then returns the result string.

The file name contains a space solution

We can directly write to the file list for the cycle, rather than give the command, for example:

?
1
2
3
4
for file in "file1 file2 file3"
do
  ...
done

See for loop is to split the string by spaces, to be changed if the file name contains a space, it will be split into a plurality of file names, so that error.

To solve this problem, we can IFS (internal field separator) is set to newline \ n, this way, for loop will rows to get the value of a variable to ensure that every acquisition is indeed a full file name .

IFS command set of variables need to be placed before the for loop:

?
1
2
3
4
5
IFS=$ '\n'
for file in ` ls `
do
  ...
done

Can also be used directly while read command once read a line in the variable file:

?
1
2
3
4
ls | grep "*.txt" | while read file
do
  ...
done

Use find to obtain the file list

The previous example, we are to get a list of files by the ls command. This command can only get files in a directory, and can not be screened a variety of conditions.

And when it comes to look for a file, you have to mention the powerful find command. This command can be found in multiple levels of files in a directory, and can set various conditions for time, file size, owner, such as creating, finding particular files from convenient and flexible.

With the find command to get a list of files, then sed command with regular expressions to modify the file name, the combination of these two commands can do almost all common batch rename task.

For example, all greater than 1M, and suffix txt or jpg file, such as the shape book_20170101.txt, image_20170101.jpg file renamed 20170101-book.txt, 20170101-image.jpg, code is as follows:

?
1
2
3
4
5
for file in ` find . -size +1M -name "*_*.txt" -o -name "*_*.jpg" `
do
  newfile=` echo $ file | sed 's/\([a-z]\+\)_\([0-9]\+\)./\2-\1./' `
  mv $ file $newfile
done

More about the find command usage, refer to  //www.jb51.net/article/108198.htm

 

Reference documents:

How to use sed Linux shell batch file name change Detailed

Guess you like

Origin www.cnblogs.com/kaerxifa/p/12072140.html