Piping results of 'ls' to 'find' using 'xargs'


 
Thread Tools Search this Thread
Operating Systems Solaris Piping results of 'ls' to 'find' using 'xargs'
# 1  
Old 08-21-2009
Piping results of 'ls' to 'find' using 'xargs'

I'm trying to get a count of all the files in a series of directories on a per directory basis. Directory structure is like (but with many more files):

/dir1/subdir1/file1.txt
/dir1/subdir1/file2.txt
/dir1/subdir2/file1.txt
/dir1/subdir2/file2.txt
/dir2/subdir1/file1.txt
/dir2/subdir2/file1.txt

So, I want my output to be something like:

dir1
4
dir2
2

To achieve this I tried:

ls -d1 dir* | xargs -n 1 find {} -type f | wc -l

but I get the following error message:

find: bad option dir1
find: path-list predicate-list
...

It appears that find is interpreting the directory passed by xargs as an option rather than a path-list.

Can anyone explain why please?

Thanks for taking the time to look.
# 2  
Old 08-21-2009
Try this:
Code:
ls -d1 dir* | xargs -I {} find {} -type f | wc -l

# 3  
Old 08-21-2009
Thanks "jlliagre" that makes it work.

However, seems I'm barking up the wrong tree as what I've done pipes ALL files in ALL directories to wc

In other words, it does the same as:

Code:
find dir1 dir2 -type f | wc -l

When what I really want is:

Code:
 
find dir1 -type f | wc -l; find dir2 -type f | wc -l



---------- Post updated at 11:57 AM ---------- Previous update was at 11:24 AM ----------

Somebody showed me how to do a for loop and I came up with this, which does what I wanted:

Code:
 
for dir in `ls -d dir*`; do echo $dir; find $dir -type f | wc -l; done

Thanks to all those who had a look.
# 4  
Old 08-21-2009
Might be slightly simplified that way:

Code:
for dir in dir*; do echo $dir; find $dir -type f | wc -l; done

# 5  
Old 08-21-2009
Another way:

Code:
#  find . | nawk -F"/" 'NF>2{t[$2]++}END{for (x in t){print x,t[x]}}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Piping commands using xargs

Need help in piping commands using xargs I have several .tar.gz files that I need to list the folder content in a subdirectory. For example, a.tar.gz b.tar.gz c.tar.gz The following command works great for each .tar.gz file but it's a pain to run the tar command for each file. tar -tf... (11 Replies)
Discussion started by: april
11 Replies

2. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

3. Shell Programming and Scripting

Xargs + Find Help

Guys i want to run a command to list all directories that havn't been modified in over 548 days ( 1.5 yrs ). Id like to run a script to first print what the command finds ( so i get a list of the files pre move ... i have a script set for this : find /Path/Of\ Target/Directory/ -type d -mtime... (4 Replies)
Discussion started by: modulartention
4 Replies

4. Shell Programming and Scripting

piping problem with xargs

I'm trying to pipe the output from a command into another using xargs but is not getting what I want. Running this commands: find . -name '33_cr*.rod' | xargs -n1 -t -i cut -f5 {} | sort -k1.3n | uniq | wc -l give the following output: cut -f5 ./33_cr22.rod cut -f5 ./33_cr22.rod ... 9224236... (7 Replies)
Discussion started by: ivpz
7 Replies

5. Shell Programming and Scripting

Display find results, and pipe to xargs

I have an overnight script which runs across a large directory to repair permissions and ownership. I also have this command output the list of files affected so that cron can email these as a log file. Previously I had the command in the form: find /path/to/files -not -user myname -print -exec... (4 Replies)
Discussion started by: mij
4 Replies

6. Shell Programming and Scripting

find and xargs

hi, i've been trying to figure this weird error but I cannot seem to know why. I am using below find command: find . \( ! -name . -prune \) -type f -mtime +365 -print The above code returns no file because no files are really more then 365 days old. However, when I use xargs, its... (9 Replies)
Discussion started by: The One
9 Replies

7. UNIX for Dummies Questions & Answers

XARGS and FIND together

I am trying to delete files older than 60 days from a folder: find /myfolder/*.dat -mtime +60 -exec rm {} \; ERROR - argument list too long: find I can't just give the folder name, as there are some files that I don't want to delete. So i need to give with the pattern (*.dat). I can... (3 Replies)
Discussion started by: risshanth
3 Replies

8. Shell Programming and Scripting

find | xargs cat

Hi, I am having trouble getting a combination of commands to work. I need to traverse through all sub-directories of a certain directory and 'cat' the contents of a particular file in the sub-directories. The commands on their own work but when I combine them I get no output. The... (4 Replies)
Discussion started by: DownunderDave
4 Replies

9. UNIX for Dummies Questions & Answers

use of xargs and prune piping with find command.

Can anyone interpret and tell me the way the below command works? find * -name "*${msgType}" -mtime +${archiveDays} -prune -type f -print 2>/dev/null | xargs rm -f 2> /dev/null Please tell me the usage of prune and xargs in the above command? Looking forward your reply. Thanks in... (1 Reply)
Discussion started by: venkatesht
1 Replies

10. Shell Programming and Scripting

String substitution on find results inside exec/xargs

What I'm trying to do is perform a copy, well a ditto actually, on the results of a find command, but some inline string substitution needs to happen. So if I run this code find ./ -name "*.tif" I get back these results. .//1234567.tif .//abcdefg.tif Now the action from exec or xargs I... (2 Replies)
Discussion started by: myndcraft
2 Replies
Login or Register to Ask a Question