I have a long list of files named according to the following pattern:


Because these files correspond to different experimental treatments, I want to separate them into directories for each treatment. Is there a way to move say the 300 files numbered S905_S5_fasta.gz . . . S1205_S305_fasta.gz into a new directory?

I've tried using the for loop below to do this, but it has not worked:

for i in 'seq 905 1205'; do
  scp S${i}* Target_directory

The results of this loop are as follows:

cp: cannot stat 'Sseq': No such file or directory
cp: cannot stat '905': No such file or directory
cp: cannot stat '1205*': No such file or directory

Any help would be appreciated!

  • 1
    You have the wrong quotes; 'seq 905 1203' is just that static string of 14 characters. To run the seq command, you need $(seq 905 1205) or the equivalent legacy backticks (ASCII 96) which are hard to type in a comment because they are used for controlling formatting.
    – tripleee
    Jan 4, 2023 at 16:26
  • ... and the lack of quoting around "S$i"* causes further havoc. You'll also need to quote the target directory if it contains shell metacharacters like spaces etc, or backslash-escape them.
    – tripleee
    Jan 4, 2023 at 16:28
  • What exactly works also depends on which shell you are using; if you are on Bash or Ksh, you could perhaps enable extglob and copy with a single wildcard.
    – tripleee
    Jan 4, 2023 at 16:31
  • This syntax works: for i in $(seq 905 1205); do scp "S$i"* Target ; done Thank you! Jan 4, 2023 at 18:55

1 Answer 1


If you're using a shell such as bash that understands brace expansions you may be able to enumerate the files directly:

# S905_S5_fasta.gz to S1205_S305_fasta.gz
mkdir new_directory
mv S{905..1205}_* new_directory
  • OP asked for coping, not moving files, so because of complete linux beginners maybe it's better to put cp or scp instead of mv command in your last line.
    – Damir
    Jan 4, 2023 at 17:43
  • @Damir, the OP asked for moving in the question text (where I looked) but copying in the question title (which I skipped) Jan 4, 2023 at 17:51
  • @Damir cp I can understand, but you wouldn't use scp to copy files in the local filesystem Jan 4, 2023 at 17:52
  • you are right, I actually missed the part that you catched :) "Is there a way to move ...". Regarding scp, I saw OP used it in his for-loop so I thought maybe he has some good reason for that, I don't know... Even scp can be used to copy locally (the same syntax as cp), I agree with you, man page is clear "scp copies files between hosts on a network".
    – Damir
    Jan 4, 2023 at 18:17
  • Using your mv suggestion works, thank you! Jan 4, 2023 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .