xargs question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting xargs question
# 1  
Old 08-01-2012
xargs question

Suppose your piping a path into xargs and want to print the name along with the total size:

Code:
find . -maxdepth 1 | xargs -I {} echo "{} [`du -hs {} | sed 's/ [[:alnum:][:punct:]]*//g'`]"

This won't work because {} ceases having its special meaning within the ``. How to preserve it? Thanks
Moderator's Comments:
Mod Comment Code tags for code, please!

Last edited by Corona688; 08-01-2012 at 07:04 PM..
# 2  
Old 08-01-2012
I don't think {} ever meant what you thought it did. In xargs' case, it just means "please don't assume quotes or spaces are special characters". {} only means 'file name' to 'find', which you left behind the instant you typed | . Put more pipes after xargs instead of trying to put them inside -- xargs isn't a shell, it can't do that.

Code:
find . -maxdepth 1 | xargs -I {} du -hs | sed 's/ [[:alnum:][unct:]]*//g'

I don't understand the intention behind your sed here, so it may not be quite right. Could you show what you're trying to do with it -- the output from du, and what you're trying to transform it into?
# 3  
Old 08-01-2012
I want to put the file size after the filename instead of before, also inserted between square brackets. I may also want to put the last modified time after the size, not sure. This way I have control instead of just at the mercy of what du is willing to give me.

So basically printing:

Code:
/a/b/c [10K]
/a/b/d [100K]

and so forth

Last edited by Scott; 08-01-2012 at 07:43 PM.. Reason: ...
# 4  
Old 08-01-2012
Quote:
Originally Posted by stevensw
I want to put the file size after the filename instead of before, also inserted between square brackets.
Okay.

Code:
find . -maxdepth 1 | xargs -I {} du -hs | while read SIZE FILE
do
        echo "$FILE" "[$SIZE]"
done

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-01-2012
It printed:

Code:
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
. [35M]
...

a bunch of times, basically printing the cwd and its size each time.

Last edited by Scott; 08-01-2012 at 07:44 PM.. Reason: ...
# 6  
Old 08-01-2012
Try this:

Code:
find . -maxdepth 1 -type d -print0 | xargs -0 -n 1 du -hs | while read SIZE FILE
do
    echo "$FILE" "[$SIZE]"
done

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 08-01-2012
That works thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Xargs

Hi, can anyone tell me in detail ? what the following do in detail ? I am trying to get a largest number in a list Thanks Tao LARGEST=$(echo $* | xargs -n1 | sort -nr | tail -1) (3 Replies)
Discussion started by: ccp
3 Replies

2. Shell Programming and Scripting

Xargs

Hello, I need some help with xargs $ ls aaa bbb ccc ddd$ ls | xargs -I{} ls -la {} -rw-rw-r--. 1 xxx xx 0 May 30 20:04 aaa -rw-rw-r--. 1 xxx xx 0 May 30 20:04 bbb -rw-rw-r--. 1 xxx xx 0 May 30 20:04 ccc -rw-rw-r--. 1 xxx xx 0 May 30 20:04 dddit's possible to have output like this with... (3 Replies)
Discussion started by: vikus
3 Replies

3. Shell Programming and Scripting

Help with xargs

Using the bash shell I'm trying to either create a command for the command line or a script that will show netstat info for a given process name. Here is an example of what I'm trying to do:$ ps aux |grep catalina |grep -v grep | awk '{print $2}' 5132 $ netstat -nlp |grep 5132 (Not all processes... (11 Replies)
Discussion started by: axiopisty
11 Replies

4. Shell Programming and Scripting

xargs

I have dir with many files ( close to 4M ) . $ ls -la total 76392 drwxr-xr-x 10 oracle dba 512 Jun 06 14:39 . drwxr-xr-x 11 oracle dba 512 Dec 20 13:21 .. drwxr-xr-x 2 oracle dba 39074816 Jun 15 14:07 ad I am trying to delete them using... (8 Replies)
Discussion started by: talashil
8 Replies

5. Shell Programming and Scripting

xargs

Dear all , any suggest on xargs to combine from (1.txt and 2.txt) to output.txt ? thanks a lot. 1.txt 0123 BUM-5M BUM-5M 93490481 63839 0124 BUM-5M BUM-5M 112112 ... (3 Replies)
Discussion started by: samoptimus
3 Replies

6. Shell Programming and Scripting

Help with xargs

hi Could any one please tell me the option using which we can run multiple commands using xargs I have list of files, I want to run dos2unix and chmod at one shot on them I tried google n searched man pages but couldnt really find the solution , please help right now im doing this ls... (4 Replies)
Discussion started by: sunilmenhdiratt
4 Replies

7. Shell Programming and Scripting

Using xargs

hi i just want to know that how do we use xargs command to find files which are greater than specified memory in a given directory (6 Replies)
Discussion started by: sumit the cool
6 Replies

8. UNIX for Advanced & Expert Users

xargs -P

I discovered that GNU's xargs has a -P option to allow its processes to run in parallel. Great! Is this a GNU thing, or is it supported by other platforms as well? (4 Replies)
Discussion started by: otheus
4 Replies

9. Shell Programming and Scripting

why we use xargs..

hi , can anyone help me by saying why we use xargs.. is it acing like a place holder..? thanks, Krips. (3 Replies)
Discussion started by: kripssmart
3 Replies

10. UNIX for Dummies Questions & Answers

Question concerning xargs

Hello... I happened upon this site when searching for an answer, and I consider myself a UNIX dummy, so here I am! Here's my issue: I'm trying to use xargs to strip blank lines out of a group of files and copy the output to another directory, and I just can't figure out the syntax. Here's what... (4 Replies)
Discussion started by: cp27316
4 Replies
Login or Register to Ask a Question