Using xargs and pipelining

Posted on 2009-05-12
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'"

Question by:eunit3130
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 40

Expert Comment

ID: 24365533
what exactly you want to do?
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'/'



Author Comment

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:

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 40

Accepted Solution

omarfarid earned 500 total points
ID: 24365799

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

Author Comment

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


Author Comment

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
LVL 40

Expert Comment

ID: 24365869
awk can process fields and here I put the field separation char as / and ask it to print first field

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension ( This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

734 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