dragons
dragons

Reputation: 619

rename first part of the filename in linux?

I have lot of files starting with processConfig-. I wanted to rename it to processCfg-. What's the easy way to change the first part of file name to processCfg- in linux?

But I don't want to rename this file processConfig.json since it doesn't match with my prefix.

> ls -lrth
total 467
-rw-r--r--  1 david  staff   9.8K May 26 15:14 processConfig-data-1234.json
-rw-r--r--  1 david  staff    11K May 26 15:14 processConfig-data-8762.json
-rw-r--r--  1 david  staff   4.9K May 26 15:14 processConfig-dataHold-1.json
-rw-r--r--  1 david  staff   6.6K May 26 15:14 processConfig-letter.json
-rw-r--r--  1 david  staff   5.6K May 26 16:44 processConfig-data-90987.json
-rw-r--r--  1 david  staff   284K May 28 18:44 processConfig.json

Upvotes: 0

Views: 148

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 184965

Like this :

rename -n 's/^processConfig-/processCfg-/' processConfig-*.json

Remove -n switch when the output looks good to rename for real.

man rename

warning There are other tools with the same name which may or may not be able to do this, so be careful.

The rename command that is part of the util-linux package, won't.

If you run the following command (GNU)

$ file "$(readlink -f "$(type -p rename)")"

and you have a result that contains Perl script, ASCII text executable and not containing ELF, then this seems to be the right tool =)

If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :

$ sudo apt install rename
$ sudo update-alternatives --set rename /usr/bin/file-rename

If you don't have this command with another distro, search your package manager to install it or do it manually (no deps...)


This tool was originally written by Larry Wall, the Perl's dad.

Upvotes: 1

Related Questions