-2

I have few files under a particular directory. These are the files

idex.1.ebwt  idex.2.ebwt  idex.3.ebwt  idex.4.ebwt  idex.rev.1.ebwt  idex.rev.2.ebwt

I want to change the names of the file from idex to index keeping the rest of the name same. Basically I want to replace idex with index

Any one line command?

Anthon
  • 79,293
user3138373
  • 2,559

2 Answers2

2

You could do this with a simple for-loop and some shell magic:

for file in idex.*; do
    mv "$file" "index${file#idex}"
done

Yes, you could also put this into one line. ;)

${var#pattern} evaluates to $var with a leading pattern removed (where pattern is a glob(7) pattern). There exist four variants of this (POSIX compliant):

  1. ${var#pattern} evaluates to $var with a leading pattern removed (non-greedy).
  2. ${var##pattern} evaluates to $var with a leading pattern removed (greedy).
  3. ${var%pattern} evaluates to $var with a trailing pattern removed (non-greedy).
  4. ${var%%pattern} evaluates to $var with a trailing pattern removed (greedy).

Difference to non-greedy and greedy is that if you have for example $var set to foo.bar.baz, ${var%.*} will evaluate to foo.bar, but ${var%%.*} will evaluate to foo. This is because ${var%.*} will search for the shortest pattern matching .* (thus from last . to end), where ${var%%.*} will search for the longest one (thus from first . to end).

You can help yourself memorizing which version are the non-greedy and the greedy ones simply with memorizing that the longer (# resp. % twice) search for a longer match.

Andreas Wiese
  • 10,400
  • Please try searching the site before adding more answers that provide the same general solutions that have already been captured before. Renaming files have been written up dozens of times already. – slm Apr 21 '14 at 17:21
1

If you're using a major linux distribution or have the standard util-linux package installed, this one-liner code should do what you want.

rename idex index idex*
  • 2
    Which rename command? rename varies by OS and some can even have different non-compatible alternatives for the rename command. – jordanm Apr 21 '14 at 17:16
  • Isn't rename part of the standard util-linux package which is common for most major distros? – Chirag Bhatia - chirag64 Apr 21 '14 at 17:19
  • Linux distros, indeed. – Andreas Wiese Apr 21 '14 at 17:20
  • @Chirag64 Please try searching the site before adding more answers that provide the same general solutions that have already been captured before. – slm Apr 21 '14 at 17:20
  • 1
    Yes, it is in util-linux but there is also a rename that comes with perl, which is also very common. Which one is /usr/bin/rename by default is up to the distro. In Debian, it is the perl version. – jordanm Apr 21 '14 at 19:53