The best way to expand glob pattern?

Clash Royale CLAN TAG#URR8PPP
I need to expand a glob pattern (like ../smth*/*, or /etc/cron*/) into a list of files, programmatically. What would be the best way to do it?
bash wildcards
add a comment |
I need to expand a glob pattern (like ../smth*/*, or /etc/cron*/) into a list of files, programmatically. What would be the best way to do it?
bash wildcards
2
You don't need to do anything special, just don't quote the*.
– Kevin
Mar 12 '12 at 14:32
1
Though if you're going to be trying to parse it, use an array like the answer says.
– Kevin
Mar 12 '12 at 14:34
add a comment |
I need to expand a glob pattern (like ../smth*/*, or /etc/cron*/) into a list of files, programmatically. What would be the best way to do it?
bash wildcards
I need to expand a glob pattern (like ../smth*/*, or /etc/cron*/) into a list of files, programmatically. What would be the best way to do it?
bash wildcards
bash wildcards
edited May 14 '13 at 6:45
vadipp
1838
1838
asked Mar 12 '12 at 13:28
RogachRogach
1,986102638
1,986102638
2
You don't need to do anything special, just don't quote the*.
– Kevin
Mar 12 '12 at 14:32
1
Though if you're going to be trying to parse it, use an array like the answer says.
– Kevin
Mar 12 '12 at 14:34
add a comment |
2
You don't need to do anything special, just don't quote the*.
– Kevin
Mar 12 '12 at 14:32
1
Though if you're going to be trying to parse it, use an array like the answer says.
– Kevin
Mar 12 '12 at 14:34
2
2
You don't need to do anything special, just don't quote the
*.– Kevin
Mar 12 '12 at 14:32
You don't need to do anything special, just don't quote the
*.– Kevin
Mar 12 '12 at 14:32
1
1
Though if you're going to be trying to parse it, use an array like the answer says.
– Kevin
Mar 12 '12 at 14:34
Though if you're going to be trying to parse it, use an array like the answer says.
– Kevin
Mar 12 '12 at 14:34
add a comment |
2 Answers
2
active
oldest
votes
Just let it expand inside an array declaration's right side:
list=(../smth*/) # grab the list
echo "$#list[@]" # print array length
echo "$list[@]" # print array elements
for file in "$list[@]"; do echo "$file"; done # loop over the array
Note that the shell option nullglob needs to be set.
It is not set by default.
It prevents an error in case the glob (or one of multiple globs) does not match any name.
Set it in bash with
shopt -s nullglob
or in zsh with
setopt nullglob
And how do I print that list afterwards?
– Rogach
Mar 12 '12 at 13:38
It is just a regular array. You can do whatever you can with any array. Added some examples.
– manatwork
Mar 12 '12 at 13:44
1
There is a problem. If pattern matches no files, it prints itself - which is not very good.
– Rogach
Mar 12 '12 at 14:21
5
Ah, shopt -s nullglob solves it.
– Rogach
Mar 12 '12 at 14:25
1
@lindhe,unsetoptand the same parameter as used forsetopt.
– manatwork
Dec 5 '15 at 18:48
|
show 1 more comment
No need to overcomplicate things:
echo your/stuff*
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f34011%2fthe-best-way-to-expand-glob-pattern%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just let it expand inside an array declaration's right side:
list=(../smth*/) # grab the list
echo "$#list[@]" # print array length
echo "$list[@]" # print array elements
for file in "$list[@]"; do echo "$file"; done # loop over the array
Note that the shell option nullglob needs to be set.
It is not set by default.
It prevents an error in case the glob (or one of multiple globs) does not match any name.
Set it in bash with
shopt -s nullglob
or in zsh with
setopt nullglob
And how do I print that list afterwards?
– Rogach
Mar 12 '12 at 13:38
It is just a regular array. You can do whatever you can with any array. Added some examples.
– manatwork
Mar 12 '12 at 13:44
1
There is a problem. If pattern matches no files, it prints itself - which is not very good.
– Rogach
Mar 12 '12 at 14:21
5
Ah, shopt -s nullglob solves it.
– Rogach
Mar 12 '12 at 14:25
1
@lindhe,unsetoptand the same parameter as used forsetopt.
– manatwork
Dec 5 '15 at 18:48
|
show 1 more comment
Just let it expand inside an array declaration's right side:
list=(../smth*/) # grab the list
echo "$#list[@]" # print array length
echo "$list[@]" # print array elements
for file in "$list[@]"; do echo "$file"; done # loop over the array
Note that the shell option nullglob needs to be set.
It is not set by default.
It prevents an error in case the glob (or one of multiple globs) does not match any name.
Set it in bash with
shopt -s nullglob
or in zsh with
setopt nullglob
And how do I print that list afterwards?
– Rogach
Mar 12 '12 at 13:38
It is just a regular array. You can do whatever you can with any array. Added some examples.
– manatwork
Mar 12 '12 at 13:44
1
There is a problem. If pattern matches no files, it prints itself - which is not very good.
– Rogach
Mar 12 '12 at 14:21
5
Ah, shopt -s nullglob solves it.
– Rogach
Mar 12 '12 at 14:25
1
@lindhe,unsetoptand the same parameter as used forsetopt.
– manatwork
Dec 5 '15 at 18:48
|
show 1 more comment
Just let it expand inside an array declaration's right side:
list=(../smth*/) # grab the list
echo "$#list[@]" # print array length
echo "$list[@]" # print array elements
for file in "$list[@]"; do echo "$file"; done # loop over the array
Note that the shell option nullglob needs to be set.
It is not set by default.
It prevents an error in case the glob (or one of multiple globs) does not match any name.
Set it in bash with
shopt -s nullglob
or in zsh with
setopt nullglob
Just let it expand inside an array declaration's right side:
list=(../smth*/) # grab the list
echo "$#list[@]" # print array length
echo "$list[@]" # print array elements
for file in "$list[@]"; do echo "$file"; done # loop over the array
Note that the shell option nullglob needs to be set.
It is not set by default.
It prevents an error in case the glob (or one of multiple globs) does not match any name.
Set it in bash with
shopt -s nullglob
or in zsh with
setopt nullglob
edited Jan 2 '16 at 15:59
Jakuje
16.5k53155
16.5k53155
answered Mar 12 '12 at 13:37
manatworkmanatwork
22k38385
22k38385
And how do I print that list afterwards?
– Rogach
Mar 12 '12 at 13:38
It is just a regular array. You can do whatever you can with any array. Added some examples.
– manatwork
Mar 12 '12 at 13:44
1
There is a problem. If pattern matches no files, it prints itself - which is not very good.
– Rogach
Mar 12 '12 at 14:21
5
Ah, shopt -s nullglob solves it.
– Rogach
Mar 12 '12 at 14:25
1
@lindhe,unsetoptand the same parameter as used forsetopt.
– manatwork
Dec 5 '15 at 18:48
|
show 1 more comment
And how do I print that list afterwards?
– Rogach
Mar 12 '12 at 13:38
It is just a regular array. You can do whatever you can with any array. Added some examples.
– manatwork
Mar 12 '12 at 13:44
1
There is a problem. If pattern matches no files, it prints itself - which is not very good.
– Rogach
Mar 12 '12 at 14:21
5
Ah, shopt -s nullglob solves it.
– Rogach
Mar 12 '12 at 14:25
1
@lindhe,unsetoptand the same parameter as used forsetopt.
– manatwork
Dec 5 '15 at 18:48
And how do I print that list afterwards?
– Rogach
Mar 12 '12 at 13:38
And how do I print that list afterwards?
– Rogach
Mar 12 '12 at 13:38
It is just a regular array. You can do whatever you can with any array. Added some examples.
– manatwork
Mar 12 '12 at 13:44
It is just a regular array. You can do whatever you can with any array. Added some examples.
– manatwork
Mar 12 '12 at 13:44
1
1
There is a problem. If pattern matches no files, it prints itself - which is not very good.
– Rogach
Mar 12 '12 at 14:21
There is a problem. If pattern matches no files, it prints itself - which is not very good.
– Rogach
Mar 12 '12 at 14:21
5
5
Ah, shopt -s nullglob solves it.
– Rogach
Mar 12 '12 at 14:25
Ah, shopt -s nullglob solves it.
– Rogach
Mar 12 '12 at 14:25
1
1
@lindhe,
unsetopt and the same parameter as used for setopt.– manatwork
Dec 5 '15 at 18:48
@lindhe,
unsetopt and the same parameter as used for setopt.– manatwork
Dec 5 '15 at 18:48
|
show 1 more comment
No need to overcomplicate things:
echo your/stuff*
add a comment |
No need to overcomplicate things:
echo your/stuff*
add a comment |
No need to overcomplicate things:
echo your/stuff*
No need to overcomplicate things:
echo your/stuff*
answered Feb 8 at 17:28
Alexei AverchenkoAlexei Averchenko
1011
1011
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f34011%2fthe-best-way-to-expand-glob-pattern%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You don't need to do anything special, just don't quote the
*.– Kevin
Mar 12 '12 at 14:32
1
Though if you're going to be trying to parse it, use an array like the answer says.
– Kevin
Mar 12 '12 at 14:34