0

So there's a folder in my directory which contains around 400-500 files (java, cpp, xml & etc) each with one name common for e.g

xml_1_ubuntu.xml
java_1_ubuntu.java
cpp_1_ubuntu.cpp
...

I wanna change the ubuntu in their name (irrespective of their extension/file type) to notubuntu so the work dir will have files like

xml_1_notubuntu.xml
java_1_notubuntu.java
cpp_1_notubuntu.cpp
...

is there anyway I can do this?

5 Answers5

4

Yes with the rename command using a normal regex:

rename -n 's/_ubuntu/_notubuntu/' *

-n makes it a dry-run. Remove it if this does what you want.

ubfan1
  • 17,838
Rinzwind
  • 299,756
  • 2
    You might want to add the underscore in front of ubuntu, to avoid a possible rerun and notnotubuntu results. – ubfan1 Oct 28 '20 at 20:03
2

I would use rename.

rename -n 's@_(ubuntu\.)@_not$1@' *_ubuntu.*

You can do this in plain bash as well:

for f in *_ubuntu.* ; do
  base="${f%%_ubuntu.*}"
  ext="${f##*_ubuntu.}"
  mv "$f" "${base}_notubuntu.${ext}"
done
xiota
  • 4,849
1

With mmv (from package mmv in the Universe repository)

mmv -n -- '*_ubuntu*' '#1_notubuntu#2'

If you have access to the zsh shell you can use its zmv with a very similar syntax

autoload zmv
zmv -n -- '(*)_ubuntu(*)' '$1_notubuntu$2'

or more succinctly

zmv -n -- '(*)_(ubuntu*)' '$1_not$2'

In all cases, remove the -n once you are happy with the transformations.

steeldriver
  • 136,215
  • 21
  • 243
  • 336
0

you can install GPRename, https://www.ghacks.net/2010/08/20/batch-rename-in-linux-with-gprename/ is batch renamer for both files and directories that is released under the GPL v3. It's easy to install, and even easier to use.

open terminal with CRTL+ALT+T:

sudo apt install gprename

enter image description here

pat
  • 429
-1

Without the rename command:

ls -1 | awk '{ name=$0; gsub(/ubuntu/,"notubuntu",name); print "mv " $0 " " name }' | bash

Without | bash at the end it will only display the commands without actually performing them (dry-run).

raj
  • 10,353