I am trying to organize a large group of files for use with another program that can run them archived but only if they are named for example "My Document.7z" and not "My Document.txt.7z". The best solution I could come up with as linux doesn't have a variable for just grabbing the file name without the extension (%%~ni in Batch) was to just use regex to delete the last 4 characters (".txt" in this example) of the file name and then assign that variable to the title of the archive name creation command. I'm aware that this breaks files that have 2 and 4 letter extensions like .sh and .jpeg but I've been stumped for so long I'm honestly beyond caring at this point.
The code I've come up with so far is roughly:
#!/bin/bash
shopt -s extglob
for f in *.* ; do
f$ !(*.sh)
g=$($f-4) #placeholder code for delete last 4 characters of filename syntax
7z a $g.7z $f -sdel
done
read -p "pause"
exit
Try:
#!/bin/bash
for f in *.* ; do
if [[ $f == *.sh ]] || ! [[ -f $f ]]; then
continue
fi
g=${f%.*}
7z a "$g.7z" "$f" -sdel
done
read -p "pause"
exit
Notes:
a.b.c -> a
) replace g=${f%.*}
with g=${f%%.*}
.a.txt
and a.exe
will both become a
. It would probably be safer to replace dots with, e.g., underscores (a.txt -> a_txt
). To do this replace g=${f%.*}
with g=${f/./_}
(replace last dot only) or g=${f//./_}
(replace all dots).