Find command takes long


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find command takes long
# 1  
Old 07-12-2015
Hammer & Screwdriver Find command takes long

Hi,

I am trying to search for a Directory called "mont" under a directory path "/opt/app/var/dumps"

Although "mont" is in the very parent directory called "dumps" i.e "/opt/app/var/dumps/mont" and it can never be inside any Sub-Directory of "dumps"; my below find command which also checks for permissions on the directory "mont" however takes more than 2 hrs.

Code:
find /opt/app/var/dumps -type d -name mont -perm 600

Is there a way we can only consider the directory structure in the search while excluding the files.

How can we quicker the search result ?

Last edited by mohtashims; 07-12-2015 at 06:19 AM..
# 2  
Old 07-12-2015
No! How do you suppose find determines which files it finds in a file hierarchy search are directories as opposed to some other type of file? It reads all of the directory entries in each directory it finds and then uses stat() (or a similar system call) to determine the type of that file. The find utility has no way to magically ignore non-directory files before it determines whether or not that file is a directory.

However, if find does encounter any directory that is mode 600, it won't be able to examine any files in or under that directory.

If you know that the pathname to the directory you want to process is /opt/app/var/dumps/mont, why are you using find instead of something like:
Code:
ls -ld /opt/app/var/dumps/mont

?
# 3  
Old 07-12-2015
Here is another approach without using find:-
Code:
#!/bin/ksh

dir="/opt/app/var/dumps/mont"

os=$(uname)

if [ -d "$dir" ]
then
    
        [ "$os" = "Linux" ] && mod=$( stat --printf="%a" "$dir" ) || mod=$( perl -e'printf "%o",(stat shift)[2] & 07777' "$dir" )

        [ "$mod" = "600" ] && print "$dir"
fi

# 4  
Old 07-12-2015
Quote:
Originally Posted by Don Cragun
No! How do you suppose find determines which files it finds in a file hierarchy search are directories as opposed to some other type of file? It reads all of the directory entries in each directory it finds and then uses stat() (or a similar system call) to determine the type of that file. The find utility has no way to magically ignore non-directory files before it determines whether or not that file is a directory.

However, if find does encounter any directory that is mode 600, it won't be able to examine any files in or under that directory.

If you know that the pathname to the directory you want to process is /opt/app/var/dumps/mont, why are you using find instead of something like:
Code:
ls -ld /opt/app/var/dumps/mont

?
The reason i am using find instead ls ls -ld is becoz along with making sure mont exists under dump folder i should be able to check permissions on mont folder
# 5  
Old 07-12-2015
Code:
find /opt/app/var/dumps/mont -prune -type d -perm 600 2>/dev/null

But I share the feeling there might be a better approach - if we knew the context.
# 6  
Old 07-12-2015
And if I use ls -ld for files that do not exist and for files that do exist, as in:
Code:
$ ls -ld /opt/app/var/dumps/mont
ls: /opt/app/var/dumps/mont: No such file or directory
$ ls -ld .
drwxr-xr-x@ 211 dwc  staff  7174 Jul 12 10:10 .
$

it clearly shows me that /opt/app/var/dumps/mont does not exist on my machine and clearly shows me that . does exist and shows me its mode. On the other hand, if I run the command:
Code:
$ find . -type d -name pdf -perm 600
$

I only know that there is no directory named pdf with mode 600 in the file hierarchy rooted in my current directory, while the command:
Code:
$ ls -ld pdf
drwxr-xr-x@ 14 dwc  staff  476 Aug 25  2013 pdf
$

clearly shows me that there is a directory named pdf in my current directory and that it has mode 755.

And, all of the above commands (except the find command) take less than a second.

So, less than a second to get the exact information you want versus more than two hours to maybe get the information you want. Tell me again why:
Code:
ls -ld /opt/app/var/dumps/mont

is a bad choice here to find out if there is a file named /opt/app/var/dumps/mont and, if there is, what type of file it is and what mode it has???
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script takes too long to complete

Hi, I have a lengthy script which i have trimmed down for a test case as below. more run.sh #!/bin/bash paths="allpath.txt" while IFS= read -r loc do echo "Working on $loc" startdir=$loc find "$startdir" -type f \( ! -name "*.log*" ! -name "*.class*" \) -print | while read file do... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. UNIX for Advanced & Expert Users

Find command takes too long to complete

Hi, Below is my find command find /opt/app/websphere -name myfolder -perm -600 | wc -l At time it even takes 20 mins to complete. my OS is : SunOS mypc 5.10 Generic_150400-09 sun4v sparc SUNW,T5440 (10 Replies)
Discussion started by: mohtashims
10 Replies

3. Shell Programming and Scripting

Timeout to abolish ssh connection command it takes too long

Hi, I am running a ssh connection test in a script, how can I add a timeout to abolish the process if it takes too long? ssh -i ~/.ssh/ssl_key useraccount@computer1 Thank you. - j (1 Reply)
Discussion started by: hce
1 Replies

4. Shell Programming and Scripting

sort takes a long time

Dear experts I have a 200MG text file in this format: text \tab number I try to sort using options -fd and it takes very long! is that normal or I can speed it up in some ways? I dont want to split the file since this one is already splitted. I use this command: sort -fd file >... (12 Replies)
Discussion started by: voolek
12 Replies

5. UNIX for Dummies Questions & Answers

time how long it takes to load a module

Hello, like the title says, how can i measure the time it takes to load a module in Linux, and how how can i measure the time it takes to load a statically compiled module. /Best Regards Olle ---------- Post updated at 01:13 PM ---------- Previous update was at 11:54 AM ---------- For... (0 Replies)
Discussion started by: ollebanan
0 Replies

6. Shell Programming and Scripting

find command takes a lot of time ( can I skip directories)

I have a file called "library" with the following content libnxrdbmgr.a libnxrdbmgr.so libnxtk.a libnxtk.so libora0d_nsc_osi.so I am trying to locate if these libraries are on my machine or not. find command runs for about few seconds and hangs after this. Can someone please help me and... (3 Replies)
Discussion started by: knijjar
3 Replies

7. Shell Programming and Scripting

Killing a process that takes too long

Hello, I have a C program that takes anywhere from 5 to 100 arguments and I'd like to run it from a script that makes sure it doesnt take too long to execute. If the C program takes more than 5 seconds to execute, i would like the shell script to kill it and return a short message to the user. ... (3 Replies)
Discussion started by: WeezelDs
3 Replies

8. Programming

fwrite takes extremely long time

After my previous thread, I think I found out what causes the long delays. I run this program on several Linux computers, and the sometimes (after the file with the arrays becomes big) the fwrite takes between 100 ms to 900 ms. This is very bad for me, as I want a timer to halt each 30 ms.... ... (5 Replies)
Discussion started by: inna
5 Replies

9. Shell Programming and Scripting

command find returned bash: /usr/bin/find: Argument list too long

Hello, I create a file touch 1201093003 fichcomp and inside a repertory (which hava a lot of files) I want to list all files created before this file : find *.* \! -maxdepth 1 - newer fichcomp but this command returned bash: /usr/bin/find: Argument list too long but i make a filter all... (1 Reply)
Discussion started by: yacsil
1 Replies

10. UNIX for Dummies Questions & Answers

Finding out how long a command takes to run

Hi I am trying to find out the best way to find out how long a command takes to run in miliseconds .. Is there such a way of doing this in Unix ? Thanks (3 Replies)
Discussion started by: cfoxwell
3 Replies
Login or Register to Ask a Question