Recursive files renaming adding a suffix

To rename all files in a directory recursively adding a suffix, for example in a way that the new filename suffix will be the number of lines of the original file (e.g. if the fileĀ ‘my_file’ has 10 lines then it will be renamed to ‘my_file_10’):

for f in *; do mv "$f" "$f"_$(wc -l < "$f"); done

To keep extensions (if present):

for f in *; do 
    [[ $f =~ \. ]] && ext="."${f#*.}; 
    mv "$f" "${f%%.*}"_$(wc -l < "$f")$ext; 

Leave a comment

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