I have a directory " /data/archive

In this directory there are 8 different subdirectories.

eg : /data/archive/a
/data/archive/b and so on

Files under these subdirectories have date appended to their names so I'm copying files only for the date I need.

Currently what I'm doing is this :

cd a
scp *20210826* user@server:/data/20210826_inputfiles/a
cd b
scp *20210826* user@server:/data/20210826_inputfiles/b

Is it possible to code it in a way that I don't have to move through each directory one by one ?

Any help on this would be very helpful.

Thanks in advance!


1 Answer 1


The most obvious is:

for dir in a b b d e f g ; do
    cd $dir
    scp *20210826* user@server:/data/20210826_inputfiles/$dir
    cd ..

(I added the cd .. because that would seem logical to me)

You can also put it in a script. That will need some rethinking:

  • do you want to give the date as an argument to the script?
  • do you want to have the script determine the direcories by itsself, or have them hard-coded in the script?

et cetera.

  • 1
    scp "$dir"/*20210826* ... would be more logical than changing directories at all
    – roaima
    Aug 28, 2021 at 16:02

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.