[1. What you want to do](#What you want to do) [2. Code introduction](# Code introduction) [3. Execution example](# Execution example) [4. Bonus](# Bonus)
Get the data (name) in the folder. In this article, I will introduce a shell script of a loop statement that changes the extension of files in a folder from .txt to .log.
** Current folder status **
Status before execution
$ ls
1.txt 2.txt 3.txt 4.txt 5.txt main.sh
main.sh
#!/bin/sh
for filename in *.txt
do
mv ${filename} ${filename%.txt}.log
done
main.run sh
sh main.sh
Post-execution status
$ ls
1.log 2.log 3.log 4.log 5.log main.sh
You can see that the .txt extension has been changed to .log.
You can also use it if you want to number hundreds of thousands of data. If you add a counter in the middle of the for minute, you can number a large amount of data at once.
main.sh
#!/bin/sh
cnt=1
for filename in *.txt;
do
mv ${filename} data_${cnt}.log;
#Increment of cnt variable
cnt=`expr ${cnt} + 1`
done
Post-execution status
$ ls
data_1.log data_2.log data_3.log data_4.log data_5.log main.sh
Recommended Posts