Questions tagged [basename]

For questions pertaining to basename, a utility that returns the non-directory portion of a pathname.

Returns non-directory portion of a pathname.

External reference

basename specification (The Open Group Base Specifications Issue 7, 2018 edition)

39 questions
31
votes
4 answers

dirname and basename vs parameter expansion

Is there any objective reason to prefer one form to the other? Performance, reliability, portability? filename=/some/long/path/to/a_file parentdir_v1="${filename%/*}" parentdir_v2="$(dirname…
Wildcard
  • 35,316
  • 26
  • 130
  • 258
28
votes
3 answers

shellcheck is advising not to use basename: why?

I am trying out shellcheck. I have something like that basename "${OPENSSL}" and I get the following suggestion Use parameter expansion instead, such as ${var##*/}. From the practical point of view I see no difference $ export…
Matteo
  • 9,676
  • 4
  • 49
  • 66
14
votes
4 answers

use basename in find -exec?

If I just use basename {} .txt, it will work: find . -iname "*.txt" -exec basename {} .txt \; It will just print xxx instead of ./xxx.txt if I want use $(basename {} .txt) in the -exec option, it will fail: find . -iname "*.txt" -exec echo…
Wang
  • 1,212
  • 2
  • 15
  • 26
14
votes
6 answers

Use basename to parse a list of paths held in a file

I'm running Mac OSX and trying to use the command line to find the number of files I have with the same name. I tried to use the following command: find ~ -type f -name "*" -print | basename | sort | uniq -d > duplicate_files It doesn't work! When…
JohnB
  • 153
  • 1
  • 1
  • 6
6
votes
3 answers

How do you call the "happy-dog" part of file "happy-dog.png"?

I just realized I don't know how file is called in file.ext. The whole file.ext is called a file or filename, ext is called extension but how do you call the file part itself of file.ext? For example happy-dog.png. All the file/filename is…
bodacydo
  • 312
  • 1
  • 3
  • 10
6
votes
2 answers

Remove multiple possible suffixes

Example: I want to create a quick command to shrink images (for my filebrowser). ... FN="/tmp/some-image.jpg" gm convert "$FN" -resize 50% "$(dirname $FN)/$(basename $FN .jpg/png/gif).jpg" ... How can I specify that more than one suffix should be…
Profpatsch
  • 1,749
  • 4
  • 20
  • 23
4
votes
1 answer

the command find not working with -name option in sh file

I am using the following command to retrieve the number of files which names contains sv or json in a given directory in a remote server: nbs_files=`ssh -q -i ${sshkey} ${user}@${server} "find ${path}/ -maxdepth 1 -mindepth 1 -type f -name '*sv*' -o…
rainman
  • 149
  • 1
  • 5
4
votes
1 answer

How to get the filename of a symlink destination in a shell script?

let's say the scriptname is myscript. It's a symbolic link to thescript_1.91.sh Is there a way to get the name of the link destination? An example: I want to write in a logfile cat <
Michael
  • 41
  • 1
  • 3
4
votes
5 answers

Execute on the basename of a find command

Suppose I have a directory structure as follows test test/a test/b Now I want to execute a command, such that in the . folder I can execute a command on the basename of the files a and b. So basically, I want something like this, which I naively…
Bernhard
  • 11,992
  • 4
  • 59
  • 69
3
votes
2 answers

Bash shell scripting basic question regarding the syntax and basename

Consider the script below: myname=`basename $0`; for i in `ls -A` do if [ $i = $myname ] then echo "Sorry i won't rename myself" else newname=`echo $i |tr a-z A-Z` mv $i $newname fi done 1) I am aware that basename $0 signifies my script…
Gokul
  • 1,061
  • 5
  • 16
  • 31
3
votes
3 answers

find -exec command options with basename

I have the following JPEG files : $ ls -l -rw-r--r-- 1 user group 384065 janv. 21 12:10 CamScanner 01-10-2022 14.54.jpg -rw-r--r-- 1 user group 200892 janv. 10 14:55 CamScanner 01-10-2022 14.55.jpg -rw-r--r-- 1 user group 283821 janv. 21 12:10…
ChennyStar
  • 1,319
  • 9
  • 22
3
votes
2 answers

How can I use basename with parallel?

I have files like this on a Linux system: 10S1_S5_L002_chrm.fasta SRR3184711_chrm.fasta SRR3987378_chrm.fasta SRR4029368_chrm.fasta SRR5204465_chrm.fasta SRR5997546_chrm.fasta 13_S7_L003_chrm.fasta SRR3184712_chrm.fasta …
Fraizu
  • 55
  • 5
3
votes
1 answer

basename "$0" not working

I have this command: base_name="$(basename "$0")"; and I am getting this error: basename: illegal option -- b usage: basename string [suffix] basename [-a] [-s suffix] string [...] anyone know what's going on with that?
Alexander Mills
  • 9,330
  • 19
  • 95
  • 180
3
votes
1 answer

basename extra operand error

I have a script that will try to extract the file's base name and then I do additional work with it. Only when using this script with a file with this naming convention (including spaces and characters - not really sure what's triggering the error),…
ksa_coder
  • 153
  • 1
  • 1
  • 5
3
votes
2 answers

Concatenate pwd and basename

I want to assign the path and file name to a variable: /path/to/myfile/file.txt For example MYFILE=$(pwd)$(basename) How can i do it ?
Omar BISTAMI
  • 551
  • 2
  • 7
  • 14
1
2 3