Search code examples
scplswc

Count number of files in directory then scp transfer a certain range such as 21404-42806


I found the number of files in /dev/shm/split/1/ to be 42806 using:

/bin/ls -lU /dev/shm/split/1/ | wc -l

What I can't seem to find anywhere online is how to select a certain range, say from 21404-42806, and use scp to securely copy those files. Then, for management purposes, I would like to move the files I copied to another folder, say /dev/shm/split/2/.

How do I do that using CentOS?

I tried:

sudo chmod 400 ~/emails/name.pem ; ls -1 /dev/shm/split/1/ | sed -n '21443,42806p' | xargs -i scp -i ~/emails/name.pem {} root@ipaddress:/dev/shm/split/2/ 

This produced:

no such file or directory

errors on all of the files...


Solution

  • ls itself lists files relative to the directory you give. This means your ls prints the filenames in the directory, but later on, scp doesn't have the path to them. You can fix this two ways:

    Give the path to scp:

    ls -1 /dev/shm/split/1/ | sed -n '21443,42806p' | xargs -i \
      scp -i ~/emails/name.pem /dev/shm/split/1/{} root@ipaddress:/dev/shm/split/2/ 
    

    Change to that directory and it will work:

    cd /dev/shm/split/1/; ls -1 | sed -n '21443,42806p' | xargs -i \
      scp -i ~/emails/name.pem {} root@ipaddress:/dev/shm/split/2/