Rename Multiple Files While Keeping the Same Extension on Linux

Rename file while keeping the extension in Linux?

you can do this through bash.

can you please provide more details. how your deciding this $dir and $count variable value.

if you already know by what you want to change the file name like below

OLD NAME|NEW NAME|Path

test.1|newtest.1|Path

arty.2|xyz.2|Path

if you want to replace it by specific names then you can prepare a list like above and then traverse through the file by while or for loop. below is simple bash snippet for case where you have files under multiple directory

while IFS="|" read OLD NEW PATH
do
cd $Path

filename=`echo $NEW|awk -F '.' '{print $1}'`

filetype=`echo $NEW|awk -F '.' '{print $2}'`

mv $OLD $filename.$filetype

done<FILE_PATH

if want to perform operation under single directory then below snippet will work.

for i in $(ls /tmp/temp)
do
filename=`echo $i|awk -F "." '{print $1}'`
fileType=`echo $i|awk -F "." '{print $2}'`
mv $i $filename.$fileType
done

Rename multiple files with different extensions on linux

Using Extract filename and extension in Bash, I would say:

for file in *
do
extension="${file##*.}"
filename="${file%.*}"
mv "$file" "${filename}-4cc8.${extension}"
done

This loops through all the files, gets its name and extension and then moves it (that is, renames it) to the given name with an extra -4cc8 value before the extension.

Bulk rename image files but keep extension same linux

Something as simple as this should do the trick:

i=0; for f in *.*; do mv -- "$f" "$((++i)).${f##*.}"; done

If you need to specify the extensions, use: for f in *.jpg *.png ...

How to rename files without changing extension in Linux 102221.pdf to 102221_name.pdf

This is what you want I think:

for x in *; do mv "$x" "${x%.*}_name.${x##*.}"; done
  • ${x%.*} will give the name of the file without extention
  • ${x##*.} will extract the extentions

Rename multiple files?

use find:

find /path -depth -name "*.php" -exec sh -c 'mv "$1" "${1%.php}.html"' _ {} \;

How do I rename the extension for a bunch of files?

For an better solution (with only bash functionality, as opposed to external calls), see one of the other answers.


The following would do and does not require the system to have the rename program (although you would most often have this on a system):

for file in *.html; do
mv "$file" "$(basename "$file" .html).txt"
done

EDIT: As pointed out in the comments, this does not work for filenames with spaces in them without proper quoting (now added above). When working purely on your own files that you know do not have spaces in the filenames this will work but whenever you write something that may be reused at a later time, do not skip proper quoting.

How to bulk rename files and remove additional characters after extension in linux

If you want to rename the existing files, please try:

#!/bin/bash

for f in *mp4$'\r'; do
mv -- "$f" "${f%$'\r'}"
done

It assumes the *mp4 files are located in the same directory. If you want to fix the filenames in the current directory recursively, please try:

#!/bin/bash

shopt -s globstar
for f in **/*mp4$'\r'; do
mv -- "$f" "${f%$'\r'}"
done

BTW if you want to fix the filenames downloading from now on, modify your awk command as:

awk '{url=$1; $1="";{sub(/ /,""); sub(/\r$/, "");out=url" -O \""$0"\""}; print out}' file.txt | xargs -L 1 wget

It removes the trailing CR characters out of the names in file.txt then passes the corrected filenames to wget.

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