How to Change File Extension in Linux Shell Script

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.

Bash script to change file names of different extension

Here is a relatively simple command to do it:

find ./src -type f \( -name \*.js -o -name \*.css -o -name \*.woff \) -print0 | 
while IFS= read -r -d $'\0' line; do
dest="./dst/$(echo $(basename $line) | sed -E 's/(\..{20}\.)(js|css|woff)/\.\2/g')"
echo Copying $line to $dest
cp $line $dest
done

How can I remove the extension of a filename in a shell script?

You should be using the command substitution syntax $(command) when you want to execute a command in script/command.

So your line would be

name=$(echo "$filename" | cut -f 1 -d '.')

Code explanation:

  1. echo get the value of the variable $filename and send it to standard output
  2. We then grab the output and pipe it to the cut command
  3. The cut will use the . as delimiter (also known as separator) for cutting the string into segments and by -f we select which segment we want to have in output
  4. Then the $() command substitution will get the output and return its value
  5. The returned value will be assigned to the variable named name

Note that this gives the portion of the variable up to the first period .:

$ filename=hello.world
$ echo "$filename" | cut -f 1 -d '.'
hello
$ filename=hello.hello.hello
$ echo "$filename" | cut -f 1 -d '.'
hello
$ filename=hello
$ echo "$filename" | cut -f 1 -d '.'
hello

File name manipulation with shell scripts: changing file extensions

This should work for you:

#!/bin/bash

for file in *.png
do
filename=$(basename "$file")
filename=${filename%.*}
convert $file $filename.tif
done

A line-by-line walkthrough of how this works:

  • for file in *.png - you don't need command substitution ls *.png to get the list of files with png extension. The wildcard * will auto-expand in shell to match the list of files in cwd; and in this case the list of files ending in .png.
  • filename=$(basename "$file") - this is only for defensive programming; it gets the actual name of the file
  • filename=${filename%.*} - this removes the extension from filename
  • convert $file $filename.tif - runs your actual convert command

How to change file extension in Linux shell script?

If you're using bash

${f%%.mp4}

will give the filename without the .mp4 extension.

Try using it like this:

for f in *.mp4; do
ffmpeg -i "$f" -f mp3 -ab 192000 -vn "mp3s/${f%%.mp4}.mp3"
done

... and don't forget the do keyword as in the example given.

Explanation

The bash Manual(man bash) states:

${parameter%word} ${parameter%%word}

Remove matching suffix pattern.
The word is expanded to produce
a pattern just as in pathname expansion. If the pattern matches
a trailing portion of the expanded value of parameter, then the
result of the expansion is the expanded value of parameter with
the shortest matching pattern (the %'' case) or the longest
matching pattern (the
%%'' case) deleted. If parameter is @
or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant
list. If parameter is an array variable subscripted with @ or
*, the pattern removal operation is applied to each member of
the array in turn, and the expansion is the resultant list.

This is just one of many string manipulations you can perform on shell variables. They all go by the name of Parameter Expansion.

That's as well the section label given in the bash manual. Thus man bash /paramter exp should bring you there fast.
`

How can I change the extension of files of a type using find with Bash?

Combining the find -exec bash idea with the bash loop idea, you can use the + terminator on the -exec to tell find to pass multiple filenames to a single invocation of the bash command. Pass the new type as the first argument - which shows up in $0 and so is conveniently skipped by a for loop over the rest of the command-line arguments - and you have a pretty efficient solution:

find . -type f -iname "*.$arg1" -exec bash -c \
'for arg; do mv "$arg" "${arg%.*}.$0"; done' "$arg2" {} +

Alternatively, if you have either version of the Linux rename command, you can use that. The Perl one (a.k.a. prename, installed by default on Ubuntu and other Debian-based distributions; also available for OS X from Homebrew via brew install rename) can be used like this:

find . -type f -iname "*.$arg1" -exec rename 's/\Q'"$arg1"'\E$/'"$arg2"'/' {} +

That looks a bit ugly, but it's really just the s/old/new/ substitution command familiar from many UNIX tools. The \Q and \E around $arg1 keep any weird characters inside the suffix from being interpreted as regular expression metacharacters that might match something unexpected; the $ after the \E makes sure the pattern only matches at the end of the filename.

The pattern-based version installed by default on Red Hat-based Linux distros (Fedora, CentOS, etc) is simpler:

find . -type f -iname "*.$arg1" -exec rename ".$arg1" ".$arg2" {} +

but it's also dumber: if you rename .com .exe stackoverflow.com_scanner.com, you'll get a file named stackoverflow.exe_scanner.exe.

Recursively change file extensions in Bash

Use:

find . -name "*.t1" -exec bash -c 'mv "$1" "${1%.t1}".t2' - '{}' +

If you have rename available then use one of these:

find . -name '*.t1' -exec rename .t1 .t2 {} +
find . -name "*.t1" -exec rename 's/\.t1$/.t2/' '{}' +


Related Topics



Leave a reply



Submit