Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a script to look for files and not subfolders - use of the ls -m flag 1

Status
Not open for further replies.

bazil2

Technical User
Feb 15, 2010
148
0
0
DE
(Elementary user)

Could anyone please assist with the following ....

I have written a very simple script that evaluates how many files are in a directory.

The problem is that sometimes the files are in sub-folders and these also end up been 'counted' as files.

I suppose it is because I am using the ls -m flag which will produce a list for example:

File01.pdf,File02.pdf,MY_SUBFOLDER,

Is it possible to change my script so that it looks for files recursively in the path and ignores the directories (numberoffiles)?

Best regards


---------------------------------------------------------------
#!/bin/sh
#
#
numberoffiles=`ls -m /path/to/my/files/`
while [ -n "$numberoffiles" ]
do
# run my programme
/usr/my/binary Test
numberoffiles=`ls -m /path/to/my/files/`
done
#
#
# END

---------------------------------------------------------------
 
A single line should do the job:
[tt]numberoffiles=`find /path/to/my/files/ -type f|wc -l`
[/tt]
If you want to cound files in a single directory and not descend into any sub-dir:
[tt]numberoffiles=`find /path/to/my/files/ -maxdepth 1 -type f|wc -l`[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top