ls ignore “no matches”

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite












I would like to list all of the files in a folder called foldername that have the extension test, atest or btest.



My immediate thought was to run ls ./foldername/*.a,b,test



This works fine unless there is nothing with the extension atest, in which case I get the error zsh: no matches found: ./foldername/*.atest.



Is there any way I can simply ignore this error and print the files that do exist?



I need this to work in both ZSH and BASH.







share|improve this question


















  • 3




    Redirect standard error to /dev/null?
    – DopeGhoti
    Nov 28 '17 at 16:48














up vote
5
down vote

favorite












I would like to list all of the files in a folder called foldername that have the extension test, atest or btest.



My immediate thought was to run ls ./foldername/*.a,b,test



This works fine unless there is nothing with the extension atest, in which case I get the error zsh: no matches found: ./foldername/*.atest.



Is there any way I can simply ignore this error and print the files that do exist?



I need this to work in both ZSH and BASH.







share|improve this question


















  • 3




    Redirect standard error to /dev/null?
    – DopeGhoti
    Nov 28 '17 at 16:48












up vote
5
down vote

favorite









up vote
5
down vote

favorite











I would like to list all of the files in a folder called foldername that have the extension test, atest or btest.



My immediate thought was to run ls ./foldername/*.a,b,test



This works fine unless there is nothing with the extension atest, in which case I get the error zsh: no matches found: ./foldername/*.atest.



Is there any way I can simply ignore this error and print the files that do exist?



I need this to work in both ZSH and BASH.







share|improve this question














I would like to list all of the files in a folder called foldername that have the extension test, atest or btest.



My immediate thought was to run ls ./foldername/*.a,b,test



This works fine unless there is nothing with the extension atest, in which case I get the error zsh: no matches found: ./foldername/*.atest.



Is there any way I can simply ignore this error and print the files that do exist?



I need this to work in both ZSH and BASH.









share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '17 at 20:33









Gilles

507k12010031531




507k12010031531










asked Nov 28 '17 at 16:44









Jonathan Hodgson

9212




9212







  • 3




    Redirect standard error to /dev/null?
    – DopeGhoti
    Nov 28 '17 at 16:48












  • 3




    Redirect standard error to /dev/null?
    – DopeGhoti
    Nov 28 '17 at 16:48







3




3




Redirect standard error to /dev/null?
– DopeGhoti
Nov 28 '17 at 16:48




Redirect standard error to /dev/null?
– DopeGhoti
Nov 28 '17 at 16:48










2 Answers
2






active

oldest

votes

















up vote
5
down vote



accepted










It may be best to do this with find:



find ./foldername -maxdepth 1 -name '*.atest' -o -name '*.btest' -o -name '*.test'





share|improve this answer


















  • 1




    perhaps with a -maxdepth 1, to closer emulate the ls behavior
    – Jeff Schaller
    Nov 28 '17 at 17:29










  • Agreed, I'll add that. Thanks!
    – John Moon
    Nov 28 '17 at 17:30






  • 4




    @JeffSchaller, note that -maxdepth is a GNU extension. Note 3 other differences with globs: find would include hidden files, not sort the list and fail to match file names that contain bytes not forming valid characters (like a $'Stxe9phane.atest' in a UTF-8 locale)
    – Stéphane Chazelas
    Nov 28 '17 at 17:32

















up vote
11
down vote















In



ls -d ./foldername/*.a,b,test


a,b,... is not a glob operator, that's brace expansion, that's first expanded to:



ls -d ./foldername/*.atest ./foldername/*.btest ./foldername/*.test


And each glob expanded individually, and if any glob doesn't match, the command is cancelled as you'd expect in zsh (or fish; in bash, you need the failglob option to get a similar behaviour).



Here, you'd want to use a single glob that matches all those files, and only cancel the command if that one glob didn't match any file:



ls -d ./foldername/*.(a|b|)test


You don't want to use nullglob, as if none of the globs matched, it would run ls without arguments, so list the current directory. cshnullglob is better in that regard as it removes non-matching globs but still cancels the command if all the globs fail to match.



You wouldn't want to use nonomatch, as that would give you the broken behaviour of bash which would be a shame.



For a glob alternative that works in both zsh and bash, you could use the ksh globs (set -o kshglob in zsh and shopt -s extglob in bash).



Then, you'd do:



ls -d ./foldername/*.@(a|b|)test


or:



ls -d ./foldername/*.?([ab])test


Add the failglob option in bash to avoid the glob being passed literally to ls when it doesn't match.



See Why is nullglob not default? for more information.






share|improve this answer






















  • I'm not sure why it's a requirement of the OP, but is there an extglob that works for this in both bash and zsh? I had to use ?(...) or +(...) for bash.
    – Jeff Schaller
    Nov 28 '17 at 17:28










  • @JeffSchaller, see edit
    – Stéphane Chazelas
    Nov 28 '17 at 17:33










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407532%2fls-ignore-no-matches%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
5
down vote



accepted










It may be best to do this with find:



find ./foldername -maxdepth 1 -name '*.atest' -o -name '*.btest' -o -name '*.test'





share|improve this answer


















  • 1




    perhaps with a -maxdepth 1, to closer emulate the ls behavior
    – Jeff Schaller
    Nov 28 '17 at 17:29










  • Agreed, I'll add that. Thanks!
    – John Moon
    Nov 28 '17 at 17:30






  • 4




    @JeffSchaller, note that -maxdepth is a GNU extension. Note 3 other differences with globs: find would include hidden files, not sort the list and fail to match file names that contain bytes not forming valid characters (like a $'Stxe9phane.atest' in a UTF-8 locale)
    – Stéphane Chazelas
    Nov 28 '17 at 17:32














up vote
5
down vote



accepted










It may be best to do this with find:



find ./foldername -maxdepth 1 -name '*.atest' -o -name '*.btest' -o -name '*.test'





share|improve this answer


















  • 1




    perhaps with a -maxdepth 1, to closer emulate the ls behavior
    – Jeff Schaller
    Nov 28 '17 at 17:29










  • Agreed, I'll add that. Thanks!
    – John Moon
    Nov 28 '17 at 17:30






  • 4




    @JeffSchaller, note that -maxdepth is a GNU extension. Note 3 other differences with globs: find would include hidden files, not sort the list and fail to match file names that contain bytes not forming valid characters (like a $'Stxe9phane.atest' in a UTF-8 locale)
    – Stéphane Chazelas
    Nov 28 '17 at 17:32












up vote
5
down vote



accepted







up vote
5
down vote



accepted






It may be best to do this with find:



find ./foldername -maxdepth 1 -name '*.atest' -o -name '*.btest' -o -name '*.test'





share|improve this answer














It may be best to do this with find:



find ./foldername -maxdepth 1 -name '*.atest' -o -name '*.btest' -o -name '*.test'






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 '17 at 17:31

























answered Nov 28 '17 at 17:13









John Moon

49026




49026







  • 1




    perhaps with a -maxdepth 1, to closer emulate the ls behavior
    – Jeff Schaller
    Nov 28 '17 at 17:29










  • Agreed, I'll add that. Thanks!
    – John Moon
    Nov 28 '17 at 17:30






  • 4




    @JeffSchaller, note that -maxdepth is a GNU extension. Note 3 other differences with globs: find would include hidden files, not sort the list and fail to match file names that contain bytes not forming valid characters (like a $'Stxe9phane.atest' in a UTF-8 locale)
    – Stéphane Chazelas
    Nov 28 '17 at 17:32












  • 1




    perhaps with a -maxdepth 1, to closer emulate the ls behavior
    – Jeff Schaller
    Nov 28 '17 at 17:29










  • Agreed, I'll add that. Thanks!
    – John Moon
    Nov 28 '17 at 17:30






  • 4




    @JeffSchaller, note that -maxdepth is a GNU extension. Note 3 other differences with globs: find would include hidden files, not sort the list and fail to match file names that contain bytes not forming valid characters (like a $'Stxe9phane.atest' in a UTF-8 locale)
    – Stéphane Chazelas
    Nov 28 '17 at 17:32







1




1




perhaps with a -maxdepth 1, to closer emulate the ls behavior
– Jeff Schaller
Nov 28 '17 at 17:29




perhaps with a -maxdepth 1, to closer emulate the ls behavior
– Jeff Schaller
Nov 28 '17 at 17:29












Agreed, I'll add that. Thanks!
– John Moon
Nov 28 '17 at 17:30




Agreed, I'll add that. Thanks!
– John Moon
Nov 28 '17 at 17:30




4




4




@JeffSchaller, note that -maxdepth is a GNU extension. Note 3 other differences with globs: find would include hidden files, not sort the list and fail to match file names that contain bytes not forming valid characters (like a $'Stxe9phane.atest' in a UTF-8 locale)
– Stéphane Chazelas
Nov 28 '17 at 17:32




@JeffSchaller, note that -maxdepth is a GNU extension. Note 3 other differences with globs: find would include hidden files, not sort the list and fail to match file names that contain bytes not forming valid characters (like a $'Stxe9phane.atest' in a UTF-8 locale)
– Stéphane Chazelas
Nov 28 '17 at 17:32












up vote
11
down vote















In



ls -d ./foldername/*.a,b,test


a,b,... is not a glob operator, that's brace expansion, that's first expanded to:



ls -d ./foldername/*.atest ./foldername/*.btest ./foldername/*.test


And each glob expanded individually, and if any glob doesn't match, the command is cancelled as you'd expect in zsh (or fish; in bash, you need the failglob option to get a similar behaviour).



Here, you'd want to use a single glob that matches all those files, and only cancel the command if that one glob didn't match any file:



ls -d ./foldername/*.(a|b|)test


You don't want to use nullglob, as if none of the globs matched, it would run ls without arguments, so list the current directory. cshnullglob is better in that regard as it removes non-matching globs but still cancels the command if all the globs fail to match.



You wouldn't want to use nonomatch, as that would give you the broken behaviour of bash which would be a shame.



For a glob alternative that works in both zsh and bash, you could use the ksh globs (set -o kshglob in zsh and shopt -s extglob in bash).



Then, you'd do:



ls -d ./foldername/*.@(a|b|)test


or:



ls -d ./foldername/*.?([ab])test


Add the failglob option in bash to avoid the glob being passed literally to ls when it doesn't match.



See Why is nullglob not default? for more information.






share|improve this answer






















  • I'm not sure why it's a requirement of the OP, but is there an extglob that works for this in both bash and zsh? I had to use ?(...) or +(...) for bash.
    – Jeff Schaller
    Nov 28 '17 at 17:28










  • @JeffSchaller, see edit
    – Stéphane Chazelas
    Nov 28 '17 at 17:33














up vote
11
down vote















In



ls -d ./foldername/*.a,b,test


a,b,... is not a glob operator, that's brace expansion, that's first expanded to:



ls -d ./foldername/*.atest ./foldername/*.btest ./foldername/*.test


And each glob expanded individually, and if any glob doesn't match, the command is cancelled as you'd expect in zsh (or fish; in bash, you need the failglob option to get a similar behaviour).



Here, you'd want to use a single glob that matches all those files, and only cancel the command if that one glob didn't match any file:



ls -d ./foldername/*.(a|b|)test


You don't want to use nullglob, as if none of the globs matched, it would run ls without arguments, so list the current directory. cshnullglob is better in that regard as it removes non-matching globs but still cancels the command if all the globs fail to match.



You wouldn't want to use nonomatch, as that would give you the broken behaviour of bash which would be a shame.



For a glob alternative that works in both zsh and bash, you could use the ksh globs (set -o kshglob in zsh and shopt -s extglob in bash).



Then, you'd do:



ls -d ./foldername/*.@(a|b|)test


or:



ls -d ./foldername/*.?([ab])test


Add the failglob option in bash to avoid the glob being passed literally to ls when it doesn't match.



See Why is nullglob not default? for more information.






share|improve this answer






















  • I'm not sure why it's a requirement of the OP, but is there an extglob that works for this in both bash and zsh? I had to use ?(...) or +(...) for bash.
    – Jeff Schaller
    Nov 28 '17 at 17:28










  • @JeffSchaller, see edit
    – Stéphane Chazelas
    Nov 28 '17 at 17:33












up vote
11
down vote










up vote
11
down vote











In



ls -d ./foldername/*.a,b,test


a,b,... is not a glob operator, that's brace expansion, that's first expanded to:



ls -d ./foldername/*.atest ./foldername/*.btest ./foldername/*.test


And each glob expanded individually, and if any glob doesn't match, the command is cancelled as you'd expect in zsh (or fish; in bash, you need the failglob option to get a similar behaviour).



Here, you'd want to use a single glob that matches all those files, and only cancel the command if that one glob didn't match any file:



ls -d ./foldername/*.(a|b|)test


You don't want to use nullglob, as if none of the globs matched, it would run ls without arguments, so list the current directory. cshnullglob is better in that regard as it removes non-matching globs but still cancels the command if all the globs fail to match.



You wouldn't want to use nonomatch, as that would give you the broken behaviour of bash which would be a shame.



For a glob alternative that works in both zsh and bash, you could use the ksh globs (set -o kshglob in zsh and shopt -s extglob in bash).



Then, you'd do:



ls -d ./foldername/*.@(a|b|)test


or:



ls -d ./foldername/*.?([ab])test


Add the failglob option in bash to avoid the glob being passed literally to ls when it doesn't match.



See Why is nullglob not default? for more information.






share|improve this answer
















In



ls -d ./foldername/*.a,b,test


a,b,... is not a glob operator, that's brace expansion, that's first expanded to:



ls -d ./foldername/*.atest ./foldername/*.btest ./foldername/*.test


And each glob expanded individually, and if any glob doesn't match, the command is cancelled as you'd expect in zsh (or fish; in bash, you need the failglob option to get a similar behaviour).



Here, you'd want to use a single glob that matches all those files, and only cancel the command if that one glob didn't match any file:



ls -d ./foldername/*.(a|b|)test


You don't want to use nullglob, as if none of the globs matched, it would run ls without arguments, so list the current directory. cshnullglob is better in that regard as it removes non-matching globs but still cancels the command if all the globs fail to match.



You wouldn't want to use nonomatch, as that would give you the broken behaviour of bash which would be a shame.



For a glob alternative that works in both zsh and bash, you could use the ksh globs (set -o kshglob in zsh and shopt -s extglob in bash).



Then, you'd do:



ls -d ./foldername/*.@(a|b|)test


or:



ls -d ./foldername/*.?([ab])test


Add the failglob option in bash to avoid the glob being passed literally to ls when it doesn't match.



See Why is nullglob not default? for more information.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 28 '17 at 17:42

























answered Nov 28 '17 at 17:25









Stéphane Chazelas

282k53520854




282k53520854











  • I'm not sure why it's a requirement of the OP, but is there an extglob that works for this in both bash and zsh? I had to use ?(...) or +(...) for bash.
    – Jeff Schaller
    Nov 28 '17 at 17:28










  • @JeffSchaller, see edit
    – Stéphane Chazelas
    Nov 28 '17 at 17:33
















  • I'm not sure why it's a requirement of the OP, but is there an extglob that works for this in both bash and zsh? I had to use ?(...) or +(...) for bash.
    – Jeff Schaller
    Nov 28 '17 at 17:28










  • @JeffSchaller, see edit
    – Stéphane Chazelas
    Nov 28 '17 at 17:33















I'm not sure why it's a requirement of the OP, but is there an extglob that works for this in both bash and zsh? I had to use ?(...) or +(...) for bash.
– Jeff Schaller
Nov 28 '17 at 17:28




I'm not sure why it's a requirement of the OP, but is there an extglob that works for this in both bash and zsh? I had to use ?(...) or +(...) for bash.
– Jeff Schaller
Nov 28 '17 at 17:28












@JeffSchaller, see edit
– Stéphane Chazelas
Nov 28 '17 at 17:33




@JeffSchaller, see edit
– Stéphane Chazelas
Nov 28 '17 at 17:33

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407532%2fls-ignore-no-matches%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?