8

I have ~250 files with the name:

no_responseEvent_2002.02.07.03.15.56.970/       
no_responseEvent_2002.02.10.01.47.07.450/       
no_responseEvent_2002.02.13.14.18.46.00/

I want to remove 'no_response' from each file name.
How do I do that?
I know a for loop could work, but I am confused about how to implement it.

PersianGulf
  • 10,850
geeb.24
  • 207
  • Build a Directory listing in your for loop and use the following: mv $f $(echo $f | sed -e 's/no_responseEvent//')where$f` is your directory listing. I'd agree with Don here, but the files don't all have the same extension, therefore this is not a duplcate. – eyoung100 Oct 23 '14 at 20:14
  • If you're using Debian/Ubuntu you can use the Perl based rename command which has a powerful regular expression feature. – garethTheRed Oct 23 '14 at 20:18

1 Answers1

18

I assume all the 250 files are in the same directory and follow the same naming pattern. If that is the case, you could do,

for i in  "$remove"*;do mv "$i" "${i#"$remove"}";done

Testing

ls
no_responseEvent_2002.02.07.03.15.56.970   
no_responseEvent_2002.02.07.03.15.56.972
no_responseEvent_2002.02.07.03.15.56.971  
no_responseEvent_2002.02.07.03.15.56.973

Now, after I run the for loop, I get the output as,

remove=no_response
for i in  "$remove"*;do mv "$i" "${i#"$remove"}";done
ls
Event_2002.02.07.03.15.56.970  
Event_2002.02.07.03.15.56.971  
Event_2002.02.07.03.15.56.972  
Event_2002.02.07.03.15.56.973

References

http://www.commandlinefu.com/commands/view/2519/uniformly-correct-filenames-in-a-directory

mikeserv
  • 58,310
Ramesh
  • 39,297