Linux lowercase all files

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' *9* # 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.

Источник

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:

List Directory 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.
Читайте также:  Linux what is ipcs

Sample output after renaming files and subdirectories to lowercase in Files directory.

Rename Files and Directory Names to Lowercase

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

Lowercase File Names Using Script

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.

Источник

Convert Filenames to Lowercase through Ubuntu Command Line

Change File Names to Lower Case

While working with Linux, you might come across some utilities and apps that only work with file names in lowercase. We do not usually save files in this format and might have to look for a workaround that recursively converts all filenames in a folder to lowercase. In this article, we will show you two ways to convert all files and folder names in a given path to lowercase, simply through the command line.

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.

Here is how you can list the contents of your folder using the find command:

$ find [directory_name] -depth

My Downloads folder, which I will be using as a sample for this article, has all files starting from uppercase letters and also contains a few in between the names.

List files with the find command

Method 1: Using the rename command

In this method, we will be making use of the Ubuntu find, Xargs and rename commands in order to recursively rename all files/folders in a given directory.

Open your Ubuntu command line, the Terminal, either through the Application Launcher search or the Ctrl+Alt+T shortcut.

Here is the syntax of the command you will be using:

$ find [directory_name] -depth | xargs -n 1 rename -v ‘s/(.*)\/([^\/]*)/$1\/\L$2/’ <> \;

If you do not have the rename command installed on your system, you might get an error when you run the above command. You can install rename to your Ubuntu through the following apt-get command:

$ sudo apt-get install rename

I will be using the following command in order to convert filenames to lowercase in my Downloads directory:

$ find Downloads -depth | xargs -n 1 rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' <> \;

Change file names to lowercase on Linux

When I listed the contents of the directory again, I was able to see all file names converted to lowercase as follows:

Читайте также:  Linux kernel source files

Filename list

Method 2: Using a script to rename the files

In this method, we will make use of a bash script that uses the find and mv commands in order to recursively rename file and folder names of a directory, including the directory name itself.

Open the Terminal application and move to the bin folder as follow:

Now, open a new script file in one of your favorite text editors. We will use the nano editor in order to open an empty script file by the name of lowercase_filenames.sh

$ sudo nano lowercase_filenames.sh

In that empty file, add the following script.

#!/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 exit 0

Tip: Instead of typing the whole script into you bash file, you can copy it from here and paste in the Terminal by using the Ctrl+Shift+V, or by using the Paste option from the right-click menu.

This is how your file will look like:

File lowercase rename script

Now, exit the file through the Ctrl+X shortcut and save the file on the “Save modified buffer?” prompt by typing Y and then hitting enter.

In order to make this file an executable script, run the following command in your Terminal:

$ sudo chmod +x lowercase_filenames.sh

Now you are ready to use the script on any of your folders.

When I run the script on my Downloads folder, I see all the files and subfolders names converted to lowercase as follows:

Run script to turn filenames to lower case

So, these were the two ways through which you can rename the filenames to all lowercase letters so that the application you are using does not fail to recognize any uppercase files names.

Источник

Convert all file extensions to lower-case

I’m trying to lower-case all my extensions regardless of what it is. So far, from what I’ve seen, you have to specify what file extensions you want to convert to lower-case. However, I just want to lower-case everything after the first last dot . in the name. How can I do that in bash ?

The asker has another question recently about a bash script to rename files. So he might be asking how to write a bash script that changes the name of every file (in his home directory? in and under a directory? just in a directory?) to change each uppercase letter in the filename’s extension to lowercase, except for the first letter. E.g., “PICTURE.JPG” would be renamed to “PICTURE.Jpg”.

11 Answers 11

You can solve the task in one line:

find . -name '*.*' -exec sh -c ' a=$(echo "$0" | sed -r "s/([^.]*)\$/\L\1/"); [ "$a" != "$0" ] && mv "$0" "$a" ' <> \; 

Note: this will break for filenames that contain newlines. But bear with me for now.

Читайте также:  Readline linux что это

Example of usage

$ mkdir C; touch 1.TXT a.TXT B.TXT C/D.TXT $ find . . ./C ./C/D.TXT ./1.TXT ./a.TXT ./B.TXT $ find . -name '*.*' -exec sh -c 'a=$(echo "$0" | sed -r "s/([^.]*)\$/\L\1/"); [ "$a" != "$0" ] && mv "$0" "$a" ' <> \; $ find . . ./C ./C/D.txt ./a.txt ./B.txt ./1.txt 

Explanation

You find all files in current directory ( . ) that have period . in its name ( -name ‘*.*’ ) and run the command for each file:

a=$(echo "$0" | sed -r "s/([^.]*)\$/\L\1/"); [ "$a" != "$0" ] && mv "<>" "$a" 

That command means: try to convert file extension to lowercase (that makes sed ):

$ echo 1.txt | sed -r "s/([^.]*)\$/\L\1/" 1.txt $ echo 2.TXT | sed -r "s/([^.]*)\$/\L\1/" 2.txt 

and save the result to the a variable.

If something was changed [ «$a» != «$0» ] , rename the file mv «$0» «$a» .

The name of the file being processed ( <> ) passed to sh -c as its additional argument and it is seen inside the command line as $0 . It makes the script safe, because in this case the shell take <> as a data, not as a code-part, as when it is specified directly in the command line. (I thank @gniourf_gniourf for pointing me at this really important issue).

As you can see, if you use <> directly in the script, it’s possible to have some shell-injections in the filenames, something like:

In this case the injection will be considered by the shell as a part of the code and they will be executed.

While-version

Clearer, but a little bit longer, version of the script:

find . -name '*.*' | while IFS= read -r f do a=$(echo "$f" | sed -r "s/([^.]*)\$/\L\1/"); [ "$a" != "$f" ] && mv "$f" "$a" done 

This still breaks for filenames containing newlines. To fix this issue, you need to have a find that supports -print0 (like GNU find ) and Bash (so that read supports the -d delimiter switch):

find . -name '*.*' -print0 | while IFS= read -r -d '' f do a=$(echo "$f" | sed -r "s/([^.]*)\$/\L\1/"); [ "$a" != "$f" ] && mv "$f" "$a" done 

This still breaks for files that contain trailing newlines (as they will be absorbed by the a=$(. ) subshell. If you really want a foolproof method (and you should!), with a recent version of Bash (Bash≥4.0) that supports the ,, parameter expansion here’s the ultimate solution:

find . -name '*.*' -print0 | while IFS= read -r -d '' f do base=$ ext=$ a=$base.$ [ "$a" != "$f" ] && mv -- "$f" "$a" done 

Back to the original solution

Or in one find go (back to the original solution with some fixes that makes it really foolproof):

find . -name '*.*' -type f -exec bash -c 'base=$ ext=$ a=$base.$; [ "$a" != "$0" ] && mv -- "$0" "$a"' <> \; 

I added -type f so that only regular files are renamed. Without this, you could still have problems if directory names are renamed before file names. If you also want to rename directories (and links, pipes, etc.) you should use -depth :

find . -depth -name '*.*' -type f -exec bash -c 'base=$ ext=$ a=$base.$; [ "$a" != "$0" ] && mv -- "$0" "$a"' <> \; 

so that find performs a depth-first search.

You may argue that it’s not efficient to spawn a bash process for each file found. That’s correct, and the previous loop version would then be better.

Источник

Оцените статью
Adblock
detector