Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Find commmand returning search path with the result set Post 302948457 by bakunin on Monday 29th of June 2015 07:16:31 PM
Old 06-29-2015
I may shed light onto some of your troubles:

Code:
## BTW.. Following command will work even without -print option

$ find /tmp/test3 -print

Yes, because "-print" is the default clause if no other one is given. When you write it out you just manually recreate the default. I suppose it is in the man page, it is definitely on the POSIX description:

Quote:
If no expression is present, -print shall be used as the expression. Otherwise, if the given expression does not contain any of the primaries -exec, -ok, or -print, the given expression shall be effectively replaced by:

( given_expression ) -print

Code:
$ find /tmp/test3 -exec rm -fr {} \; 
find: `/tmp/test3': No such file or directory

Yes, look at the output of your command when you used the "-print" instead of "-exec": the first directory to be listed was - "/tmp/test3", yes? Now, what string do you suppose has gone first into "rm -rf", hmmm? What will a rm -rf /tmp/test3 do to a file /tmp/test3/file or to a directory /tmp/test3/directory or to a file /tmp/test3/directory/file in it?

What you see is "find" being a bit dumbfounded by the fact that the directory structure it was just starting to read is already gone. Your prompt shows "/tmp/test3" being the current directory, but this is just because the shell hasn't noticed now that the directory is already gone. Do a "cd *" and you will probably find that "/tmp/test doesn't exist any more.

Quote:
If you have a symbolic link under /tmp/test3 that points to anywhere else, then find may follow it and do untold damage.
This is absolutely true and a valuable advice you should take to heart! It might not be necessary because of the reasons above - before find could even try to follow a link it will already be long gone - but beware once you got your command halfways correct and it starts actually doing what you mean (which might not be what you want).

Fortunately there is a remedy:

Code:
find /tmp/test3/* -prune ! -type l -exec rm -rf {} \;

Because you use rm -r you do not need to descend the directory structure at all, the first layer will suffice. All links are filtered out before find gets to the dangerous stuff.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

find command not returning any result

I am looking for all the header files (*.h).. which as per documentation of the UNIX system shouldbe there. I am using find / -name *.h -print But it does't give anything. My question is under what condition the "find" condition will fail to find the file? What is the work around. ... (4 Replies)
Discussion started by: rraajjiibb
4 Replies

2. UNIX for Dummies Questions & Answers

Question about Restricting Search path of FIND to current directory

Hi, By default FIND command searches for matching files in all the subdirectories within the specified path. Is there a way to restrict FIND command's search path to only the specified directory and NOT TO scan its subdirectories. Any help would be more than appreciated. Thanks and Regards (2 Replies)
Discussion started by: super_duper_guy
2 Replies

3. AIX

The shell script is not returning proper result

Can anybody pls look into this script and tell me where I went wrong. After running this script, it is showing like "Trying to overlay current working directory ABORT!!!" :-( ARGCNT=$# if then echo "Two parameters are needed for this shell " echo "Please try again with... (1 Reply)
Discussion started by: clnsharma123
1 Replies

4. UNIX for Dummies Questions & Answers

how to use find commnad to show only path of the result

Hello all say i like to find files i do : find . -name "*.txt" but if i like to find ( and print out ) only the path's where the files are ( the *.txt files ) what can i add to the find command ? (1 Reply)
Discussion started by: umen
1 Replies

5. UNIX for Dummies Questions & Answers

PATH set but I can't find where!!!!

Hi, Can anybody help with this? When I log into my user account on my box via ssh and then instantly perform an env command I see that my path has been set as follows: PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin My user account uses the ksh shell. In my home directory there is no... (7 Replies)
Discussion started by: Donkey25
7 Replies

6. UNIX for Advanced & Expert Users

transporting scripts onto sloaris, executing it and returning the result to windows

Hi, I have to write a windows XP program, that would generate a Solaris Script, which would then be transported to Solaris, executed, the execution result then needs to be returned to the XP program. For transporting the file i was thinking of following FTP (admins rejected it) or File share on... (1 Reply)
Discussion started by: 00262881
1 Replies

7. Shell Programming and Scripting

os.path.isdir is always returning false

Just started with learning python and tried something, most people here would call more than simple. I just wanted to list all directories within my main directory. So I user the following code: #!/usr/bin/env python import os if os.path.isdir("/home/testaccaunt/public_html"): ... (8 Replies)
Discussion started by: medic
8 Replies

8. Shell Programming and Scripting

How to find duplicate commands in the search path?

I wanted to see if there is any duplicate of a specific command in the command search path. The following code will list all copies of "openssl" in the command search path. find `printenv PATH | sed "s/:/ /g"` -maxdepth 1 -name openssl However, the above code would fail if the search path... (9 Replies)
Discussion started by: LessNux
9 Replies

9. Shell Programming and Scripting

bash: How to reuse the search result of "find"

find . -type f -print0 | xargs -0 chmod 600 find . -type f On bash, I would like to pass the search result of "find" to another command as well as to the standard output. The above code performs the same search twice -- once for "xargs -0 chmod" and another for stdout. I would like to... (5 Replies)
Discussion started by: LessNux
5 Replies

10. Shell Programming and Scripting

Find a file and set path in variable?

Hi Folks - I was wondering if you could help convert batch code in Linux? For instance, I use the following piece of code in DOS to find a file/executable, and then the FULL path as a variable. ::-- If startMaxl.exe exists, set full path --:: for %%D in (c d e f g h i j k l m n o p q r s t... (4 Replies)
Discussion started by: SIMMS7400
4 Replies
All times are GMT -4. The time now is 09:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy