Results 1 to 3 of 3

Thread: Copy every nth file

  1. #1

    Default Copy every nth file

    Hi!

    I want to create a bash script which should do something to every nth file in a directory, i.e. copy. The script I got so far looks like this:

    #! /bin/sh

    for F in *.ifd
    do
    echo copying: $F
    done
    But now it just copies every file. Now I want it to copy only every 2nd file for example. What would the approach be for that?

    Kind regards,


    Phong

  2. #2

    Default Re: Copy every nth file

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Dirty hack:

    <code interpreter="/bin/bash">
    #!/bin/bash
    ctr0=0;
    for F in *.ifd; do
    if [ $ctr0 = 0 ] ; then
    let ctr0=$ctr0+1 ;
    echo $F;
    else ctr0=0;
    fi
    done
    </code>

    Good luck.





    NhatPhongTran wrote:
    > Hi!
    >
    > I want to create a bash script which should do something to every nth
    > file in a directory, i.e. copy. The script I got so far looks like
    > this:
    >
    >> #! /bin/sh
    >>
    >> for F in *.ifd
    >> do
    >> echo copying: $F
    >> done

    >
    > But now it just copies every file. Now I want it to copy only every 2nd
    > file for example. What would the approach be for that?
    >
    > Kind regards,
    >
    >
    > Phong
    >
    >

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.9 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

    iQIcBAEBAgAGBQJKfL19AAoJEF+XTK08PnB5+V8QAIks57dvC1WVw6pRwsZSTICS
    GjQyJzHhX4afax1zQFEXXNhok4niRqIw7YfGWlQ24uAnvPURd3al+0mInZJbhZeG
    qppvB2RWwjmVrrdAy7drVRDmR/pyQtubvmK7rw2cK5qY/WNlsMacKH0w7SJcee5a
    ZN7i63hJEagHNCAgd3JVSYipq8HD+gs1I+Q7ELKcnkwbnwu8mxCZ52n9qeB9yqdw
    uL8Ax3KasykElHbXkZx9ZYDBzOE+qwP755CQFVKMw57nUHJePyiiV5ZCujPHTZ26
    Y6LOb2a8rDvu7BNGl8nWVTyCoKgKFJPeBuvIvEYDFM6+lKc1Lw6P+TiQ3iliW+NJ
    LXfUxx/PRHWGlBbLB5WgVrwbhpV9sx6/khGYEm80EqqlVPz4fZGha1hpuuwbNya8
    6wWCnciH+9NyTkosvtZmcNBjkiKn9hcrEtLA4pWtJ3Id96Vf5uvvnD7OexmkSHBk
    iUCGwX45gz9YlN8wjIY2r1bSVwxHGGIu9L+euolgeIawhf22q/0cB5+U0UFXdwlY
    x+BDSnDSj2WoczwrR3H9AkbUhs/2v2ZH+PrSvlscLPq/zDoCrOTrlGZgKPa/v+qt
    IIYvgOT6pEWYOmIYf6ZAYLb7B8uGUnL1ugZRFuMdwJ4ldhBMWpSdqg4DliRgKFFD
    KzJZHyYVc+41owWHb1zB
    =uJ35
    -----END PGP SIGNATURE-----

  3. #3

    Default Re: Copy every nth file

    Code:
    ls | awk 'NR%2==0{ cmd = "mv \047"$0"\047 /destination";system(cmd)}'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •