1

How can I remove the first letter from a directory name? for example: Folder is named as "AFolder_01" how can I rename it to "Folder_01"

The reason for my question is that I have list of folders and I want to rename all these folders at once by removing the first letter. I found this code online to remove the last character(s):

  while IFS= read -r dir; do [[ -d $dir ]] && mv -i "$dir" "${dir%?}"; done <all.txt
  1. How can this code be revised to remove the first letter? i.e. In my example rename "AFolder_01" to "Folder_01"
  2. How can this code be revised to add charterer back at the beginning of the folder name: i.e. in my example rename "Folder_01" to "AFolder_01"
1
  • 1
    As to no. 2 - how to add a character to a file name... seriously ? Jan 26, 2017 at 18:57

1 Answer 1

4

Once you have your directory name in a variable (e. g. dir), you can:

mv "$dir" "${dir:1}"

This will strip the first character from the variable. I shall leave sanity-checking that the new directory does not yet already exist up to you.

To add something to the beginning (e. g. the letter A):

mv "$dir" "A$dir"
0