34

As a follow-up to my previous question, if I have multiple files of the form

sw.ras.001
sw.ras.002
sw.ras.003
…

What command can I use to remove the ras. in the middle of all the files?

Paul
  • 9,423

6 Answers6

49

You can do this with a fairly small modification of either answer from the last question:

rename s/ras\.// sw.ras.*

or

for file in sw.ras.*; do
    mv "$file" "${file/ras./}"
done

Explanation:

rename is a perl script that takes a perl regular expression and a list of files, applies the regex to each file's name in turn, and renames each file to the result of applying the regex. In our case, ras is matched literally and \. matches a literal . (as . alone indicates any character other than a newline), and it replaces that with nothing.

The for loop takes all files that start with sw.ras. (standard shell glob) and loops over them. ${var/search/replace} searches $var for search and replaces the first occurrence with replace, so ${file/ras./} returns $file with the first ras. removed. The command thus renames the file to the same name minus ras.. Note that with this search and replace, . is taken literally, not as a special character.

slm
  • 369,824
Kevin
  • 40,767
  • Could you explain your answer to others learning from? What "s/ras.// sw.ras.*" do? I guess I know it but I want to be sure, and other can learn too. Second option same way. – H_7 Mar 03 '12 at 16:47
  • 1
    @H_7 Good idea, I've added an explanation. – Kevin Mar 03 '12 at 18:09
  • Is ${file/ras./} used a lot these days? If I have to delete a substring I use sed with substitute s// which seems more logical or rational. – Timo Jul 11 '21 at 07:07
  • Just read this and understand that sed for filename-change is even more cryptic and should not be used at all. – Timo Jul 11 '21 at 19:39
17

Another option is to use mmv (Mass MoVe and rename):

mmv '*ras.*' '#1#2'

Don't forget to use the single quotes around your patterns, otherwise the stars will be expanded at the shell level.

The utility is not always available but if it's not, you can install it with:

sudo apt-get install mmv

See the man page here.

alexg
  • 271
6

In any POSIX-compliant shell (bash, dash, ksh, etc):

for file in sw.ras.[[:digit:]][[:digit:]][[:digit:]]; do
    mv "${file}" "${file/ras\./}"
done

Or with rename:

rename 's/ras\.//' sw.ras.[[:digit:]][[:digit:]][[:digit:]]
Chris Down
  • 125,559
  • 25
  • 270
  • 266
3

I recently had to do a bulk rename of a number of files where I had to replace the last occurrence of a character where the character occurred multiple times in the filenames. In this particular case I had to replace the last dash - with an underscore _, turning this:

some-long-file-name.ext

into this:

some-long-file_name.ext

It took some time but this finally did it:

for FILE in *; do mv $FILE ${FILE%-*}_${FILE##*-}; done

Here:

  • ${i%-*} matches the begining of the filename up to the last occurrence of the dash -
  • ${file##*-} matches the rest of the filename after the last occurrence of the dash -
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
0

Using find, xargs and sed:

find -name 'sw.ras.*' -print0 | sed -ze "p;s/\.ras//" | xargs -0 -n2 mv

https://stackoverflow.com/questions/4793892/recursively-rename-files-using-find-and-sed#comment60553396_11709995

Ray Foss
  • 1,002
-1

How do you delete the ads in a filestring?

11/06/2020 06:45 PM 30,534,952 1. Installing Cisco Routers--- [ DevCourseWeb.com ] ---.mp4 11/06/2020 06:45 PM 26,577,869 2. Installing Cisco Switch--- [ DevCourseWeb.com ] ---.mp4 11/06/2020 06:45 PM 24,517,510 3. Installing Cisco ASA--- [ DevCourseWeb.com ] ---.mp4 11/06/2020 06:45 PM 21,726,183 4. Installing Cisco IOS XRv Router--- [ DevCourseWeb.com ] ---.mp4 11/06/2020 06:45 PM 21,918,964 5. Installing Cisco Nexus 7000--- [ DevCourseWeb.com ] ---.mp4 11/06/2020 06:45 PM 31,551,814 6. Installing Cisco Nexus 9000--- [ DevCourseWeb.com ] ---.mp4 11/06/2020 06:45 PM 24,567,825 7. Installing Cisco CSR 1000v Router--- [ DevCourseWeb.com ] ---.mp4 11/06/2020 06:45 PM 78,034,899 8. Installing Cisco ISE--- [ DevCourseWeb.com ] ---.mp4 10/08/2020 10:35 AM 172 Bonus Courses + Project Files.url