0

I have log files like this:

tmp_1_2_3_4_5.LOG
...

I need to change name like this:

1_2_3_4_5.LOG

I try:

rename 's/^tmp+_//' *

It's working on Debian, but not working on Red Hat. How can I do this with mv command?

onur
  • 5,647
  • 3
  • 23
  • 46
  • just try: `rename 's/^tmp_//' *.LOG` – anubhava Nov 11 '14 at 07:48
  • anubhava, not working. – onur Nov 11 '14 at 07:54
  • Does rename command different on Red Hat? It's working on Debian, but does not on Red Hat. If I can't do this with rename, how can I do this with other command? Sed or mv etc... I need one line command for this. – onur Nov 11 '14 at 08:05

2 Answers2

2

You can do this with a fairly simple for-loop:

for file in tmp_*; do
    [[ -e $file ]] || continue
    mv "$file" "./${file#tmp_}"
done

Also see BashFAQ #30

geirha
  • 5,801
  • 1
  • 30
  • 35
1

You can try this, with mv :

for i in *; do s=$(sed -r 's/^(tmp_)(.*.LOG)/\2/' <<< $i); if [[ "$i" != "$s" ]]; then mv "$i" "$s"; fi; done;
Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27