How to rename multiple files in a directory at the same time

rename -n 's/(\w+)/XXXXX/' *.csv

remove the -n when happy.


Try:

for f in *.csv; do mv -i -- "$f" "XXXXX-${f#*-}"; done

How it works:

  • for f in *.csv; do

    This starts a loop over all *.csv files.

  • mv -i -- "$f" "XXXXX-${f#*-}"

    This renames the files as you want, asking interactively before overwriting any file.

  • done

    This marks the end of the loop.

Example:

$ ls -1
11234-cam-yy3r5-ro9490-85adu9.csv
12345-ram-3e3r5-io9490-89adu9.csv
14423-sam-hh3r5-uo9490-869du9.csv
45434-dam-qwe35-to9490-43adu9.csv
$ for f in *.csv; do mv -i -- "$f" "XXXXX-${f#*-}"; done
$ ls -1
XXXXX-cam-yy3r5-ro9490-85adu9.csv
XXXXX-dam-qwe35-to9490-43adu9.csv
XXXXX-ram-3e3r5-io9490-89adu9.csv
XXXXX-sam-hh3r5-uo9490-869du9.csv

I liked the little challenge that you've posted, so here is my solution. I'm assuming that all your files starts with 5 numeric characters, so using the cut command to replace the initial numeric files by "XXXXX".

Below, the files before the command.

-rw-rw-r--. 1 daniel daniel 0 May 13 23:18 11111_bar_file.csv
-rw-rw-r--. 1 daniel daniel 0 May 13 22:54 12345_baz_file.csv
-rw-rw-r--. 1 daniel daniel 0 May 13 22:54 67890_foo_file.xml

Below, the one liner command.

for src in *.csv; do dst=XXXXX$(echo $src| cut -c6-); mv $src $dst; done;

Below, the files after the command.

-rw-rw-r--. 1 daniel daniel 0 May 13 22:54 67890_foo_file.xml
-rw-rw-r--. 1 daniel daniel 0 May 13 22:54 XXXXX_bar_file.csv
-rw-rw-r--. 1 daniel daniel 0 May 13 23:18 XXXXX_baz_file.csv

Is that what you're looking for? :)

References:

Looping through command output in bash

Substrings in bash