Basename - doesnt match man page ? (bash)

leftyleo

New Member
Joined
Aug 6, 2024
Messages
17
Reaction score
5
Credits
142
On the basename man page it has two syntax uses. Only the second version works for me

basename NAME [SUFFIX]
basename OPTION... NAME...

version one:
basename -s datafile.pdf
doesnt work but
basename datafile.pdf .pdf
-does work

as does the necessity of using the back ticks in the script to exec the command
OUTFILE=basename ${INFILE} .pdf

experimented both direct command line and in a script and some web searching to get it right
 


version one:
basename -s datafile.pdf
That's not how first variant is supposed to be used, you're providing option but first version expect none.

First version should be:
Code:
basename datafile.pdf .pdf

Second version is:
Code:
basename -s .pdf datafile.pdf
 
i see i have them all mixed up in my brain.
The first version the way you have it make perfect sense and i follow that
but the usage says :
Code:
basename NAME [SUFFIX]
doesnt the [ ] mean optional ?
what if you dont know or dont care about the suffix ?

I re-read then man page and then followed the address to gnu.org for more info
and it it clearer
-- it seems that it is more for stripping the path (leading) rather than for removing extensions.
The examples in the doc lead me to believe you have to know the extension to remove it
Is that because you can have several "." .. like .pdf.gz or .config.bak - so you have to specify the suffix

This is so interesting ..
thank you for your help
 
what if you dont know or dont care about the suffix ?
If one doesn't know or care what the suffixes are, they can be deleted using the cut command, for example:

Code:
[tom@min ~]$ ls
file1.jpg  file1.pdf  file3.png  file4.text  file5.xpm

[tom@min ~]$ for i in $(ls) ;do mv $i $(echo $i|cut -d. -f1); done

[tom@min ~]$ ls
file1  file2  file3  file4  file5

There are dangers here however, for example if files are only differentiated by the suffix: file.txt and file.png, would both be named "file" after deleting the suffix, but only the last one processed would survive.
 
Last edited:
it took me a while to figure this out. I had to compare your post (scratching my head over the
Code:
-d.
where i expected
Code:
-d .

This works as you said :
Bash:
ls | cut -d. -f1

Perhaps i need to get book on grep / sed / awk - the gnu.org page mentions sed to do this.
Bash:
ls | awk -F . '{print $1}'

--and yes i 100% understand about files overwriting. if you have file.txt and file.sh and remove the .txt and the .sh , whichever file is processed last is the "file" you will be left with
 

Members online


Top