bash script to crop file names to the first part of the name before a space
by Pedroski from LinuxQuestions.org on (#6CJS2)
With kind help from LQ, quite a while ago, I have this script to remove spaces from file names before processing them from .wma to .mp3 with ffmpeg. Works perfectly! Thank you!
Code:find . -type f -name "* *.*" -exec bash -c 'mv -v "$0" "${0// /_}"' {} \;Now the file names have the format: YYYY-MM-DD HH.MM.SS.* (could be any ending)
I would like to alter this to:
A: crop the name to just the part of the name before the space, which is a year.
B: check if that file name exists already (Likely because it is just a year.)
C: if the name already exists, add a number to the new name before saving.
Any tips please?
I can get the file name and extension like this, but I don't know how to crop the file name on the first space.
Code:getPath="/home/pedro/temp/"
cd $getPath
files=$(ls)
for f in $files;
do
# Ensure it is a file
[ -f "$f" ] || continue
echo "file is "$f;
filename=${f%.*}
echo "first part of the file is "$filename;
extension=${f##*.}
echo "The extension is "$extension;
done
Code:find . -type f -name "* *.*" -exec bash -c 'mv -v "$0" "${0// /_}"' {} \;Now the file names have the format: YYYY-MM-DD HH.MM.SS.* (could be any ending)
I would like to alter this to:
A: crop the name to just the part of the name before the space, which is a year.
B: check if that file name exists already (Likely because it is just a year.)
C: if the name already exists, add a number to the new name before saving.
Any tips please?
I can get the file name and extension like this, but I don't know how to crop the file name on the first space.
Code:getPath="/home/pedro/temp/"
cd $getPath
files=$(ls)
for f in $files;
do
# Ensure it is a file
[ -f "$f" ] || continue
echo "file is "$f;
filename=${f%.*}
echo "first part of the file is "$filename;
extension=${f##*.}
echo "The extension is "$extension;
done