Solved

Using xargs and pipelining

Posted on 2009-05-12
7
608 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
ID: 24365533
what exactly you want to do?
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 24365695
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
ID: 24365749
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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 24365799
try

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

Author Comment

by:eunit3130
ID: 24365800
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
ID: 24365828
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
ID: 24365869
awk can process fields and here I put the field separation char as / and ask it to print first field
0

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
add a 1000 ms delay after each sending email operation 12 68
Virtual Box issue 6 82
LINUX, CPANEL & WHM 5 44
Setting up two Raspberry Pi gateways/routers 3 36
Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
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.

930 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

8 Experts available now in Live!

Get 1:1 Help Now