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
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. Sogrep 'NC022.*nii'
matchesNC022
tonii
with anything (including nothing) in between.In contrast,
.
is literal in shell globs, while*
itself means zero or more characters. SoNC022.*nii
matchesNC022.
tonii
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 wantNC022*.nii
i.e.In our case, the issue was a little different. There was a space in the path, so we had put the whole path in quotes. Then the same error message
mv: cannot stat xxxx': No such file or directory
results. The solution was to omit the quotes and mask the spaces:Before:
Solution:
Maybe this will help others who are looking for this issue.