How to match a file name by specifying a prefix substring?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I would like to copy the files, located on a remote machine, whose filenames match a prefix string, on my local machine.
I have tried to select only the files whose names start with either massif
or calgrind
using the following command:
scp my-machine:/home/user/a-folder/[massif,callgrind]* .
However, this expression allows to match the empty string plus any number of characters and therefore, matches all the filenames related to the directory.
What do I have to change to make it work correctly?
bash shell-script filenames wildcards bash-expansion
New contributor
add a comment |Â
up vote
1
down vote
favorite
I would like to copy the files, located on a remote machine, whose filenames match a prefix string, on my local machine.
I have tried to select only the files whose names start with either massif
or calgrind
using the following command:
scp my-machine:/home/user/a-folder/[massif,callgrind]* .
However, this expression allows to match the empty string plus any number of characters and therefore, matches all the filenames related to the directory.
What do I have to change to make it work correctly?
bash shell-script filenames wildcards bash-expansion
New contributor
It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
â nico
20 hours ago
The brace expansion may produce filenames that do not exist.
â Fólkvangr
6 hours ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I would like to copy the files, located on a remote machine, whose filenames match a prefix string, on my local machine.
I have tried to select only the files whose names start with either massif
or calgrind
using the following command:
scp my-machine:/home/user/a-folder/[massif,callgrind]* .
However, this expression allows to match the empty string plus any number of characters and therefore, matches all the filenames related to the directory.
What do I have to change to make it work correctly?
bash shell-script filenames wildcards bash-expansion
New contributor
I would like to copy the files, located on a remote machine, whose filenames match a prefix string, on my local machine.
I have tried to select only the files whose names start with either massif
or calgrind
using the following command:
scp my-machine:/home/user/a-folder/[massif,callgrind]* .
However, this expression allows to match the empty string plus any number of characters and therefore, matches all the filenames related to the directory.
What do I have to change to make it work correctly?
bash shell-script filenames wildcards bash-expansion
bash shell-script filenames wildcards bash-expansion
New contributor
New contributor
edited 7 mins ago
Fólkvangr
38010
38010
New contributor
asked 20 hours ago
nico
83
83
New contributor
New contributor
It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
â nico
20 hours ago
The brace expansion may produce filenames that do not exist.
â Fólkvangr
6 hours ago
add a comment |Â
It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
â nico
20 hours ago
The brace expansion may produce filenames that do not exist.
â Fólkvangr
6 hours ago
It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
â nico
20 hours ago
It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
â nico
20 hours ago
The brace expansion may produce filenames that do not exist.
â Fólkvangr
6 hours ago
The brace expansion may produce filenames that do not exist.
â Fólkvangr
6 hours ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
It sounds like you want curly-brackets there:
scp my-machine:/home/user/a-folder/massif,callgrind* .
Otherwise you're matching on single characters, rather than the two complete strings.
bash
does a number of different types of expansion & substitution; these are documented in the bash man page - looks for the EXPANSION
section. Here, the [
type is covered under "Pathname Expansion" (it matches on pathnames) and {
under "Brace Expansion" (it generates multiple arguments from the options provided, regardless of files present).
New contributor
Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
â nico
20 hours ago
Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
â nico
20 hours ago
I've just added an edit with some references to documentation, hope that helps!
â Jeremy Kerr
20 hours ago
add a comment |Â
up vote
0
down vote
Bash provides some extended patterns if the extglob
shell option is enabled (c.f. Bash Reference Manual).
prompt% ssh username@hostname echo 'shopt -s extglob >>~/.bash_profile'
prompt% scp username@hostname:"/home/user/dirname/@(foo|bar)*" .
The first command enables the extglob
shell option on the remote host. The second command allows to copy filenames that begin with foo
or bar
in the current directory of the local machine.
Note: I recommend editing the configuration file .bash_profile
manually. The double quotes are necessary to avoid the interpretation of the filename expansion by the local shell.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
It sounds like you want curly-brackets there:
scp my-machine:/home/user/a-folder/massif,callgrind* .
Otherwise you're matching on single characters, rather than the two complete strings.
bash
does a number of different types of expansion & substitution; these are documented in the bash man page - looks for the EXPANSION
section. Here, the [
type is covered under "Pathname Expansion" (it matches on pathnames) and {
under "Brace Expansion" (it generates multiple arguments from the options provided, regardless of files present).
New contributor
Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
â nico
20 hours ago
Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
â nico
20 hours ago
I've just added an edit with some references to documentation, hope that helps!
â Jeremy Kerr
20 hours ago
add a comment |Â
up vote
1
down vote
accepted
It sounds like you want curly-brackets there:
scp my-machine:/home/user/a-folder/massif,callgrind* .
Otherwise you're matching on single characters, rather than the two complete strings.
bash
does a number of different types of expansion & substitution; these are documented in the bash man page - looks for the EXPANSION
section. Here, the [
type is covered under "Pathname Expansion" (it matches on pathnames) and {
under "Brace Expansion" (it generates multiple arguments from the options provided, regardless of files present).
New contributor
Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
â nico
20 hours ago
Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
â nico
20 hours ago
I've just added an edit with some references to documentation, hope that helps!
â Jeremy Kerr
20 hours ago
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
It sounds like you want curly-brackets there:
scp my-machine:/home/user/a-folder/massif,callgrind* .
Otherwise you're matching on single characters, rather than the two complete strings.
bash
does a number of different types of expansion & substitution; these are documented in the bash man page - looks for the EXPANSION
section. Here, the [
type is covered under "Pathname Expansion" (it matches on pathnames) and {
under "Brace Expansion" (it generates multiple arguments from the options provided, regardless of files present).
New contributor
It sounds like you want curly-brackets there:
scp my-machine:/home/user/a-folder/massif,callgrind* .
Otherwise you're matching on single characters, rather than the two complete strings.
bash
does a number of different types of expansion & substitution; these are documented in the bash man page - looks for the EXPANSION
section. Here, the [
type is covered under "Pathname Expansion" (it matches on pathnames) and {
under "Brace Expansion" (it generates multiple arguments from the options provided, regardless of files present).
New contributor
edited 20 hours ago
New contributor
answered 20 hours ago
Jeremy Kerr
1305
1305
New contributor
New contributor
Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
â nico
20 hours ago
Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
â nico
20 hours ago
I've just added an edit with some references to documentation, hope that helps!
â Jeremy Kerr
20 hours ago
add a comment |Â
Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
â nico
20 hours ago
Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
â nico
20 hours ago
I've just added an edit with some references to documentation, hope that helps!
â Jeremy Kerr
20 hours ago
Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
â nico
20 hours ago
Aha! That is it. In fact it was accepting anything that would start with these letters plus the comma. Thank you!
â nico
20 hours ago
Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
â nico
20 hours ago
Also, do you happen to know how you call this construct/feature? It was hard to find this information in google. I always hit the if [[...]] or if[...] question from there.
â nico
20 hours ago
I've just added an edit with some references to documentation, hope that helps!
â Jeremy Kerr
20 hours ago
I've just added an edit with some references to documentation, hope that helps!
â Jeremy Kerr
20 hours ago
add a comment |Â
up vote
0
down vote
Bash provides some extended patterns if the extglob
shell option is enabled (c.f. Bash Reference Manual).
prompt% ssh username@hostname echo 'shopt -s extglob >>~/.bash_profile'
prompt% scp username@hostname:"/home/user/dirname/@(foo|bar)*" .
The first command enables the extglob
shell option on the remote host. The second command allows to copy filenames that begin with foo
or bar
in the current directory of the local machine.
Note: I recommend editing the configuration file .bash_profile
manually. The double quotes are necessary to avoid the interpretation of the filename expansion by the local shell.
add a comment |Â
up vote
0
down vote
Bash provides some extended patterns if the extglob
shell option is enabled (c.f. Bash Reference Manual).
prompt% ssh username@hostname echo 'shopt -s extglob >>~/.bash_profile'
prompt% scp username@hostname:"/home/user/dirname/@(foo|bar)*" .
The first command enables the extglob
shell option on the remote host. The second command allows to copy filenames that begin with foo
or bar
in the current directory of the local machine.
Note: I recommend editing the configuration file .bash_profile
manually. The double quotes are necessary to avoid the interpretation of the filename expansion by the local shell.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Bash provides some extended patterns if the extglob
shell option is enabled (c.f. Bash Reference Manual).
prompt% ssh username@hostname echo 'shopt -s extglob >>~/.bash_profile'
prompt% scp username@hostname:"/home/user/dirname/@(foo|bar)*" .
The first command enables the extglob
shell option on the remote host. The second command allows to copy filenames that begin with foo
or bar
in the current directory of the local machine.
Note: I recommend editing the configuration file .bash_profile
manually. The double quotes are necessary to avoid the interpretation of the filename expansion by the local shell.
Bash provides some extended patterns if the extglob
shell option is enabled (c.f. Bash Reference Manual).
prompt% ssh username@hostname echo 'shopt -s extglob >>~/.bash_profile'
prompt% scp username@hostname:"/home/user/dirname/@(foo|bar)*" .
The first command enables the extglob
shell option on the remote host. The second command allows to copy filenames that begin with foo
or bar
in the current directory of the local machine.
Note: I recommend editing the configuration file .bash_profile
manually. The double quotes are necessary to avoid the interpretation of the filename expansion by the local shell.
edited 11 hours ago
answered 15 hours ago
Fólkvangr
38010
38010
add a comment |Â
add a comment |Â
nico is a new contributor. Be nice, and check out our Code of Conduct.
nico is a new contributor. Be nice, and check out our Code of Conduct.
nico is a new contributor. Be nice, and check out our Code of Conduct.
nico is a new contributor. Be nice, and check out our Code of Conduct.
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f477664%2fhow-to-match-a-file-name-by-specifying-a-prefix-substring%23new-answer', 'question_page');
);
Post as a guest
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
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
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
It seems that by using square brackets I was accepting any of the characters inside the square brackets and not the empty string as I first thought.
â nico
20 hours ago
The brace expansion may produce filenames that do not exist.
â Fólkvangr
6 hours ago