Solved

Using xargs and pipelining

Posted on 2009-05-12
7
602 Views
Last Modified: 2013-12-16
ls -A -d -1 | xargs -I {} -i ksh "tr -d '/' | tar czfp /tmp/run1/'"{}.tar.gz" '{}'"

this works:
ls -1 | xargs -I {} tar czfp /tmp/run1/'{}.tar.gz" '{}'

but the problem is i only want directories. This will list all contests 1 line at a time, then tar each file into a new tarball. This does not work:

ls -A -d -1 | tr -d '/' | xargs -I {} tar czfp /tmp/run1/"{}.tar.gz" '{}'

ls -A -d -1 | xargs -I {}-i ksh "tr -d '/'; tar czfp /tmp/run1/'$1'.tar.gz '$1'"

0
Comment
Question by:eunit3130
  • 3
  • 3
7 Comments
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
what exactly you want to do?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
Comment Utility
I guess the problem here is :

ls -A -d  -1 does not give list of first level directory names. Replace it with:

find . -type d -print -maxdepth 1 | cut -f2 -d'/'

instead

Cheers,
K.
0
 

Author Comment

by:eunit3130
Comment Utility
i want to ls all the directories and tarball eachone using xargs. not scripting.
So kinda like:

ls -A -d -1 */ |  xargs -I {} tar czfp /tmp/results/'{}.tar.gz' '{}'

the problem is
ls -A -d -1 */  ouputs:
dir1/
dir2/
dir3/

so the variable {} actualy equals the directory name and outputs an error saying

"file tmp/reults/dir1/.tar.gz does not exist" which makes sence since '{}' = dir/

so tr -d '/' would remove the backslash but you cant pipe xargs to tr it would have to be the other way around
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
Comment Utility
try

ls -A -d -1 */ |  awk -F'/' '{ print $1 }' | xargs -I {} tar czfp /tmp/results/'{}.tar.gz' '{}'
0
 

Author Comment

by:eunit3130
Comment Utility
this creates /tmp/run1/{}.tar.gz instead of the directories.
ls -A -d -1 | xargs -I {} tar czfp /tmp/run1/'{}'.tar.gz '{}' | tr -d '/'

Open in new window

0
 

Author Comment

by:eunit3130
Comment Utility
That was the solution but can you explain the
awk -F'/' '{ print $1 }'
portion please? Ill just go ahead and give each reply 250 points
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
awk can process fields and here I put the field separation char as / and ask it to print first field
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now