HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Execute "find" only once
Operating System - HP-UX
1832273
Members
2039
Online
110041
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2007 02:00 PM
12-18-2007 02:00 PM
Here is my scenario: I could have 1M files in a directory. I need to report that there are more than 0 files in that directory. Basically, I don't want to parse through all the files. I want to quit as soon as I hit first file. I was trying to do the following:
for i in `find . -name "*.bin"`
do
echo $i
#exit after first occurrence.
exit
done
I think in above example, find will only return after it's done parsing through all the files. I need to find out some command that will exit after one try and doesn't parse through the entire directory.
I also tried following but didn't work:
for i in `find . -name "*.bin" -exec exit \;`
do
exit
done
for i in `find . -name "*.bin"`
do
echo $i
#exit after first occurrence.
exit
done
I think in above example, find will only return after it's done parsing through all the files. I need to find out some command that will exit after one try and doesn't parse through the entire directory.
I also tried following but didn't work:
for i in `find . -name "*.bin" -exec exit \;`
do
exit
done
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2007 02:09 PM
12-18-2007 02:09 PM
Re: Execute "find" only once
# ls -1 *.bin | wc -l > /dev/null && exit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2007 02:45 PM
12-18-2007 02:45 PM
Re: Execute "find" only once
Hi:
If you want to consider only files in a directory (without recursively descending any subdirectories), use 'ls' and skip the "total" line and the '.' and '..' directories that a root user will return, but include "hidden" files beginning with a ".". Capture the file count and compare as you please:
# ls -al /path|awk '!/^total/ && $1~/^-/ && $NF !~/^\.\.?$/'|wc -l
Regards!
...JRF...
If you want to consider only files in a directory (without recursively descending any subdirectories), use 'ls' and skip the "total" line and the '.' and '..' directories that a root user will return, but include "hidden" files beginning with a ".". Capture the file count and compare as you please:
# ls -al /path|awk '!/^total/ && $1~/^-/ && $NF !~/^\.\.?$/'|wc -l
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-18-2007 04:40 PM
12-18-2007 04:40 PM
Solution
This seems to be crude but effective:
dy # cat /misc/kill_parent.sh
#!/bin/sh
kill $PPID
dy # find /usr -name 'n*' -exec echo {} \; -exec /misc/kill_parent.sh \;
/usr/bin/netstat
Terminated
dy #
> I could have 1M files in a directory.
Some might say that this is not a sign of a
good design.
dy # cat /misc/kill_parent.sh
#!/bin/sh
kill $PPID
dy # find /usr -name 'n*' -exec echo {} \; -exec /misc/kill_parent.sh \;
/usr/bin/netstat
Terminated
dy #
> I could have 1M files in a directory.
Some might say that this is not a sign of a
good design.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP