Questions tagged [basename]

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

Filter by
Sorted by
Tagged with
0 votes
1 answer
59 views

Basename detects an extra operand if I use it with find

I try to execute basename via find like this: find ./test_folder -type f -exec basename {} + But I get the following error: basename: extra operand './test_folder/test/file.crt' Why I get this ...
Dimitrios Desyllas's user avatar
0 votes
2 answers
63 views

Basename in find -exec

I need use in find results full path and dir name. This not work: find ./1cv8 -maxdepth 1 -type d -wholename "./1cv8/*" -exec bash -c 'echo vrunner -src "{}" -o ./builds/"${...
Dmitriy's user avatar
0 votes
1 answer
165 views

Why basename don't work here [duplicate]

Why when I do this (I know, it's stupid) it returns the full path, not only the filename as expected? ls -l | awk '{print $9}' | xargs -I% find ./my_dir -type f -name "%" -exec echo $(...
Alysko's user avatar
  • 103
2 votes
1 answer
305 views

Why do I need to use the -I parameter in `compgen -G ... | xargs basename`?

I ran into a situation where I was piping the output of compgen -G to xargs basename and could not get it to work until I added the xargs -I parameter as seen below. Here is a script demonstrating ...
chris's user avatar
  • 123
3 votes
3 answers
987 views

find -exec command options with basename [duplicate]

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 -...
ChennyStar's user avatar
  • 1,497
0 votes
4 answers
563 views

How can I output a txt file, that has part of filename followed by path of file for all files in a folder, one line per file?

I have the following files /folder/abc1.txt.gz /folder/abc2.txt.gz /folder/abc3.txt.gz I would like to make a txt file with the following abc1 /folder/abc1.txt.gz abc2 /folder/abc2.txt.gz abc3 /...
christoforos giatzakis's user avatar
0 votes
2 answers
197 views

Map over file tree, preserving structure

I have a file tree that looks like: $ tree src src ├── bible │   ├── index.md │   └── README.md ├── index.md └── other.md I want to render every Markdown file within this file tree to HTML via pandoc(...
jmcph4's user avatar
  • 75
3 votes
2 answers
469 views

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 ...
Fraizu's user avatar
  • 55
1 vote
1 answer
477 views

Bash: removing several patterns from file names

Normally, looping over the files I use basename to extract the name of the file as a separate variable: # remove .pdb extension from filename in new variable and print name of the file without it for ...
user3470313's user avatar
4 votes
1 answer
391 views

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

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 ${...
rainman's user avatar
  • 149
0 votes
3 answers
222 views

Filename from pathname excluding (unknown) extension

Having a pathname it is possible to extract its filename, excluding its apriori known extension, with basename: $ pathname="/home/paulo/paulo.pdf" $ printf "%s\n" "$(basename $pathname .pdf)" paulo ...
Paulo Tomé's user avatar
  • 3,752
0 votes
1 answer
126 views

basename execution on remote node

ssh ubuntu@$ip -n "aws s3 cp s3://bucket/$userlistlocation . --region eu-central-1 ; fbname=$(basename '$userlistlocation') ; echo "$fbname"" This above command is part of jenkins job. ...
rahuls36's user avatar
3 votes
1 answer
1k views

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 [...]...
Alexander Mills's user avatar
0 votes
2 answers
127 views

only use the filename for move

I have to move some files from one folder to another, criteria set by a find and grep. When I try to move the files it breaks. The actual move it's trying is mv /somefolder/export//somefolder/storage/...
Joris D R.'s user avatar
0 votes
2 answers
359 views

here-document basename conflicts?

I am using EOF to generate bash scripts that run the Rscripts. In the Rscript I used basename to specify the output file name. When I use EOF to generate a list of bash scripts, I could not get ...
Molly_K's user avatar
  • 161
1 vote
3 answers
2k views

How can I use an argument from xargs to evaluate another expression?

I would like to evaluate a basename expression given an argument from xargs. I tested: find . -name '*.txt' | xargs -I f cp f DIR_OUT/copied_$(basename f) which gives no file or directory because $(...
Change-the-world's user avatar
0 votes
4 answers
146 views

How to debug my script?

This script name is hello for i in $(ls $*);do x=$(echo $(basename $i".md")); pandoc $i -t "latex" -o $x.pdf; done When I write this $ ./hello *bye.md This error ls: cannot access 'test': ...
ZPUFF19's user avatar
  • 33
3 votes
1 answer
16k views

basename extra operand error [duplicate]

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 ...
ksa_coder's user avatar
  • 153
1 vote
2 answers
123 views

Get container directory more easily

I have a bash script here: $GOPATH/ src/ build.sh and in build.sh I have: export GOPATH="$(cd $(dirname "$BASH_SOURCE") && pwd)" is there a shorter way to get the containing dir ...
Alexander Mills's user avatar
1 vote
1 answer
2k views

Using bash, resolve absolute path to relative path

Say I have this simple bash script: #!/usr/bin/env bash file="$1"; if [ -z "$file" ]; then echo "Must pass relative file path as the first argument."; fi git_root=`git rev-parse --show-...
Alexander Mills's user avatar
0 votes
2 answers
2k views

How to get the filename part of a path from a shell variable

A shell variable holds a path. How does one get its filename portion? In bash(1), I experimented and found that I can do it with ."${i/*\///}" where i is the name of the environment variable. Such a ...
Tankman六四's user avatar
0 votes
2 answers
2k views

Find with absolute path inside a bashscript | xargs basename

I have to find recursively inside a bash script. Since I can invoke the bash script from anywhere, I am forced to use the absolute path. Sometimes the absolute path can get very long, and the output ...
Porcupine's user avatar
  • 1,792
4 votes
1 answer
7k views

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 <<EOF ####...
Michael's user avatar
  • 41
1 vote
2 answers
102 views

Sorting out directories by searching certain file extension

Let say, I am in a directory A, under A there are many folders (B, C, D, etc.), and in each folder there is a file "*.out" and sub-folders. I want to run a script from A which will look for the text "...
Akand's user avatar
  • 69
0 votes
2 answers
433 views

Bash for file processing [duplicate]

I am writing a bash script to move all images into a central file. I create the list of image files with: img_fil='/home/files/img_dump.txt' locate -i image | grep \.jpg > "$img_fil" locate -i ...
OldManRiver's user avatar
3 votes
2 answers
12k views

Concatenate pwd and basename [closed]

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's user avatar
0 votes
2 answers
258 views

can't get an "awk for loop" to work, while using basename

I have many xyz files that I want to run through awk. I just want to create a new file with an "_s" appended, for each one. Having a little trouble getting the basename syntax correct. Here is what I ...
lbeazy's user avatar
  • 63
31 votes
4 answers
37k views

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's user avatar
  • 36.1k
6 votes
3 answers
545 views

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 ...
bodacydo's user avatar
  • 322
2 votes
3 answers
682 views

Rename mp3 file names to a pattern

I have a list of like names likes so: /A/B/C/D/E/F/filename_album1-003.mp3 its more like album\d+ I would like to change it to something like /A/B/C/D/E/F/filename_song-3.mp3 song is just a ...
Sten Kin's user avatar
  • 391
2 votes
1 answer
974 views

basename doesn't work as expected

How do I make ".mkv" disappear in the target filename? $ echo a/b.mkv | xargs -I % echo ffmpeg -i "%" $(basename "%" ".mkv").mp4 ffmpeg -i a/b.mkv a/b.mkv.mp4
forthrin's user avatar
  • 2,249
14 votes
4 answers
36k views

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 ...
Wang's user avatar
  • 1,244
1 vote
1 answer
2k views

Extract file name from find command

I am trying to extract the file name and attaching to mail. But when I send mail the attachment is coming with path name and file name. Ex: dir=/home/as123/bill and filename=abc.pdf.Z and I am ...
Aravind's user avatar
  • 1,569
2 votes
2 answers
8k views

Problems with basename in a loop

I am new at shell script programming and I'm trying to execute a software that reads a text and perform it's POS tagging. It requires an input and an output, as can be seen in the execute example: $ ...
Rafael's user avatar
  • 23
8 votes
2 answers
8k views

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 ...
Profpatsch's user avatar
  • 1,789
14 votes
6 answers
28k views

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 ...
JohnB's user avatar
  • 153
1 vote
1 answer
16k views

Basename gives extra operand error

I 'm using the following script to merge few pcap files into one using mergecap command. But when I run it it gives me a 'basename: extra operand /mnt/md0/capture/DCN/dcn_2014_02_04_00_11_47_598.pcap' ...
Jishnu U Nair's user avatar
29 votes
3 answers
2k views

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 ...
Matteo's user avatar
  • 9,726
3 votes
2 answers
26k views

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)...
Gokul's user avatar
  • 1,061
2 votes
2 answers
2k views

Inner function call with xargs parameters

I am trying to create a file occurent within my /tmp directory of each file containing a speicific string. The problem is that the call to basename {} does not seem to work. Neither this, neither ...
Spredzy's user avatar
  • 473
4 votes
5 answers
9k views

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 ...
Bernhard's user avatar
  • 12.1k