5

I have multiple files in my directory. for example:

eventDataLog.txt.2015-04-23-22
eventDataLog.txt.2015-04-23-23
eventDataLog.txt.2015-04-24-01
eventDataLog.txt.2015-04-24-02
eventDataLog.txt.2015-04-24-03
eventDataLog.txt.2015-04-24-04

and I want to change all of them to

abc-eventDataLog.txt.2015-04-23-22
abc-eventDataLog.txt.2015-04-23-23
abc-eventDataLog.txt.2015-04-24-01
abc-eventDataLog.txt.2015-04-24-02
abc-eventDataLog.txt.2015-04-24-03
abc-eventDataLog.txt.2015-04-24-04

How can I use mv command to perform this task ? I have use this

mv  eventDataLog.txt.2015-* ec2prod-eventDataLog.txt.2015-*
mv: target ‘ec2prod-eventDataLog.txt.2015-*’ is not a directory

but do not works

but remember I have many other files in that directory so answer would be appropraite and also I have to ignore this file eventDataLog.txt because it is current file and in use.

4 Answers4

10

Bash is good for this.

for FILE in eventDataLog.txt.2015*; do mv "$FILE" "abc-$FILE"; done

Jenny D
  • 13,172
  • 3
    You should add some quotes to that command: for FILE in eventDataLog.txt.2015*; do mv "$FILE" "abc-$FILE"; done or for FILE in eventDataLog.txt.2015*; do mv "$FILE" abc-"$FILE"; done. – kasperd Jul 20 '15 at 17:53
  • 2
    @kasperd True - while in this particular case there were no filenames with spaces, it's a good habit to get into. – Jenny D Jul 20 '15 at 18:05
  • Missing one double quote after $FILE – sa289 Jul 20 '15 at 18:37
  • 1
    It's OK to edit someone else's answer to fix things like this. – Jenny D Jul 20 '15 at 18:38
  • Theres many ways to skin a cat. I approve of the obvious-what-this-does approach. – Matthew Ife Jul 20 '15 at 18:46
  • 1
    I'm pretty sure you can't edit an answer unless you change at least 4 characters or some minimum. I've tried to correct typos before and it wouldn't let me. – sa289 Jul 20 '15 at 21:37
  • @sa289 If I recall correctly, the limit is 6 characters and applies to users with less than 2000 reputation. – kasperd Jul 21 '15 at 10:02
6

You can accomplish this using the rename comamnd. In your case you'd do rename eventDataLog.txt.2015 abc-eventDataLog.txt.2015 eventDataLog.txt*.

The syntax it uses is rename frompattern topattern listoffilessuchaswildcardmatch.

sa289
  • 774
  • now check again my question guys – Hassan Sohail Jul 20 '15 at 16:21
  • sa289 your solution will change also my current file eventDataLog.txt but I want to ignore it. – Hassan Sohail Jul 20 '15 at 16:22
  • Then simply do rename eventDataLog.txt.2015 abc-eventDataLog.txt.2015 eventDataLog.txt* so it's more specific on what it renames from and to. If it doesn't find a match it won't rename. – sa289 Jul 20 '15 at 16:37
  • 2
    That works with the rename command from Red Hat based distributions. Debian based distributions have a different rename command with a different syntax. – kasperd Jul 20 '15 at 17:57
2

I would use the mmv tool and do

mmv eventDataLog.txt.\* abc-eventDataLog.txt.\#1

which is specifically designed to do mass renames.

glglgl
  • 1,210
0

Command

for FILE in $(ls -1 | egrep '^eventDataLog' | egrep -v '^eventDataLog.txt$'); do mv $FILE abc-$FILE; done

Notes

  • egrep is the regex-capable version of grep and is included with grep in most distributions
  • First egrep will filter all files starting with "eventDataLog"
  • Second egrep will filter all files that do not (note -v noting an inverse match) match the full name of the file you wish to ignore

Proof

$ ls -1
eventDataLog.txt
eventDataLog.txt.2015-04-23-22
eventDataLog.txt.2015-04-23-23
eventDataLog.txt.2015-04-24-01
eventDataLog.txt.2015-04-24-02
eventDataLog.txt.2015-04-24-03
eventDataLog.txt.2015-04-24-04
myOtherFile
mySecondOtherFile

$ for FILE in $(ls -1 | egrep '^eventDataLog' | egrep -v '^eventDataLog.txt$'); do mv $FILE abc-$FILE; done

$ ls -1
abc-eventDataLog.txt.2015-04-23-22
abc-eventDataLog.txt.2015-04-23-23
abc-eventDataLog.txt.2015-04-24-01
abc-eventDataLog.txt.2015-04-24-02
abc-eventDataLog.txt.2015-04-24-03
abc-eventDataLog.txt.2015-04-24-04
eventDataLog.txt
myOtherFile
mySecondOtherFile
  • That solution works for this case, but I'd suggest the rename command is a better general solution as it can do renames even mid-filename. I've posted an answer with an example. – sa289 Jul 20 '15 at 16:16
  • but Craig there are a lots of file in that directory so it would not be work – Hassan Sohail Jul 20 '15 at 16:17
  • Have you tried it? That solution works for the data you have given. If you need a different solution, edit your question and post better requirements. – Craig Watson Jul 20 '15 at 16:18
  • There's no need to use ls - just use a fileglob to expand the filenames directly. – Jenny D Jul 20 '15 at 18:06
  • 2
    This is like going from Boston to San Francisco via Beirut. – Matthew Ife Jul 20 '15 at 18:48
  • normal grep can handle basic regular expressions - egrep can handle "extended" regular expressions. (Anyone used to perl regexp will find the "extensions" quite sparse - check out pcregrep.) – Dan Pritts Jul 21 '15 at 03:57