Solved

Using xargs and pipelining

Posted on 2009-05-12
7
609 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

777 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