Operation and Maintenance Enterprise Interview Question 3 Renamed the "ten random letters _test.html" file in the above question

Practical programming written test questions for Linux operation and maintenance (19 questions)

Corporate Interview Questions 3

#Change the file name created in question 2 uopiyhgawe_test.html
# test-->modify to omg,html-->HTML

method one:

Idea: Read the file name in a loop, store it in the changename.tmp file, use sed to change, delete the source file; read changename.tmp, create a new file;

This method complicates simple problems, and is not suitable for batch changing the file names of existing file contents;

#!/bin/bash
#
#
# Name the file created in question 2 uopiyhgawe_test.html
# test-->修改为omg,html-->HTML
touch /tmp/sh/changename.tmp
for i in `ls /tmp/www`;do
    echo $i >> /tmp/sh/changename.tmp;
    rm -f /tmp/www/$i
done

sed -i 's@_test.html@_omg.HTML@g' /tmp/sh/changename.tmp

for i in `cat /tmp/sh/changename.tmp`;do
    cd /tmp/www
    touch $i
done

rm -f / tmp / sh /changename.tmp


Method 2: 

Idea: Read the file name in a loop and store it in the file changename.tmp , grep -o select the first ten random letter strings, and use mv to rename

 

-98-[root@vm]10:50 /tmp/sh # cat changenamev2.sh 
#!/bin/bash
#
#
#change name with mv
#version 0.2

for i in `ls /tmp/www`;do
    bname=`echo $i | grep -o '[[:alpha:]]\{10\}'`
    mv /tmp/www/${bname}_test.html /tmp/www/${bname}_OMG.HTML
done

operation result:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325288291&siteId=291194637