Pular para o conteúdo principal

Postagens

Mostrando postagens de maio, 2015

Append suffix in linux file names with Shell Parameter Expansion

Well, everytime I read about shell (bash) parameter expansion I see examples ripping off the suffixes. But someday, I needed to do the inverse: Append suffixes. How to do that? I've made a simple bash script to do so: #!/bin/bash for i in *; do  mv ${i} ${i}.sh; done exit 0 Using a for loop, I moved every file in the directory to the same name, with a ".sh" appended as suffix. Feel free to change the suffix you want. To revert: #!/bin/bash for i in *; do mv ${i} ${i/.sh/}; done exit 0 In this case, in every file of the directory, ".sh" will be substituted for nothing. It may cause some troubles. Let's suppose you have a dir, full of these files: 1.shellvpower 2.shellvpower 3.shellvpower 4.shellvpower They'll become: 1ellvpower 2ellvpower 3ellvpower 4ellvpower So, in a better way, to remove only the suffixes ".sh": for i in *; do  mv ${i} ${i%%\.sh}; done exit 0 Bonus: Append prefixes: To append,...