2

This question already has an answer here:

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.

marked as duplicate by don_crissti, jasonwryan, chaos, Anthon, slm Oct 23 '14 at 21:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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
10

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

  • 2
    Or simply, "${i/no_response/}"... – jasonwryan Oct 23 '14 at 20:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.