0

For example, files starting with the number 1 to the folder number 1 and so on…, I can do it for each one using the following command:

find /home/ -name '1_*_*_*_*_*_*.nii' -exec cp '{}' /home/1 ';'
0

You mean something like this (1 to 100)?

for i in {1..100}; do
  find /home/ -name "${i}_*_*_*_*_*_*.nii" -exec cp '{}' /home/${i} \;
done
  • Thanks a lot for your response, but I do not know why it did not work !! – Nour Nov 4 '16 at 10:58
  • I fixed the find command you provided and replaced single quotes with double. – rdupz Nov 4 '16 at 11:03
0

Into the source directory run this script:

for name in *.nii ; # find all files with a pattern and process its
do
  directory="${name%%_*}/"
  if [ ! -d "$directory" ]; then mkdir "$directory" ; fi # make directory if not exist
  cp "$name" "$directory" ; # copy file into the directory
done

The ${name%%_*} removes longest matching suffix pattern. In this case it will be all symbols before first _. The result of the substitution for name 1_2_3.nii will be cp 1_2_3.nii 1/.

This solution is more flexible because it automatically creates necessary folders and there's no need to give the range of files prefix.

  • 1
    Don't forget about double quotes in case there are file names with spaces. – phk Nov 4 '16 at 10:35
  • Thank you very much, also it did not work, it showed this message: cp: omitting directory ‘*.nii’ – Nour Nov 4 '16 at 11:03
  • Removed single quotes at the first line --- for name in *.nii – Konstantin Morenko Nov 4 '16 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.