cp after xargs not working
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
.
Related videos on Youtube
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, 2022Comments
-
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 about 7 yearsI think you meant to use
cp -t
instead ofcp -u
? -
Admin about 7 yearsNo 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 about 7 yearsalso, 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 about 7 yearsThat works. I'm thinkinng sometimes to complicated. ^^ Make your comment to an answer, so I can except this.
-
-
Nicholas Terry almost 6 yearsis the {} arbitrary? could I do -Iblah for example?
-
aragaer almost 6 yearsYes, you could.
-
Mohamed Sylla almost 5 yearson mac without -u