Linux rename with regular expression

Hello,

I have the following filenames

Instruments_20171004.csv_11-03-2017_20:01:06.gz
Instruments_20171030.csv.gz
Orders_0024_20171103.csv_11-03-2017_20:01:05.gz

I like to change the filenames as:

Instruments_20171004.csv_11-03-2017_20-01-06.gz
Instruments_20171030.csv.gz
Orders_0024_20171103.csv_11-03-2017_20-01-05.gz

I'm trying to replace the ":" with "-"
I tried the following

rename -v 's/\:/-/g' *2017*.csv_*.gz

It did not throw any error but did not rename the filenames.
Linux <servername> 2.6.18-422.el5 #1 SMP x86_64 x86_64 x86_64 GNU/Linux

Appreciate your help in correcting my code.

You must be using the rename utility normally provided by the util-linux-ng. The command you are using might work for the Perl script with the same name.

Alternatively, this might be of value:

ls *2017*.csv_*.gz  | perl -lne '$o=$_; rename $o, $_ if s/:/-/g'
1 Like

Already provided it.

Try

for FN in *2017*.csv_*; do echo mv "$FN" "${FN//:/-}"; done
mv Instruments_20171004.csv_11-03-2017_20:01:06.gz Instruments_20171004.csv_11-03-2017_20-01-06.gz
mv Orders_0024_20171103.csv_11-03-2017_20:01:05.gz Orders_0024_20171103.csv_11-03-2017_20-01-05.gz

remove echo if happy with result.

2 Likes

To avoid a "files are identical" error better ensure that the source file has a : character

for FN in *2017*.csv_*:*; do ...

BTW the // modifier works in bash and zsh and ksh93.

1 Like

Hi, bobbygsk .

It's always useful to tell us your OS and shell. As Aia pointed out, there are differences in command rename depending on the distribution.

If you still need advice, perhaps one of these possibilities may be useful:

Rename multiple files, groups of files

        1) rename -- Debian version and RedHat version differ, q.v.
           (try package util-linux:
           http://en.wikipedia.org/wiki/Util-linux)

        2) ren -- RedHat relatives

        3) renameutils -- package contains qmv, imv, icp, qcp, and deurlname

        4) mved -- (circa 2006; good as of 2017.11), perl
           http://raf.org/mved/
           (An earlier shell version may be available.)

        5) rename -- perl builtin library routine (DIY)

        6) mmv -- move/rename/copy/append/link multiple files by wildcard patterns

        7) gprename - batch rename using a GUI (Gnome)

        8) krename - batch rename using a GUI (KDE)

Best wishes ... cheers, drl

1 Like

Thanks all for you time and your help.

I'm trying to get files from linux to Windows 7 using WinSCP application through batch script (MS-DOS batch script)

I have files with extension "*.gz" in linux.
I gunzip those files during sftp using winscp by exclamation - "! <linux commands>"
However, my logs shows that the filenames are read as

Orders_0024_20171103.csv_11-03-2017_20%3A01%3A05 
Instruments_20171004.csv_11-03-2017_20%3A01%3A06

instead of

Orders_0024_20171103.csv_11-03-2017_20:01:05 
Instruments_20171004.csv_11-03-2017_20:01:06

I'm looking something simpler just like Aia provided.
Aia's code works and I'm using his code.

If there is any simpler, I like to use that code.

Thanks all for your help and time.

---------- Post updated at 02:17 PM ---------- Previous update was at 02:12 PM ----------

Already provided earlier. Except for servername and timestamp, I provided everything