0

I have a bunch of files.

academic-cap.svg arrow-narrow-right.svg ban.svg chart-bar.svg adjustments.svg arrow-narrow-up.svg beaker.svg chart-pie.svg ... ... 

I'd like to change

  1. the first letter and after the dashes to uppercase
  2. remove dashes
  3. add Icon at the end
  4. change the file name to svelte.
AcademicCapIcon.svelte ArrowNarrowRightIcon.svelte BanIcon.svelte ChartBarIcon.svelte ... 

How can I do using Bash script or terminal.

1
  • What have you tried? Commented Feb 6, 2022 at 5:58

1 Answer 1

4

Using the Perl-base rename utility (sometimes also caled prename):

rename -n 's/./\U$&/;s/-(.)/\U$1/g;s/\.svg$/Icon.svelte/' -- *.svg 

The above command applies three Perl substitution operations on each name in the current directory matching the filename globbing pattern *.svg.

The first substitution, s/./\U$&/, replaces the very first character of the filename with the upper-case variant of itself.

The second substitution, s/-(.)/\U$1/g, replaces each occurrence of a character following a dash with the upper-case variant of that character while the dash is removed.

The last substitution, s/\.svg$/Icon.svelte/, replaces the .svg filename suffix with Icon.svelte.

The -n option to rename causes the filename transformations to be displayed but not carried out. You may later change -n to -v to verbosely rename the files.

Example given the names in the question:

$ ls academic-cap.svg ban.svg adjustments.svg beaker.svg arrow-narrow-right.svg chart-bar.svg arrow-narrow-up.svg chart-pie.svg 
$ rename -n 's/./\U$&/;s/-(.)/\U$1/g;s/\.svg$/Icon.svelte/' -- *.svg rename(academic-cap.svg, AcademicCapIcon.svelte) rename(adjustments.svg, AdjustmentsIcon.svelte) rename(arrow-narrow-right.svg, ArrowNarrowRightIcon.svelte) rename(arrow-narrow-up.svg, ArrowNarrowUpIcon.svelte) rename(ban.svg, BanIcon.svelte) rename(beaker.svg, BeakerIcon.svelte) rename(chart-bar.svg, ChartBarIcon.svelte) rename(chart-pie.svg, ChartPieIcon.svelte) 
1
  • Wow. Thanks. It works like a charm. Commented Feb 6, 2022 at 6:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.