I have hundreds of directories that I need to rename, as follows:

from 15_öblös to 15_adj_öblös
from 14_kies to 14_adj_kies
and so on...

So, I have to insert 'adj' between the number and the word.

What I have done, but it's not doing well

for dic in [0-9]*
m=`echo "$dic" | cut -d"_" -f1`
l=`echo "$dic" | cut -d"_" -f2`
mv "$dic" "${m}_adj_${l}"
  • What's going wrong? Your script seems OK... Aug 25, 2016 at 14:41
  • too complexe solution... try the solution I gived to you behind Aug 25, 2016 at 14:47

3 Answers 3


Fedora, Debian and derivatives (Ubuntu, Mint, …) have installed by default a command named rename or prename for Perl version that does this for you, it works like that :

prename 's/_/_adj_/' *
# OR
rename 's/_/_adj_/' *

You can do something more complex to find and rename your files with more control like:

prename 's/(\d+)_/$1_adj_/' *
# OR 
rename 's/(\d+)_/$1_adj_/' *

Note: for prename, the first argument is Perl code.

  • Linux doesn't have anything installed by default, it's just a kernel. Some distributions of software built for that kernel do have a rename command though. Actually, on Linux-based systems, there are two main variants of rename: the one coming from perl (or one of its derivatives) which is the one you're refering to or the one from util-linux. Aug 25, 2016 at 15:00

I could not see the problem in your script but not knowing all the directories you have, I don't know what you are facing. But something like this might help:

find ./ -maxdepth 1 -type d | while read dirname
  newdirname=$(echo ${dirname} | sed -e "1,1s/_/_adj_/")
  mv ${dirname} ${newdirname}

On non-embedded Linux systems, you can use the rename command, which is very simplistic but good enough for this case. To replace the first _ by _adj in all the files in the current directory whose name begins with a digit:

rename _ _adj_ [0-9]*

On Debian and derivatives (Ubuntu, Mint, …), this command is called rename.ul. These distribution's rename command is a different, more powerful one which is also called prename; its first argument is perl code. To replace _ by _adj_:

prename 's/_/_adj_/' [0-9]*

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .