0

Including the extension. Eg file.txt --> FILE.TXT

If anyone could point me in the general direction then I'd be grateful :)


And here is just some random text because the character count was too low for Stackoverflow...

Noob
  • 33
  • 1
  • 7

4 Answers4

5

An initial solution would be:

rename 'y/a-z/A-Z/' *

It takes every file/directory in current directory, and changes every character in the range a-z by its corresponding uppercase version.

The problem with rename is there is no option to go inside directories to apply the renaming recursively, and * character is expanded to names of current directory (files and directories). Even more, this command will rename directories also, but you only want to rename files.

To do it recursively, but over files only, you can use find, which search recursively, and pass each file to rename:

find . -type f -execdir rename 'y/a-z/A-Z/' {} \;

This command searches only files, and executes rename over each file inside the directory (execdir option) where that file has been found. That's important because otherwise find will pass the complete path of the file (eg: ./fold1/fold2/file.txt') to rename, which in turns will try to pass to uppercase the complete path: (./FOLD1/FOLD2/FILE.TXT) which will cause an error because folders FOLD1 and FOLD2 don't exist.

ABu
  • 10,423
  • 6
  • 52
  • 103
  • The `-execdir` trick did it quite well, thanks. You can replace `-type f` with `-depth` if you also want to uppercase the directories themselves. – Pedro Gimeno Nov 19 '20 at 10:14
3

Using bash, this is easy:

for f in *; do mv "$f" "${f^^}"; done

The expansion ${f^^} converts the name of the file to uppercase.

In another shell, using tr:

for f in *; do mv "$f" "$(echo "$f" | tr '[:lower:]' '[:upper:]')"; done
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
1

I would suggest something like that:

ls|sed 's/\(.*\)/mv "\1" "\U\1"/' | sh

I like to use sed this way, because it is easy to inspect the output before piping it into sh (removing |sh in a first step)

Benoit Lacherez
  • 506
  • 2
  • 8
1

Using perl

perl -e 'for (glob "*.txt" ){ rename $_,uc($_)}'

glob "*" creates a list of all the .txt files in the current directory. rename replaces each value with a uc (uppercase) value.

user1717259
  • 2,717
  • 6
  • 30
  • 44