DEV Community

Eray Ates
Eray Ates

Posted on • Edited on

Change filenames

If you want to change filenames with given a prefix and do it in all inner folders, its little bit problem so this problem can solve easily with find sed and awk programs but I want to show a way just some script.

Lets define a deep first search algorithm for folders and get filenames.

After that goes to deep folder, first searching files with regular expressions (Added asterix to code due to passing asterix cause passing all filenames to function) and executing that filename with our ${2} given function.

[[ -n ${2} ]] && (${2} ${file})
Enter fullscreen mode Exit fullscreen mode

Good, now can prepare change filename function and give it to this script. As you see ${file} will be our first argument ${1}.

function changeFileName() { mv $(pwd)/${1} $(pwd)/_${1}; }
export -f changeFileName
Enter fullscreen mode Exit fullscreen mode

Ok than,

./check.sh .scss changeFileName
Enter fullscreen mode Exit fullscreen mode

Now I added prefix underscore for all my related files in all folders.

Top comments (0)