Use shell batch to change file suffix names
There are times when you need to change the log files in the server to a different uniform format, and it's a good idea to use your scripts automatically. In this paper, the JPEG format file is changed to JPG format as an example.
Script to create 10 files, with JPEG as the suffix, the code is as follows:
#!/bin/sh for
((i=0;i<=10;i++)) doing touch
${i}.jpeg
Done
After executing the above script, you will see that 10 files are generated in the current directory, and the file name consists of Name.suffix
In order to change the files in the current directory in bulk, we need to traverse the current directory, get the name of the file that needs to be changed, and then splice with the new file suffix to form a new filename name.newsuffix.
The code is as follows:
#!/bin/sh
oldsuffix= "JPEG"
newsuffix= "jpg"
dir=$ (eval pwd) for
file in $ (ls $dir | grep. ${oldsuffix})
do
name=$ (ls ${file} | cut-d.-f1)
mv $file ${name}.${newsuffix}
did
echo "change jpeg to JPG successd! "
Oldsuffix is the file's old suffix, Newsuffix is the file's new suffix $ (cmd) is a command substitution that executes the cmd command when the statement is run, and then returns the execution result of the CMD command; the role of Eval is to perform command-line processing again (not two times for a command), That is, the arguments followed by the Eval are consolidated into the correct command line command execution. $ (eval pwd) The result returned after execution is the directory path where the script resides. In the loop statement, we use the Cut command to shear the matching file name. The cut command cuts bytes, characters, and fields from each line of the file and sends them to standard output. Parameter-D is used to customize the separator character, the default is tab, and the-D in the program. As a delimiter, parameter-f specifies which region to intercept, and-F1 represents the name of the filename that intercepts the first area.
Reference Articles link