Search code examples
linuxbashshellunixmv

How can I change the name of file in the child's directory


There is a child directory who name is stored in a variable called temp. I'd like to rename all files in that directory to their lower case version.

So I wrote this code:

mv ls $temp 'ls $temp | tr [:upper:][:lower:] [:lower:][:upper:]'

but it doesn't work. How can I change it?


Solution

  • You need a loop.

    You can use Bash brace expansion to convert to lower case, instead of tr which creates an extra process each time:

    #!/bin/bash
    cd "$temp"
    for f in *; do
      mv "$f" "${f,,}"
    done
    

    If you want to reverse the case of each character of the file name (thanks @SLePort for the tip):

    #!/bin/bash
    cd "$temp"
    for f in *; do
      mv "$f" "${f~~}"
    done