Batch change file extension in Linux

In the daily development process, we sometimes need to do some batch operations, as described in the previous article.

Today, we will share how to use Linux commands to change file extensions in batches.

First, we create files with txt extensions in batch under the current directory.

➜ awk 'BEGIN {do {++i; system("touch file_num_" i ".txt") } while (i<9) }'

Next, we will introduce two change methods:

Method 1: use the for loop

Use the for loop to change the txt extension of all files in the current directory to json.

➜ for i in *.txt;do mv "$i" "${i%.txt}.json" ;done

Method 2: use the awk command

Use the find command and awk command to batch change the json file extension in the current directory to txt.

➜ find . -type f -name "*.json" | awk -F"[./]" '{system("mv "$0" " $3".txt")}'

Is there any better way? Comments are welcome.

Add a Comment

Your email address will not be published. Required fields are marked *