12

I had a group of files that I'd like to consistently rename, the files are named things like

"System-Log-01-01-2009-NODATA.txt"
"Something-Log-01-01-2009-NODATA.txt"

And I wanted them as lowercase, yyyymmdd, .log extension

"system.20090101.log"
"something.20090101.log"
Osama ALASSIRY
  • 794
  • 3
  • 7
  • 22

7 Answers7

15

I used to write perl scripts to do this, until I discovered the rename command.

It accepts a perl regex to do the rename:

for this, I just typed two commands:

rename 's/(\w+)-(\w+)-(\d\d)-(\d{4})-NODATA.txt\$1.$4$3$2.log$//' *
rename 'y/A-Z/a-z/' *.log

For some distros though, rename doesn't have this functionality (see its man page), and you may have to install perl-rename or prename.

gombosg
  • 103
  • 2
Osama ALASSIRY
  • 794
  • 3
  • 7
  • 22
7

mmv is a standard linux utility to move/rename multiple files. It is available from the repos for most distributions. For your example above, you could do:

mmv '*-Log-*-*-*-NODATA.txt' '#l1.#4#3#2.log'

For more information, read this debaday article or the man page.

Hamish Downer
  • 9,142
  • 6
  • 36
  • 49
6

Since i don't have a rename command, i am relying on this:

for myfile in /my/folder/*; do
    target=$(echo $myfile|sed -e 's/foo/bar/g')
    mv "$myfile" "$target"
done
Franz Bettag
  • 897
  • 8
  • 7
5

rename util is not very "standard". Each distro ships with a different rename tool. For instance, here on Gentoo, rename is from sys-apps/util-linux package and does not support regex.

Hamish Downer suggested mmv, it seems useful, specially for use inside scripts.

On the other hand, for the general case, you might want renameutils. It has qmv and qcp commands, which will open a text editor of your choice (my preference: Vim) and allow you to edit the destination filenames there. After saving and closing the editor, qmv/qcp will do all the renaming.

Both mmv and qmv are smart enough to rename files in correct order and also to detect circular renames, and will automatically make a temporary file if needed.

  • On Gentoo you can also emerge the `sys-apps/rename` package, which gives you `renamexm` which will do regex renaming as well as mass upper/lowercase and other nice things. – radicand Jun 12 '13 at 04:21
1

To be fair:

rename 's/(\w+)-(\w+)-(\d\d)-(\d{4})-NODATA.txt\$1.$4$3$2.log$//' *.txt

gives this output:

Use of uninitialized value $4 in regexp compilation at (eval 1) line 1.
Use of uninitialized value $3 in regexp compilation at (eval 1) line 1.
Use of uninitialized value $2 in regexp compilation at (eval 1) line 1.

But:

rename -n 's/(\w+)-\w+-(\d{2})-(\d{2})-(\d{4})-NODATA\.txt$/$1.$4$3$2\.log/' *.txt && rename 'y/A-Z/a-z/' System.20090101.log

gives the right output:

System-Log-01-01-2009-NODATA.txt renamed as System.20090101.log
System.20090101.log renamed as system.20090101.log

replacing {-n} switch with {-v}

fpba
  • 11
  • 2
1

I created a small bash script to do this:

#!/bin/bash

for f in `ls /path/to/folder`; do
  # Create new filename from the old one
  # This example replaces A (upper case) with a (lower case)
  new_file=`echo "$new_file" | tr A a`

  # Rename the file
  mv "$f" "$new_file"
done
bjarkig82
  • 111
  • 1
  • In general parsing `ls` output is not the best idea. A random search pulled up this longish read on [Unix . SE](http://unix.stackexchange.com/questions/128985/why-not-parse-ls) for your amusement. | In addition your answer would better match the question with a `tr '[:upper:]' '[:lower:]'` – HBruijn Jul 28 '15 at 19:07
0

I have stumbled upon a python command-line tool called regex-rename, to install it, just run:

pip3 install --user regex-rename

To test your regex:

regex-rename '(\w+)-(\w+)-(\d+)-(\d+)-(\d+)-NODATA(\.txt)'

The actual renaming would be:

regex-rename '(\w+)-(\w+)-(\d+)-(\d+)-(\d+)-NODATA(\.txt)' '\1.\5\4\3\6' --rename
SergioAraujo
  • 109
  • 3