Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have to rename a complete folder tree recursively so that no uppercase letter appears anywhere (it's C++ source code, but that shouldn't matter).

Bonus points for ignoring CVS and Subversion version control files/folders. The preferred way would be a shell script, since a shell should be available on any Linux box.

There were some valid arguments about details of the file renaming.

  1. I think files with the same lowercase names should be overwritten; it's the user's problem. When checked out on a case-ignoring file system, it would overwrite the first one with the latter, too.

  2. I would consider A-Z characters and transform them to a-z, everything else is just calling for problems (at least with source code).

  3. The script would be needed to run a build on a Linux system, so I think changes to CVS or Subversion version control files should be omitted. After all, it's just a scratch checkout. Maybe an "export" is more appropriate.

like image 499
vividos Avatar asked Sep 30 '08 10:09

vividos


People also ask

How do I rename all files in lowercase?

rename "%f" "%f" - rename the file with its own name, which is actually lowercased by the dir command and /l combination.

How do I change capitals to lowercase in Linux?

The `tr` command can be used in the following way to convert any string from uppercase to lowercase. You can use `tr` command in the following way also to convert any string from lowercase to uppercase.

Which of the following is the script for renaming files in folder from uppercase to lowercase?

Rename files from uppercase to lowercase with mv command Renaming multiple files from uppercase to lowercase with the mv command is easier if you know a little bit of Bash scripting.


2 Answers

Smaller still I quite like:

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

On case insensitive filesystems such as OS X's HFS+, you will want to add the -f flag:

rename -f 'y/A-Z/a-z/' *
like image 189
tristanbailey Avatar answered Sep 24 '22 06:09

tristanbailey


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").

like image 44
Alex B Avatar answered Sep 24 '22 06:09

Alex B