Script to edit first letters of multiple files

This is a simple one, and there are just loads of ways to replace lines/characters/spaces with Perl one-liners and Bash.

The main point of this script is when I install a new distro and want to shutdown services that startup automatically on each run level, for Ubuntu in the directories rcS.d, rc0.d, rc1.d, rc2.d, rc3.d, rc4.d, rc5.d, rc6.d

Each item begins with an “S” for START or a “K” for KILL with a number that pertains to the order of priority (ie S25bluetooth). And depending on the run-level, and how many apps you have installed, this could be 10-20 services launched at boot.

#!/bin/bash
############################################
#KILL STARTUP SERVICES
############################################
#By richs-lxh – http://richs-lxh.com
############################################
#CHANGE “ONLY” THE “FIRST” CHARACTERS AT THE
#BEGINNING OF A FILE (EG FILES S20* to K20* )
############################################
for i in S* ;
do mv “$i” “${i//S/K}”;
done

So if you want to kill all the processes that start with S20, it would be:
for i in S20* ;
do mv “$i” “${i//S20/K20}”;
done

The reason I posted this little script is that when you Google search for scripts and one-liners to edit filenames via the CLI, practically every guide offers newbies an example of how to change the “suffixes” (ie replace .html with .htm) but no clue on how to batch-replace the beginning of filenames.

Advertisements

One thought on “Script to edit first letters of multiple files

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s