Shell script to rename given file names to from uppercase to lowercase OR lowercase to uppercase

#!/bin/bash
# 
# Shell script to rename given file names to from uppercase to 
# lowercase OR lowercase to uppercase
# 
# Copyright (C) 2005 nixCraft project.
#
# This script licensed under GNU GPL version 2.0 or above
#
# Support/FeedBack/comment :  http://cyberciti.biz/fb/
# -------------------------------------------------------------------
# To rename file uppercase to lowercase create sym link:
# ln -s /path/2upper /path/2lower
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
FILES="$1"
ME="$(basename $0)"
 
# function to display message and exit with given exit code
function die(){
  echo -e "$1"
  exit $2
}
 
# exit if no command line argument given
[ "$FILES" == "" ] && die "Syntax: $ME {file-name}\nExamples:\n $ME xyz\n $ME \"*.jpg\"" 1 || :
# scan for all input file
for i in $FILES
do
   # see if upper to lower OR lower to upper by command name
   [ "$ME" == "2upper" ] && N="$(echo "$i" | tr [a-z] [A-Z])" ||  N="$(echo "$i" | tr [A-Z] [a-z])"
   # if source and dest file not the same then rename it  
   [ "$i" != "$N" ] &&  mv "$i" "$N" || :
done
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
Category List of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN Alpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Reginald McNulty Oct 8, 2009 @ 18:36

    Hi,

    I need a script that will copy 360 files starting from run_1_00001.cbf to run_1_00360.cbf to a new directory while changing all of the files to include the word peak just prior to the file extension. For example run_1_00001.cbf changed to run_1_00001peak.cbf and placed in another directory.

  • send2us Jul 6, 2009 @ 14:56

    how to use this? can use for rename all files in a folder?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.