Find Files and then convert them to Uppercase

Hi All,

So I'm new to scripting and I've been put in a position to convert a bunch of files with specific extensions in a folder and all its subfolders to uppercase including their extension. I figure so far I could do something like this:

...
...

and then input $line into another bash script that would capitalize the filenames that it found and outputted to txtlist. I've been trying various combinations but so far had not had much luck. Could someone give me a hand?

Thanks!

You can use tr for that.

find ./ -name '*.txt' | tr '[a-z]' '[A-Z]'

...though why you'd want to, I have no idea -- UNIX file names allow mixed cases and are case sensitive, meaning the actual name of the file is irretrievably lost by uppercasing it.

1 Like

Hi, Thanks for that quick response, well the idea here is the application were using to read these files will only read them in uppercase thanks to the vendor. I tried your find command, but the problem there is that it would capitalize the entire path and not just the filename. I hope this makes sense!

Thanks

What a strange application. Oh well.

What's your system? This will be simple enough if you have GNU awk:

find /path -name '*.txt' | awk -F"/" -v OFS="/" '{$NF=toupper($NF)} 1'

nawk may have it on other systems but of this I'm less certain.

1 Like

The System I am on is running Solaris 10 - I'll give that a shot right away and let you know.

bash-3.00$ find /path/to/dir -name '*.txt' | awk -F"/" -v OFS="/" '{$NF=toupper($NF)} 1'
awk: syntax error near line 1
awk: bailing out near line 1

output, i imagine this is probably either a different implementation of awk that came with Solaris 10 maybe?

---------- Post updated at 07:05 PM ---------- Previous update was at 07:04 PM ----------

Solaris 10 is this OS - i thought i had posted that right before I posted the output!

That's why I suggested nawk on other systems.

Or gawk, if you have it.

1 Like

There we go, NAWK did the trick, you sir are awesome. Im guessing I just need to throw an MV in here somewhere and that should do the trick to rename?

you could do:

#!/usr/bin/env bash
find ... | while IFS= read -r file; do
   upper=$(nawk ... <<< "$file")
   echo mv "$file" "$upper"
done

if that looks good, remove the echo

1 Like

Thanks Neutronscott, Ill give that a shot right now.

---------- Post updated at 07:49 PM ---------- Previous update was at 07:46 PM ----------

You guys are awesome, that worked perfectly neutronscott, thank you both for the help!

---------- Post updated at 07:50 PM ---------- Previous update was at 07:49 PM ----------

Just for anyone's reference in the future who is searching: an example of this in its totality for Solaris 10 especially:

#!/usr/bin/env bash
find /path/to/search/dir -name '*.txt'| while IFS= read -r file; do
   upper=$(nawk -F"/" -v OFS="/" '{$NF=toupper($NF)} 1' <<< "$file")
   echo mv "$file" "$upper"
done

In bash version 4 or above, you can perform this

for i in $(find /path/to/search/dir -name '*.txt'); do
     mv "${i}" "${i^^}"
done

Being he has Solaris, I wouldn't assume he has anything higher than bash 2...

Also, that's a useless use of backticks. The loop can be rewritten far more safely as

find ... | while read LINE
do
...
done

Just for the record, the whole construct can be sped up by omitting the while loop:

find /path/to/dir -name "*.txt" -print | sed -e 's/^/"/' -e 's/$/"/' |  nawk -F/ '{orig=$0; $NF=toupper($NF); print "mv " orig " " $0}'  OFS=/

This will just print the commands; to run them, pipe it to sh:

find /path/to/dir -name "*.txt" -print | sed -e 's/^/"/' -e 's/$/"/' |  nawk -F/ '{orig=$0; $NF=toupper($NF); print "mv " orig " " $0}' OFS=/ | sh

The sed part inserts quotes around the filename.