How can I move all files matching a pattern into a folder?
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. 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/