cp after xargs not working

40,121

Solution 1

ls -al /var/cache/apt/archives/ |
    grep 'i386' | 
    awk '{print $9}'

can be simplified to /var/cache/apt/archives/*i386*


So, use either of these two:

cp -u /var/cache/apt/archives/*i386* /home/alex/debian-share/apt-archives/

cp -ut /home/alex/debian-share/apt-archives/ /var/cache/apt/archives/*i386*

where

   -t, --target-directory=DIRECTORY
          copy all SOURCE arguments into DIRECTORY


See also info on parsing ls

Solution 2

While you already know how you should solve your current problem, I'll still answer about xargs.

xargs puts the string it got in the end of command, while in your case you need that string before the last argument of cp. Use -I option of xargs to construct the command. Like this:

ls /source/path/*pattern* | xargs -I{} cp -u {} /destination/path

In this example I'm using {} to as a replacement string, so the syntax looks similar to find.

Share:
40,121

Related videos on Youtube

Jeff Schaller
Author by

Jeff Schaller

Unix Systems administrator http://www.catb.org/esr/faqs/smart-questions.html http://unix.stackexchange.com/help/how-to-ask http://sscce.org/ http://stackoverflow.com/help/mcve

Updated on September 18, 2022

Comments

  • Jeff Schaller
    Jeff Schaller over 1 year

    I run debian jessie on Host 64-bit and in virtualbox 32-bit. To spare traffic I try to cp the i386 packages from host to the shared folder, for using them in virualbox.

    My Hostname/var/cache/apt/archives$ ls -al /var/cache/apt/archives/ |
        grep 'i386' | 
        awk '{print $9}'
    alsa-oss_1.0.28-1_i386.deb
    gcc-4.9-base_4.9.2-10_i386.deb
    i965-va-driver_1.4.1-2_i386.deb
    libaacplus2_2.0.2-dmo2_i386.deb
    libaio1_0.3.110-1_i386.deb
    libasound2_1.0.28-1_i386.deb
    libasound2-dev_1.0.28-1_i386.deb
    libasound2-plugins_1.0.28-1+b1_i386.deb
    

    Shows me the packages I'm looking for. but them I try to cp them after xargs

    My Hostname/var/cache/apt/archives$ ls -al /var/cache/apt/archives/ |
        grep 'i386' |
        awk '{print $9}' |
        LANG=C xargs cp -u /home/alex/debian-share/apt-archives/
    cp: target 'zlib1g_1%3a1.2.8.dfsg-2+b1_i386.deb' is not a directory
    

    I can not figure out what I am doing wrong. Is this way even possible?

    My problem is I can not script. Probable it is somthing like that

    for i in *_i386.deb ; do cp [option] full-path to shared-folder
    

    I didn't dry, because I will not mess my Host.

    • Admin
      Admin about 7 years
      I think you meant to use cp -t instead of cp -u ?
    • Admin
      Admin about 7 years
      No I mean -u -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing
    • Admin
      Admin about 7 years
      also, whole thing can be simplified I feel.. something like cp -ut /home/alex/debian-share/apt-archives/ /var/cache/apt/archives/*i386* .. please try this on some other sample directories
    • Admin
      Admin about 7 years
      That works. I'm thinkinng sometimes to complicated. ^^ Make your comment to an answer, so I can except this.
  • Nicholas Terry
    Nicholas Terry almost 6 years
    is the {} arbitrary? could I do -Iblah for example?
  • aragaer
    aragaer almost 6 years
    Yes, you could.
  • Mohamed Sylla
    Mohamed Sylla almost 5 years
    on mac without -u