- How to Rename All Files to Lowercase or Uppercase in Linux
- How to Rename All Files to Lowercase or Uppercase in Linux
- Rename Files to Lowercase using find, rename and xargs
- Rename to Uppercase using find, rename & xargs
- Rename All Files and Directory Names to Lowercase in Linux
- 1. Using find, xargs and rename Commands Together
- 2. Using find and mv Commands in Shell Script
- Batch rename files to lowercase
- 4 Answers 4
- Renaming files from upper case to lower case
- 7 Answers 7
How to Rename All Files to Lowercase or Uppercase in Linux
Sometimes you may need to rename files to lowercase or uppercase in Linux. In this article, we will learn how to do this. There are several ways to rename files & folders. We will look at one of them using find, rename and xargs function. It is simple and easy to use.
How to Rename All Files to Lowercase or Uppercase in Linux
We will look at simple way to rename all files to lowercase in Linux. Let us say you have the following folder /data
file1.txt /Files/File2.txt FILE3.txt
Rename Files to Lowercase using find, rename and xargs
We will rename function for renaming files as rename function allows you to rename multiple files & folders at once. It also has shortcuts to directly rename files & folders as lowercase or uppercase. First, we will use find function to get a list of all files & subfolders in our target folder.
$ find -depth /data file1.txt FILE3.txt /Files/File2.txt /Files data
We use -depth option to get an exhaustive list of all files & folders in our target location. Also, it lists each folder & subfolder’s contents before it lists the folder or subfolder itself.
We pipe the above output to xargs command which will construct separate rename command for each file and folder in our target folder.
$ find /data -depth | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' <> \; file1.txt renamed as file1.txt FILE3.txt renames as file3.txt /Files/File2.txt renamed as /Files/file2.txt /Files renamed as /files
As you can see above, it renames the files in each folder before renaming the folder itself. In the above command, -n1 is used to instruct xargs to use at most 1 argument per line of input. We also use $1 to indicate existing filename and L$2 to specify to rename existing filename to lowercase.
If you only want to rename folders and not files, you can add -type d option in find command.
$ find /data -depth -type d | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' <> \; /Files renamed as /files
If you only want to rename files and not folders use -type f option in find command.
$ find /data -depth -type f | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' <> \; file1.txt renamed as file1.txt FILE3.txt renames as file3.txt /Files/File2.txt renamed as /Files/file2.txt
Rename to Uppercase using find, rename & xargs
Similarly, you can rename files & folders to uppercase, by simply replacing L$2 with U$2 (L for lowercase and U for uppercase) in the above commands.
$ find /data -depth | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\U$2/' <> \; file1.txt renamed as FILE1.txt FILE3.txt renames as FILE3.txt /Files/File2.txt renamed as /Files/FILE2.txt /Files renamed as /FILES
If you only want to rename folders and not files, you can add -type d option to find command.
$ find /data -depth -type d | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\U$2/' <> \; /Files renamed as /FILES
If you only want to rename files and not folders use -type f option in find command.
$ find /data -depth -type f | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\U$2/' <> \; file1.txt renamed as FILE1.txt FILE3.txt renames as FILE3.txt /Files/File2.txt renamed as /Files/FILE2.txt
In this article, we have learnt how to rename to lowercase or uppercase in Linux.
Rename All Files and Directory Names to Lowercase in Linux
In our previous article, we have described how to count the number of files and subdirectories inside a given directory. This guide will show you how to rename all files and directories names to lowercase in Linux.
There are several ways to achieve this, but we’ll explain two of the most efficient and reliable methods. For the purpose of this guide, we have used a directory named Files which has the following structure:
1. Using find, xargs and rename Commands Together
rename is a simple command line utility for renaming several files at once in Linux. You can use it together with find utility to rename all files or subdirectories in a particular directory to lowercase as follows:
$ find Files -depth | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' <> \;
Explanation of options used in the above command.
- -depth – lists each directory’s contents before the directory itself.
- -n 1 – instructs xargs to use at most one argument per command line from find output.
Sample output after renaming files and subdirectories to lowercase in Files directory.
Another alternative way using the find and mv commands in a script as explained below.
2. Using find and mv Commands in Shell Script
First create your script (you can name it anything you prefer):
Then add the code below in it.
#!/bin/bash #print usage if [ -z $1 ];then echo "Usage :$(basename $0) parent-directory" exit 1 fi #process all subdirectories and files in parent directory all="$(find $1 -depth)" for name in $; do #set new name in lower case for files and directories new_name="$(dirname "$")/$(basename "$" | tr '[A-Z]' '[a-z]')" #check if new name already exists if [ "$" != "$" ]; then [ ! -e "$" ] && mv -T "$" "$"; echo "$ was renamed to $" || echo "$ wasn't renamed!" fi done echo echo #list directories and file new names in lowercase echo "Directories and files with new names in lowercase letters" find $(echo $1 | tr 'A-Z' 'a-z') -depth exit 0
Save and close the file, then make the script executable and run it:
$ chmod +x rename-files.sh $ rename-files.sh Files #Specify Directory Name
You may also like to read these following related articles.
In this guide, we expalined you how to rename all files and directories to lowercase in Linux. If get any errors, please hit us up via the feedback form below. You can as well offer us any other methods of doing the same.
Batch rename files to lowercase
Is there a way to rename all files in a directory to lowercase|uppercase? I am looking for a oneliner command. I loved TotalCommander’s Alt + F7 , now I need that functionality in the Terminal.
4 Answers 4
For each file a_file in current directory rename a_file to lower case.
for a_file in *;do mv -v "$a_file" `echo "$a_file" | tr [:upper:] [:lower:]` ;done;
For upper case reverse the arguments to [:lower:] [:upper:]
For even more control * can be replaced with ls .
For example in a directory containing 1.txt, 2.txt, 3.txt, 1.jpg, 2.jpg and 3.jpg in order to filter only *.jpg files, ls can be used:
for a_file in $(ls *.jpg);do mv -v $a_file `echo $a_file | tr [:upper:] [:lower:]` ;done;
The above code will assign to a_file variable all files with .jpg extension.
Update added -v option to mv command as per sds suggested.
you might want to pass «-v» to mv so that you see what is being done; you might not want to pass «-f» because this may clobber existing files
Sorry, but the part about ls *jpg is a bad idea. mywiki.wooledge.org/ParsingLs You can achive the same result with *jpg along where you had * in the first place.
There’s a more elegant and general utility called prename .
Written by Larry Wall, it comes with perl so it is most likely already available on your system as /usr/bin/prename (if you have setup alternatives is may also be available as /usr/bin/rename symlinked via /etc/alternatives to prename )
Using it you can rename multiple files in one command by providing any perl expression (including but not limited to substitution s/// or char transliteration tr/// ):
# Lowercase all *.JPG filenames: prename tr/A-Z/a-z/ *.JPG # Change any 'No' in a filename to a 'Yes': prename s/No/Yes/g *No* # increment first sequence of digits in a filename by 3: prename 's/(9+)/$1+3/e' *5* # If a file contains 'aaa', append '.bak' to its name prename 'if (/aaa/) ' *
Another nice thing about prename is that it protects you, in the case of renaming a file to an existing file name.
man prename for more details.
Renaming files from upper case to lower case
I need to elaborate a little bit more. These commands are actually for a BBS FTN tosser. The configuration file for which these commands reside in, only allow specific declarations. Here are a few examples:
exec "/home/imp/imp/poll.sh" *.su? *.mo? *.tu? *.we? *th? *.fr? *.sa? *.pkt flag toss!.now [0-9a-Z][0-9a-Z][0-9a-Z][0-9a-Z][0-9a-Z][0-9a-Z][0-9a-Z][0-9a-Z]. exec "/home/imp/imp/poll.sh /home/imp/hpt/secure" /home/imp/hpt/secure/*.[STFWMstfWM][ouaherOUAHER][0-9A-ZA-a] *.[pP][kK][tT]
So that’s why I want to use that syntax ([0-9a-z] (for example)). The problem is that the mailer is looking for lowercase filenames, but only uppercase filenames exist. I’m trying to convert a file from uppercase to lowercase using the syntax below:
mv /home/imp/hpt/outbound/[0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z][0-9A-Z].[STFWMfWM][OUAHER]3 /home/imp/hpt/outbound/[0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z].[stfwm][ouaher][0-9a-za-a]
I don’t think I have the syntax correct. Here’s an example of a file name I want to rename: 0000FE70.FR0 Any and all help is greatly appreciated. Thanks.
7 Answers 7
Whenever you want to change one class of characters into another, use tr .
for f in *; do test -f "$f" && echo mv "$f" "$( tr '[:upper:]' '[:lower:]'
The script will rename all files in the current directory to all lowercase letters. It will skip directories. Remove the echo when you're certain it does what you want. You may replace [:upper:] and [:lower:] with A-Z and a-z respectively if you only have standard ASCII filenames (note: A-Z , not [A-Z] ).
Alternatively, using Bash's built-in upper-to-lowercase variable substitution:
for f in *; do test -f "$f" && echo mv "$f" "$" done
@Daniele It's a "here-string". It passes the given string to the standard input of the command. tr '[:upper:]' '[:lower:]'
@Manngo a) The test makes sure that $f refers to a regular file, and not, say, a directory. b) Nobody knows.
This answer from Alex B at Stack Overflow will help you to rename both files and subfolders under a directory.
It consists on using both find and rename Linux commands. I would only add that the command provided in the link above will modify directory names as well. In case that you want only to change file names, you need to change the -depth option with the next one: -type f .
Another regular expression given to the rename command could be the one below, which I think it is more easy to understand.
find my_root_dir -type f -execdir rename 'y/A-Z/a-z/' <> \;
Be aware that there is a necessary white space between the rename command regular expression and the curly braces, and another one between them and the command termination character \;
Also remember that it will rename every file not only in the directory where you execute the command from (the my_root_dir directory) but also to all the files under every subfolder contained into this one. You can use the -maxdepth 0 option with the find command to force it to only apply the tests and actions to the starting-points themselves.