Linux Rename Files to Uppercase

Linux rename files to uppercase


for f in * ; do mv -- "$f" "$(tr [:lower:] [:upper:] <<< "$f")" ; done

How do I rename all folders and files to lowercase on Linux?

A concise version using the "rename" command:

find my_root_dir -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;

This avoids problems with directories being renamed before files and trying to move files into non-existing directories (e.g. "A/A" into "a/a").

Or, a more verbose version without using "rename".

for SRC in `find my_root_dir -depth`
do
DST=`dirname "${SRC}"`/`basename "${SRC}" | tr '[A-Z]' '[a-z]'`
if [ "${SRC}" != "${DST}" ]
then
[ ! -e "${DST}" ] && mv -T "${SRC}" "${DST}" || echo "${SRC} was not renamed"
fi
done

P.S.

The latter allows more flexibility with the move command (for example, "svn mv").

change lowercase file names to uppercase with awk ,sed or bash

PREFACE:

If you don't care about the case of your extensions, simply use the 'tr' utility in a shell loop:

for i in *.txt; do mv "$i" "$(echo "$i" | tr '[a-z]' '[A-Z]')"; done

If you do care about the case of the extensions, then you should be aware that there is more than one way to do it (TIMTOWTDI). Personally, I believe the Perl solution, listed here, is probably the simplest and most flexible solution under Linux. If you have multiple file extensions, simply specify the number you wish to keep unchanged. The BASH4 solution is also a very good one, but you must be willing to write out the extension a few times, or alternatively, use another variable to store it. But if you need serious portability then I recommend the last solution in this answer which uses octals. Some flavours of Linux also ship with a tool called rename that may also be worth checking out. It's usage will vary from distro to distro, so type man rename for more info.

SOLUTIONS:

Using Perl:

# single extension
perl -e 's/\.[^\.]*$/rename $_, uc($`) . $&/e for @ARGV' *.txt

# multiple extensions
perl -e 's/(?:\.[^\.]*){2}$/rename $_, uc($`) . $&/e for @ARGV' *.tar.gz

Using BASH4:

# single extension
for i in *.txt; do j="${i%.txt}"; mv "$i" "${j^^}.txt"; done

# multiple extensions
for i in *.tar.gz; do j="${i%.tar.gz}"; mv "$i" "${j^^}.tar.gz"; done

# using a var to store the extension:
e='.tar.gz'; for i in *${e}; do j="${i%${e}}"; mv "$i" "${j^^}${e}"; done

Using GNU awk:

for i in *.txt; do

mv "$i" $(echo "$i" | awk '{ sub(/.txt$/,""); print toupper($0) ".txt" }');
done

Using GNU sed:

for i in *.txt; do

mv "$i" $(echo "$i" | sed -r -e 's/.*/\U&/' -e 's/\.TXT$/\u.txt/');
done

Using BASH3.2:

for i in *.txt; do

stem="${i%.txt}";

for ((j=0; j<"${#stem}"; j++)); do

chr="${stem:$j:1}"

if [[ "$chr" == [a-z] ]]; then

chr=$(printf "%o" "'$chr")

chr=$((chr - 40))

chr=$(printf '\'"$chr")
fi

out+="$chr"
done

mv "$i" "$out.txt"

out=
done

rename file names from lower case to upper case

You can always use the free Bulk Rename utility.

rename all the files in the current directory whose name conatains upper-case into all lower case

You are not passing any data into the tr program, and you are not capturing any output either.

If you are using sh:

for f in *[A-Z]*
do
if [ -f "$f" ]; then
new_name=$(echo "$f"|tr 'A-Z' 'a-z')
mv "$f" "$new_name"
fi
done

Note the indentation - it makes code easier to read.

If you are using bash there is no need to use an external program like tr, you can use bash expansion:

for f in *[A-Z]*
do
if [[ -f $f ]]; then
new_name=${f,,*}
mv "$f" "$new_name"
fi
done

UNIX rename files/directories to uppercase

Try this way :

find . -depth |while read LONG; do SHORT=$( basename "$LONG" | tr '[:lower:]' '[:upper:]' ); DIR=$( dirname "$LONG" ); if [ "${LONG}" != "${DIR}/${SHORT}"  ]; then mv "${LONG}" "${DIR}/${SHORT}" ; fi; done

or, if you want the readable version (no one-liner) :

find . -depth | \
while read LONG; do
SHORT=$( basename "$LONG" | tr '[:lower:]' '[:upper:]' )
DIR=$( dirname "$LONG" )
if [ "${LONG}" != "${DIR}/${SHORT}" ]; then
mv "${LONG}" "${DIR}/${SHORT}"
fi
done

This will rename files before, then the directory they're in, in the proper order.

Rename output file in uppercase without the extensions bash

bash parameter expansion makes both upper-casing the expansion of a variable and removing part of the resulting string easy:

filename=test.des.utf8

# Upper-cased version of filename
newfilename="${filename^^}"
# Remove everything from the first . to the end and add a new extension
newfilename="${newfilename%%.*}.cpy"

# Remove echo when happy with results
echo cp "$filename" "$newfilename"

How can i find and rename multiple files

Possible solution with Perl rename:

find /mydir -depth -type f -exec rename -v 's/(.*\/)?([^.]*)/$1\U$2/' {} +

The commands in the question have several problems.

You seem to confuse the syntax of find's -exec action and xargs.

find /mydir -depth -type f -exec rename -v 'substitution_command' {} \;
find /mydir -depth -type f| xargs -n 1 rename -v 'substitution_command'

The xargs version has problems in case a file name contains a space.

If you replace \; with +, multiple file names are passed to one invocation of rename.


The substitution command is only supported by the Perl version of the rename command. You might have to install this version. See Get the Perl rename utility instead of the built-in rename


The substitution did not work in my test. I successfully used

rename -v 's/(.*\/)?([^.]*)/$1\U$2/' file ...

The first group (.*\/)? optionally matches a sequence of characters with a trailing /. This is used to copy the directory unchanged.

The second group ([^.]*) matches a sequence of characters except ..

This is the file name part before the first dot (if any) which will be converted to uppercase. In case the file name has more than one extension, all will remain unchanged, e.g.

Path/To/Foo.Bar.Baz -> Path/To/FOO.Bar.Baz



Related Topics



Leave a reply



Submit