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
  • What other files do you have? Jul 20, 2015 at 16:21
  • Craig other files would be any like abc-date.txt zcd-date.txt and so on. Jul 20, 2015 at 16:24
  • you are taking input from ls command but ls command includes other files also Jul 20, 2015 at 16:25
  • See my updated answer. Jul 20, 2015 at 16:30

4 Answers 4

10

Bash is good for this.

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

7
  • 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, 2015 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, 2015 at 18:05
  • Missing one double quote after $FILE
    – sa289
    Jul 20, 2015 at 18:37
  • 1
    It's OK to edit someone else's answer to fix things like this.
    – Jenny D
    Jul 20, 2015 at 18:38
  • 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, 2015 at 21:37
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.

4
  • now check again my question guys Jul 20, 2015 at 16:21
  • sa289 your solution will change also my current file eventDataLog.txt but I want to ignore it. Jul 20, 2015 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, 2015 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, 2015 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.

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
6
  • 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, 2015 at 16:16
  • but Craig there are a lots of file in that directory so it would not be work Jul 20, 2015 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. Jul 20, 2015 at 16:18
  • There's no need to use ls - just use a fileglob to expand the filenames directly.
    – Jenny D
    Jul 20, 2015 at 18:06
  • 2
    This is like going from Boston to San Francisco via Beirut. Jul 20, 2015 at 18:48

Not the answer you're looking for? Browse other questions tagged .