renaming files in a directory

2

I have files in a directory. The file names are constructed using a date timestamp notation, the file names are of the form:

name_YYMM.csv

I want to rename all such files using the following naming convention:

name_YYYYMM.csv

I am not sure if I can use a command line utility like grep to do this, or if I need to write a bash script to do this.

Any help on how to solve this prob will be appreciated.

I am running on Ubuntu

Takashi

Posted 2010-12-10T10:58:29.233

Reputation: 135

1There are batch file renaming utilities available through the Ubuntu software centre, have you tried any of these? – Tog – 2010-12-10T11:26:56.730

I don't know how to do it without a bash script – RobotHumans – 2010-12-10T11:30:18.503

Answers

2

A very simple way to do it

ls name* | while read a; do mv $a `echo $a | sed s/name_/name_20/`; done

or

ls name* | while read a; do mv $a $(echo $a | sed s/name_/name_20/); done

(since use of back-quotes is depreciated (or hard to read anyway))

it lists all files matching name_ then for each file it finds it replaces name_ with name_20. You can change 20 to 19 if you are working with files from the last millennium.

Nifle

Posted 2010-12-10T10:58:29.233

Reputation: 31 337

'name' is a place holder. On my disk the files are named <name>_YYMM.csv, where <name> is an alphanumeric string. – Takashi – 2010-12-10T13:24:58.993

@Takashi - If <name> does not contain any underscores () it's still simple. Just substitute `namefor_in the above example and replacels name*withls *.csv` – Nifle – 2010-12-10T13:35:47.583

It worked! thanks. Now, ... to try to understand what kind of magic we are dealing with here ;) – Takashi – 2010-12-10T14:17:38.343

1It would be better to use a for loop without ls. Variables that contain filenames (and, similarly, command substitution that processes filenames) should always be quoted. – Paused until further notice. – 2010-12-10T16:48:10.897

1

It is likely that you have a program or Perl script called rename on your system.

For the Perl script version:

rename 's/_/_20/' *_*.csv

For the util-linux-ng version:

rename _ _20 *_*.csv

Or, using Bash:

for f in name*; do mv "$f" "${f/_/_20}"; done

Paused until further notice.

Posted 2010-12-10T10:58:29.233

Reputation: 86 075

0

In zsh:

autoload zmv
zmv '(*_[0-9][0-9])([0-9][0-9].csv)' '${1}20${2}'

You can make the pattern less precise, e.g. '(*_??)(??.csv)' or even '(*)(??).csv' if there's no risk of other files matching.

If you don't want to use zsh, see Dennis Williamson's answer.

Gilles 'SO- stop being evil'

Posted 2010-12-10T10:58:29.233

Reputation: 58 319