background

A project was being converted to TS over the weekend, so you needed to change the suffix of all files to TS. Some ides already support this feature, but on a whim, you decided to change it with a shell script

File search

Use the find command to output the file to the specified file. Because there may be a large number of file directories, I define an array and use the input loop to output it to a temporary file. In this case, > is the exported file stream and overwrites the specified file. ${pathList[*]} = ${pathList[*]} = ${pathList[*]

# define file address
pathList=("./routes/" "./public/" "./views/")
for p in ${pathList[*]}
do
	find $p -type f -name '*.js' > file.txt
doneCopy the code

Regular replacement

Sed (source string/regular matching rule/replace string) sed (source string/regular matching rule/replace string) sed (source string/regular matching rule/replace string)

for f in `cat ./file.txt`
do
	path=${f/\.js/\.ts}
	mv $f $path
doneCopy the code

Deleting temporary Files

rm -rf ./file.txtCopy the code

conclusion

The code is very simple, but it makes me familiar with shell again. In the process of using NodeJS, if you combine shell with file processing, it will be much better than native NodeJS

#! /bin/bash
# define file address
pathList=("./routes/" "./public/" "./views/")
# find file
for p in ${pathList[*]}
do
	find $p -type f -name '*.js' > file.txt
done

for f in `cat ./file.txt`
do
	path=${f/\.js/\.ts}
	mv $f $path
done
rm -rf ./file.txtCopy the code