xargs question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting xargs question
# 8  
Old 08-01-2012
Quote:
Originally Posted by Corona688
Code:
find . -maxdepth 1 | xargs -I {} du -hs | while read SIZE FILE
do
        echo "$FILE" "[$SIZE]"
done

Quote:
Originally Posted by stevensw
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.
That's because the xargs invocation isn't passing any argument to du. The fix is to add {} at the end.
Code:
xargs -I {} du -hs {}

Regards,
Alister
# 9  
Old 08-02-2012
If you have GNU Parallel installed:
Code:
find . -maxdepth 1 | parallel du -hs {} | parallel --colsep '\s+' echo {2} [{1}]

You can install GNU Parallel simply by:
Code:
    wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
    chmod 755 parallel
    cp parallel sem

Watch the intro videos for GNU Parallel to learn more:
https://www.youtube.com/playlist?lis...4C9FF2488BC6D1
# 10  
Old 08-02-2012
Not sure why du -h --max=1 wouldn't yield the same result...
# 11  
Old 08-02-2012
Quote:
Originally Posted by tange
If you have GNU Parallel installed:
Code:
find . -maxdepth 1 | parallel du -hs {} | parallel --colsep '\s+' echo {2} [{1}]

In my estimation, the xarg solutions are pointless. Why use xargs to execute a utility once per file when find can do that itself?

That parallel suggestion may improve things a bit, but it will still exec du once per file.

I would expect that throwing the maximum number of arguments at du would outperform both:
Code:
find . -maxdepth 1 -exec du -sh {} + | ...

With this approach, any legal filename, including those with ghastly whitespace, are handled correctly (at least by find and du, though passing along a filename with whitespace could be problematic).

Regards,
Alister
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