bash string substitution

so in this case we have files named like this: 

track01.cdda.wav.mp3  track06.cdda.wav.mp3  track11.cdda.wav.mp3
track02.cdda.wav.mp3  track07.cdda.wav.mp3  track12.cdda.wav.mp3
track03.cdda.wav.mp3  track08.cdda.wav.mp3  track13.cdda.wav.mp3
track04.cdda.wav.mp3  track09.cdda.wav.mp3
track05.cdda.wav.mp3  track10.cdda.wav.mp3

but we want to rename them so they are like "05.mp3"

#the first "track" is actually a variable name but does not need $ because it is already referenced by the ${track
for track in `ls *.mp3`; do name=${track/track/}; name=${name/.cdda.wav/}; mv $track $name; done

The main part of the code above is the following:



${track is the name of the variable that has the string you want to substitute

/track/ means search for the string "track" in that variable and replace with nothing.

If we had wanted to change it to something else:



bash, substitutionso, cdda, wav, mp, rename, quot, variable, referenced, ls, mv, substitute, somethingelse,

Latest Articles

  • Linux Bash Script To List All Connected IPs and their network name
  • Cisco Switches How To Get Of Port Line Status Console Messages
  • Cisco DHCP Snooping Relay Setup Information
  • Cisco Switch Setup Guide Command List
  • Cisco 2960 Switch Reset To Factory Defaults
  • How To Boot Cisco CUCM UCSInstall 8.6, 10, 11 and 12 on KVM/Proxmox
  • VBOX VirtualBox How To Import Raw .img Disk File
  • Windows Server 2012, 2016, 2019 How To Install and Missing Disabled Telnet Client
  • proxmox vm networking breaks when you restart your network on the hostnode
  • Linux ln symlink how to update existing symbolic link
  • Ubuntu 18.04 / Linux Mint 19.1 Cannot Type or Login - solution
  • LUKS Hard Drive Encryption on Linux Mint Ubuntu Debian etc how to mount encrypted hard drive
  • How to use nmap locate other machines/computers/servers on your network using nmap
  • Linux Mint 18.2 Create Config File To Start Application Upon Login
  • Dell Wyse Thin Client BIOS Access Key
  • sudoers file in /etc warning about comments/includes!
  • Centos 7 Reallocate logical volume space to another
  • lvm how to reduce volume size
  • letsencrypt certbot error "Unable to find a virtual host listening on port 80 which is currently needed for Certbot to prove to the CA that you control your domain. Please add a virtual host for port 80."
  • SSH error cannot Forward or Listen "bind: Cannot assign requested address"