0

I am trying to bulk rename many folders but for some reason my approach does not seem to work. I am trying to use the following script but it does nothing. I am new to programming so any suggestions are appreciated. These are the folder names that i have.

test.custom.tmp         untitledfolder.custom.tmp   wis.custom.tmp

I need them renamed to

test untitledfolder wis

I am using the following script but it does nothing.

while read line; do
  newname=$(echo "$line" | cut -f 1 -d '_')
  mv $line newname;
done < read

Here i created a file called read with all the foldernames that need renaming.

What is wrong with this script and also is there a better way to get this done? Thanks.

ranjit abraham
  • 101
  • 1
  • 8
  • 2
    Is `read` a file? What is the `done < read` reading from? Also, what operating system are you using? Different systems have different tools available. – terdon May 31 '22 at 14:30
  • 1
    Is it `test untitled wis` or *`test untitledfolder wis`*. – schrodingerscatcuriosity May 31 '22 at 14:30
  • How should name collisions be handled? I.e., what should happen with the two names `my.custom.name1` and `my.custom.name2`? – Kusalananda May 31 '22 at 14:35
  • What standard are you referring to? The POSIX standard? Note that while `echo` is a POSIX command, [its behaviour is left mostly unspecified and in practice it's not portable](/q/65803). `read` is also a POSIX command, but [you're using it improperly here if the intent is to read a line from a file](/q/209123). – Stéphane Chazelas May 31 '22 at 15:17
  • Apologies. I have added some more info and corrected the question. – ranjit abraham May 31 '22 at 15:28
  • @Kusalananda there wont be any name collisions. The folders are generated by a program which takes care of that. – ranjit abraham May 31 '22 at 15:28
  • Don't re-invent the wheel, it's likely to come out triangular or hexagonal or just explode in your face. Use the [perl rename](https://unix.stackexchange.com/search?q=perl+rename) utility instead. Or `rename` from util-linux or `mmv` or similar instead...but the perl rename is the best tool for this job. – cas Jun 01 '22 at 09:35

2 Answers2

2

A simple way would be:

$ for d in *.custom.tmp/; do 
    echo mv -- "$d" "${d%%.*}"
  done
mv -- test.custom.tmp/ test
mv -- untitledfolder.custom.tmp/ untitledfolder
mv -- wis.custom.tmp/ wis

Once you are sure that's the result you want, remove the echo.


The ${d%%.*} is a parameter expansion, precisely:

${parameter%word}

${parameter%%word}

The word is expanded to produce a pattern and matched according to the rules described below (see Pattern Matching). If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted.

In this case it takes the string for example test.custom.tmp and removes everythig after the first ..

schrodingerscatcuriosity
  • 12,087
  • 3
  • 29
  • 57
1

If you want to keep your aproach, you can do something like:

while read line; do
  newname=$(echo "$line" | cut -f 1 -d '.');
  mv $line $newname;
done < read

Just keep in mind that your delimiter is a point (-d '.'), and also use the $newname as a variable, not as a string.

This corrects your scripts errors, but for a better approach you can use @schrodingerscatcuriosity answer.

Dani Garcia
  • 406
  • 3
  • 6
  • See [Understanding "IFS= read -r line"](//unix.stackexchange.com/q/209123) and [Why is printf better than echo?](//unix.stackexchange.com/q/65803) and [When is double-quoting necessary?](//unix.stackexchange.com/q/68694) and [What does "--" (double-dash) mean?](//unix.stackexchange.com/a/590210) – Stéphane Chazelas May 31 '22 at 15:45
  • Thats why my script didnt work. I was calling newname as a string. sorry.. noob. Just started doing scripting a few weeks back. – ranjit abraham May 31 '22 at 16:43