Bash – In Bash how does one determining the length of filename

bashrename

I'm doing a little script to rename some files. Actually a lot of files.

The problem and the cool thing is, that I actually just need to change the files that have a filename length of 5 characters.

I did a $(filename:0:5) and look if there is some leftover, but I guess that's dirty and I would appreciate to know the real or scholastic way of doing this.

Something like that:

if [$(filename).length() == 5]; then
  reaname_file
fi

Best Answer

You can use ${#VAR} to get the length of a variable $VAR:

if [ "${#filename}" -eq 5 ]; then
   rename_file
fi
Related Question