Operating System - HP-UX
1752601 Members
4363 Online
108788 Solutions
New Discussion

Renaming lots of files-HP-UX 11.31

 
SOLVED
Go to solution
NDO
Super Advisor

Renaming lots of files-HP-UX 11.31

I have a folder on my hp-ux 11.31, with lots of files with long filename, and I just want the the filename to have the first 20 characters and add an extension.txt

The piece of code  I am trying is giving me an error:

 

for file in /path/to/directory/*; do
                echo mv –i “$file “ “${file:0:20}.txt”
done

 

But I am having the following error:

 

sh test.sh
test.sh[2]: "${file:0:20}.txt": The specified substitution is not valid for this command

 

 

Any hint on what might be wrong

6 REPLIES 6
Steven Schweda
Honored Contributor

Re: Renaming lots of files

> But I am having the following error:

   What is "${file}"?  Does the expression fail on the first name, or on
some particular name?

   Which shell?

NDO
Super Advisor

Re: Renaming lots of files

Hi

 

it fails on the very first name

Patrick Wallek
Honored Contributor
Solution

Re: Renaming lots of files

Here's what worked for me:

 

 # for file in *
> do
> file2=$(echo ${file} | cut -c 1-20)
> mv ${file} ${file2}.txt
> done
Steven Schweda
Honored Contributor

Re: Renaming lots of files

> [...] cut -c 1-20)

   But then I'd need to remember that "cut" exists.  As I always say,
"If you can't do it with 'sed', then it's not worth doing."

      $ echo 'abcdefghijklmnopqrstuvwxyz' | sed -e 's/\(.\{20\}\).*/\1/'
      abcdefghijklmnopqrst

(And it's so much clearer.)

Re: Renaming lots of files

Substring expansion is a capability of the bash shell - it is not available in the POSIX shell which is the default shell used on HP-UX. You could install bash and use that if you wanted this to work in the way you outlined. Otherwise you've been provided with some alternatives that work on HP-UX using sh and cut and/or sed (if you like explosions in punctuation factories)

bash and its runtime dependencies on HP-UX are available here: http://hpux.connect.org.uk/hppd/hpux/Shells/bash-5.0.016/


I am an HPE Employee
Accept or Kudo
NDO
Super Advisor

Re: Renaming lots of files

Thanks a lot your contribution was perfect!