• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 646
  • Last Modified:

Using xargs and pipelining

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
eunit3130
Asked:
eunit3130
  • 3
  • 3
1 Solution
 
omarfaridCommented:
what exactly you want to do?
0
 
Kerem ERSOYPresidentCommented:
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
 
eunit3130Author Commented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
omarfaridCommented:
try

ls -A -d -1 */ |  awk -F'/' '{ print $1 }' | xargs -I {} tar czfp /tmp/results/'{}.tar.gz' '{}'
0
 
eunit3130Author Commented:
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
 
eunit3130Author Commented:
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
 
omarfaridCommented:
awk can process fields and here I put the field separation char as / and ask it to print first field
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now