How can I move all files matching a pattern into a folder?
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
1
down vote
favorite
ls | grep 'NC022.*nii'
Shows me all the files containing NC022
and nii
.
But when I try to move them using
mv NC022.*nii NC022/
It complains that
mv: cannot stat 'NC022.*nii': No such file or directory
This happens also if I try this (as seen in other answers).
mv -t NC022 'ls | grep 'NC022.*nii''
I am struggling to see what the error is, as I have the feeling of having done exactly the same thing numerous times without errors...
How can I move all files matching a pattern into a folder?
Example of partial ls output for first command:
NC022_Background1_Raw import W325.39 L290.nii
NC022_Background2_Copy (2) of Raw import W325.39 L290.nii
NC022_Background3_Raw import W1103.50 L551.nii
NC022_Mask1_mask_air.nii
command-line
add a comment |Â
up vote
1
down vote
favorite
ls | grep 'NC022.*nii'
Shows me all the files containing NC022
and nii
.
But when I try to move them using
mv NC022.*nii NC022/
It complains that
mv: cannot stat 'NC022.*nii': No such file or directory
This happens also if I try this (as seen in other answers).
mv -t NC022 'ls | grep 'NC022.*nii''
I am struggling to see what the error is, as I have the feeling of having done exactly the same thing numerous times without errors...
How can I move all files matching a pattern into a folder?
Example of partial ls output for first command:
NC022_Background1_Raw import W325.39 L290.nii
NC022_Background2_Copy (2) of Raw import W325.39 L290.nii
NC022_Background3_Raw import W1103.50 L551.nii
NC022_Mask1_mask_air.nii
command-line
1
Remember thatgrep
uses regular expressions whereas the shell uses glob matches. So for examplegrep 'NC022.*nii
matches zero or more characters betweenNC022
andnii
whereasmv NC022.*nii NC022/
will only move files matchingNC022.
then zero or more characters followed bynii
â steeldriver
Apr 24 at 22:03
@ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
â hirschme
Apr 24 at 22:08
2
@hirschme please edit your question to show some of the actual filenames (for example, partial output ofls | grep 'NC022.*nii'
)
â steeldriver
Apr 24 at 22:14
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
ls | grep 'NC022.*nii'
Shows me all the files containing NC022
and nii
.
But when I try to move them using
mv NC022.*nii NC022/
It complains that
mv: cannot stat 'NC022.*nii': No such file or directory
This happens also if I try this (as seen in other answers).
mv -t NC022 'ls | grep 'NC022.*nii''
I am struggling to see what the error is, as I have the feeling of having done exactly the same thing numerous times without errors...
How can I move all files matching a pattern into a folder?
Example of partial ls output for first command:
NC022_Background1_Raw import W325.39 L290.nii
NC022_Background2_Copy (2) of Raw import W325.39 L290.nii
NC022_Background3_Raw import W1103.50 L551.nii
NC022_Mask1_mask_air.nii
command-line
ls | grep 'NC022.*nii'
Shows me all the files containing NC022
and nii
.
But when I try to move them using
mv NC022.*nii NC022/
It complains that
mv: cannot stat 'NC022.*nii': No such file or directory
This happens also if I try this (as seen in other answers).
mv -t NC022 'ls | grep 'NC022.*nii''
I am struggling to see what the error is, as I have the feeling of having done exactly the same thing numerous times without errors...
How can I move all files matching a pattern into a folder?
Example of partial ls output for first command:
NC022_Background1_Raw import W325.39 L290.nii
NC022_Background2_Copy (2) of Raw import W325.39 L290.nii
NC022_Background3_Raw import W1103.50 L551.nii
NC022_Mask1_mask_air.nii
command-line
edited Apr 24 at 22:36
asked Apr 24 at 21:54
hirschme
296
296
1
Remember thatgrep
uses regular expressions whereas the shell uses glob matches. So for examplegrep 'NC022.*nii
matches zero or more characters betweenNC022
andnii
whereasmv NC022.*nii NC022/
will only move files matchingNC022.
then zero or more characters followed bynii
â steeldriver
Apr 24 at 22:03
@ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
â hirschme
Apr 24 at 22:08
2
@hirschme please edit your question to show some of the actual filenames (for example, partial output ofls | grep 'NC022.*nii'
)
â steeldriver
Apr 24 at 22:14
add a comment |Â
1
Remember thatgrep
uses regular expressions whereas the shell uses glob matches. So for examplegrep 'NC022.*nii
matches zero or more characters betweenNC022
andnii
whereasmv NC022.*nii NC022/
will only move files matchingNC022.
then zero or more characters followed bynii
â steeldriver
Apr 24 at 22:03
@ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
â hirschme
Apr 24 at 22:08
2
@hirschme please edit your question to show some of the actual filenames (for example, partial output ofls | grep 'NC022.*nii'
)
â steeldriver
Apr 24 at 22:14
1
1
Remember that
grep
uses regular expressions whereas the shell uses glob matches. So for example grep 'NC022.*nii
matches zero or more characters between NC022
and nii
whereas mv NC022.*nii NC022/
will only move files matching NC022.
then zero or more characters followed by nii
â steeldriver
Apr 24 at 22:03
Remember that
grep
uses regular expressions whereas the shell uses glob matches. So for example grep 'NC022.*nii
matches zero or more characters between NC022
and nii
whereas mv NC022.*nii NC022/
will only move files matching NC022.
then zero or more characters followed by nii
â steeldriver
Apr 24 at 22:03
@ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
â hirschme
Apr 24 at 22:08
@ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
â hirschme
Apr 24 at 22:08
2
2
@hirschme please edit your question to show some of the actual filenames (for example, partial output of
ls | grep 'NC022.*nii'
)â steeldriver
Apr 24 at 22:14
@hirschme please edit your question to show some of the actual filenames (for example, partial output of
ls | grep 'NC022.*nii'
)â steeldriver
Apr 24 at 22:14
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
You are confusing regular expression syntax (as used by grep
) with glob patterns (as used by the shell).
In regex, .
means any single character, and *
means zero or more repetitions. So grep 'NC022.*nii'
matches NC022
to nii
with anything (including nothing) in between.
In contrast, .
is literal in shell globs, while *
itself means zero or more characters. So NC022.*nii
matches NC022.
to nii
with anything (including nothing) in between.
In particular, if you are trying to match all files with a .nii
extension, the .
is in the wrong place: you'd want NC022*.nii
i.e.
mv NC022*.nii NC022/
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You are confusing regular expression syntax (as used by grep
) with glob patterns (as used by the shell).
In regex, .
means any single character, and *
means zero or more repetitions. So grep 'NC022.*nii'
matches NC022
to nii
with anything (including nothing) in between.
In contrast, .
is literal in shell globs, while *
itself means zero or more characters. So NC022.*nii
matches NC022.
to nii
with anything (including nothing) in between.
In particular, if you are trying to match all files with a .nii
extension, the .
is in the wrong place: you'd want NC022*.nii
i.e.
mv NC022*.nii NC022/
add a comment |Â
up vote
4
down vote
accepted
You are confusing regular expression syntax (as used by grep
) with glob patterns (as used by the shell).
In regex, .
means any single character, and *
means zero or more repetitions. So grep 'NC022.*nii'
matches NC022
to nii
with anything (including nothing) in between.
In contrast, .
is literal in shell globs, while *
itself means zero or more characters. So NC022.*nii
matches NC022.
to nii
with anything (including nothing) in between.
In particular, if you are trying to match all files with a .nii
extension, the .
is in the wrong place: you'd want NC022*.nii
i.e.
mv NC022*.nii NC022/
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You are confusing regular expression syntax (as used by grep
) with glob patterns (as used by the shell).
In regex, .
means any single character, and *
means zero or more repetitions. So grep 'NC022.*nii'
matches NC022
to nii
with anything (including nothing) in between.
In contrast, .
is literal in shell globs, while *
itself means zero or more characters. So NC022.*nii
matches NC022.
to nii
with anything (including nothing) in between.
In particular, if you are trying to match all files with a .nii
extension, the .
is in the wrong place: you'd want NC022*.nii
i.e.
mv NC022*.nii NC022/
You are confusing regular expression syntax (as used by grep
) with glob patterns (as used by the shell).
In regex, .
means any single character, and *
means zero or more repetitions. So grep 'NC022.*nii'
matches NC022
to nii
with anything (including nothing) in between.
In contrast, .
is literal in shell globs, while *
itself means zero or more characters. So NC022.*nii
matches NC022.
to nii
with anything (including nothing) in between.
In particular, if you are trying to match all files with a .nii
extension, the .
is in the wrong place: you'd want NC022*.nii
i.e.
mv NC022*.nii NC022/
answered Apr 24 at 23:55
steeldriver
62.8k1196165
62.8k1196165
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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%2faskubuntu.com%2fquestions%2f1027931%2fhow-can-i-move-all-files-matching-a-pattern-into-a-folder%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
1
Remember that
grep
uses regular expressions whereas the shell uses glob matches. So for examplegrep 'NC022.*nii
matches zero or more characters betweenNC022
andnii
whereasmv NC022.*nii NC022/
will only move files matchingNC022.
then zero or more characters followed bynii
â steeldriver
Apr 24 at 22:03
@ dessert good catch yes that is a typo. @steeldriver that seems to be the answer I was looking for. I guess if you could post an answer showing how can I match the regex in glob matches? I tried NC022*nii (to move files matching NC022 and zero or more characters followed by nii) but still no success..
â hirschme
Apr 24 at 22:08
2
@hirschme please edit your question to show some of the actual filenames (for example, partial output of
ls | grep 'NC022.*nii'
)â steeldriver
Apr 24 at 22:14