Linux: remove file extensions for multiple files
rename
is slightly dangerous, since according to its manual page:
rename will rename the specified files by replacing the first occurrence of...
It will happily do the wrong thing with filenames like c.txt.parser.y
.
Here's a solution using find
and bash
:
find -type f -name '*.txt' | while read f; do mv "$f" "${f%.txt}"; done
Keep in mind that this will break if a filename contains a newline (rare, but not impossible).
If you have GNU find, this is a more solid solution:
find -type f -name '*.txt' -print0 | while read -d $'\0' f; do mv "$f" "${f%.txt}"; done
Linux : Copy Multiple files and remove it's extension
Here is a simple bash script. This script assumes that the file name only contains one "." character and splits based on that.
#!/bin/sh
for f in *.cpp; do
#This line splits the file name on the delimiter "."
baseName=`echo $f | cut -d "." -f 1`
newExtension=".new"
cp $f $baseName$newExtension
done
How to recursively delete multiple files with different extensions?
find . \( -name "*.extension1" -o -name "*.extension2" \) -type f -delete
find Documents ( -name ".py" -o -name ".html" ) -exec file {} \;
OR
find . -regextype posix-egrep -regex ".*\.(extension1|extension2)$" -type f -delete
How to remove file extension using linux and store first the filename
Let's create some sample files:
$ for i in {1..10}; do touch "$RANDOM.txt"; done
$ ls
15158.txt 15964.txt 17123.txt 21123.txt 22209.txt 29456.txt 29826.txt 4168.txt 4287.txt 6787.txt
Now, store the filenames in an array, build up the expression as a string, and send it to a single bc
invocation:
files=(*.txt)
expr="(0"
for f in "${files[@]}"; do expr+=" + ${f%.txt}"; done
expr+=") / ${#files[@]}"
echo "$expr"
echo "scale=3; $expr" | bc
outputs
(0 + 15158 + 15964 + 17123 + 21123 + 22209 + 29456 + 29826 + 4168 + 4287 + 6787) / 10
16610.100
More tersely:
join() { local IFS=$1; shift; echo "$*"; }
files=(*.txt)
printf -v expr "(%s)/%d" "$(join + "${files[@]%.txt}")" ${#files[@]}
echo "$expr"
echo "scale=3; $expr" | bc
(15158+15964+17123+21123+22209+29456+29826+4168+4287+6787)/10
16610.100
How to remove the extension of a file?
To remove a string from the end of a BASH variable, use the ${var%ending}
syntax. It's one of a number of string manipulations available to you in BASH.
Use it like this:
# Run in the same directory as the files
for FILENAME in *.bak; do mv "$FILENAME" "${FILENAME%.bak}"; done
That works nicely as a one-liner, but you could also wrap it as a script to work in an arbitrary directory:
# If we're passed a parameter, cd into that directory. Otherwise, do nothing.
if [ -n "$1" ]; then
cd "$1"
fi
for FILENAME in *.bak; do mv "$FILENAME" "${FILENAME%.bak}"; done
Note that while quoting your variables is almost always a good practice, the for FILENAME in *.bak
is still dangerous if any of your filenames might contain spaces. Read David W.'s answer for a more-robust solution, and this document for alternative solutions.
Remove multiple extensions from all files in a directory (Bash)
This may help you
#!/bin/bash
for filename in *
do
x=`echo $filename | sed 's/\..*\./\./g'`
mv $filename $x
done
Save this to a file called rename.sh
chmod +x rename.sh
./rename.sh
Batch remove former of two file extensions
Modify your mv
command like this :
#!/bin/bash
for file in /folder/*.JPG.gif
do
mv "$file" "${file/\.JPG}"
done
Your initial code uses an expansion that removes text from the beginning, not in the middle. The expansion above removes inside the string.
Please note that this is not very robust. If you have ".JPG" in your path or filenames anywhere except at the end of your filenames, it will not do what you want. Quoting, even if not necessary in your case for now, is still good practice as things change, and code gets copy and pasted.
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:
echo
get the value of the variable$filename
and send it to standard output- We then grab the output and pipe it to the
cut
command - 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 - Then the
$()
command substitution will get the output and return its value - 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
Related Topics
Find All Files with Name Containing String
How to Find the Maximum Stack Size
Git Status Ignore Line Endings/Identical Files/Windows & Linux Environment/Dropbox/Meld
Where Are Include Files Stored - Ubuntu Linux, Gcc
Truncating a File While It's Being Used (Linux)
Problem with Bash Output Redirection
How to Run a Cron Job Inside a Docker Container
Cannot Connect to X Server :0.0 with a Qt Application
What Is the Correct Way to Start a Mongod Service on Linux/Os X
Get Ceiling Integer from Number in Linux (Bash)
How to Do Memory Test on Arm Architecture Hardware? (Something Like Memtest86)
How to Edit /Etc/Sudoers from a Script
Converting Jiffies to Milli Seconds
How to Discover What Linux Distribution Is in Use