Uses for this bash filename extraction technique?
It gets rid of the filename extension (here: .tif
), sample:
$ for A in test.py test.sh test.xml test.xsl; do echo "$A: ${A%%.*}"; done
test.py: test
test.sh: test
test.xml: test
test.xsl: test
from bash manual:
${parameter%%word}
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.
Extract filename and extension in Bash
First, get file name without the path:
filename=$(basename -- "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"
Alternatively, you can focus on the last '/' of the path instead of the '.' which should work even if you have unpredictable file extensions:
filename="${fullfile##*/}"
You may want to check the documentation :
- On the web at section "3.5.3 Shell Parameter Expansion"
- In the bash manpage at section called "Parameter Expansion"
Extract filename and extension in Bash
First, get file name without the path:
filename=$(basename -- "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"
Alternatively, you can focus on the last '/' of the path instead of the '.' which should work even if you have unpredictable file extensions:
filename="${fullfile##*/}"
You may want to check the documentation :
- On the web at section "3.5.3 Shell Parameter Expansion"
- In the bash manpage at section called "Parameter Expansion"
how to extract a part of file name in unix scripting and check if it exists
Try:
FILE="abc_def_xxx_yyy_20210515.txt"
DLS="${FILE%_*}.txt"
DLL="$HOME/$DLS"
if [ -f "$DLL"]; then
echo "$DLL exists"
else
touch "$DLL"
fi
Extract directory path and filename
Use the basename command to extract the filename from the path:
[/tmp]$ export fspec=/exp/home1/abc.txt
[/tmp]$ fname=`basename $fspec`
[/tmp]$ echo $fname
abc.txt
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
Difference Between "Machine Hardware" and "Hardware Platform"
Nm Reports Symbol Is Defined But Ldd Reports Symbol Is Undefined
Graphics Card Memory and Virtual Address Space of a Process
Checking If a Screen of The Specified Name Exists
Copy File Permissions, But Not Files
Crontab - Simple Echo Not Running
The Repository 'Http://Dl.Google.Com/Linux/Chrome/Deb Stable Release' Is Not Signed
Differences Between Arm "Versions" (Armv7 Only)
Stdin into Zip Command, How to Specify a File Name
Find Which Program Caused a Core Dump File
How to Get Command History by Cursor Key in Linux Tclsh
Linux Append Console Output to a Logfile