Find commmand returning search path with the result set


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find commmand returning search path with the result set
# 1  
Old 06-29-2015
Find commmand returning search path with the result set

OS Platform : Oracle Linux 6.5

We are creating a shell script to purge old log files . It uses find command with rm in it.

The syntax is

find <Path of Log Directory> -exec rm -fr {} \;

Example:

find /tmp/test3 -exec rm -fr {} \;


For rm command , we use -r option to remove directories because it is not just files we want to delete. We have to purge directories too. Because sometimes our application creates directories for certain type of issues. For example, when the application crashes, it creates a 'Core Dump' directory by which several files are clubbed together and placed in a coreDump directory . Example coreDump_29_June_2015_11_05_32.

But, I have noticed that, when we try to search for files and directories in the search path, find command is returning the search path too in the result set. For example , I am trying to find all files and directories within /tmp/test3 directory . But, find is returning "/tmp/test3" in the result set too (shown in red below )

Since find is falsely returning the search path itself, rm is trying to remove it. But, rm is trying to delete a directory called
"/tmp/test3" within /tmp/test3 . ie. rm /tmp/test3/tmp/test3 which doesn't exist. So, it is harmless. But, how can I prevent the find command from returning the search path itself (the line shown in red below).

Code:
$ mkdir test3
$
$
$ cd test3
$
$ pwd
/tmp/test3
$ touch abc{1,2,3}.txt
$

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

$ find /tmp/test3 -print
/tmp/test3
/tmp/test3/abc2.txt
/tmp/test3/abc3.txt
/tmp/test3/abc1.txt

$ find /tmp/test3 -exec rm -f {} \;
rm: cannot remove `/tmp/test3': Is a directory
$
$ ls
$
$ ls -lt
total 0
$
$ find /tmp/test3 -exec rm -fr {} \;
find: `/tmp/test3': No such file or directory
$
$ ls
$
$ pwd
/tmp/test3

# 2  
Old 06-29-2015
Code:
rm: cannot remove `/tmp/test3': Is a directory

fails not because a directory tmp/test3 doesn't exist in /tmp/test3 (it is an absolute path, see the leading "/") but because you skipped the -r option.

---------- Post updated at 16:07 ---------- Previous update was at 16:03 ----------

And, you may want to give
Code:
find /tmp/test3/* -print

a shot.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 06-29-2015
Thank You Rudic.
I did try -r as shown in my first post
Code:
$ find /tmp/test3 -exec rm -fr {} \; 
find: `/tmp/test3': No such file or directory

# 4  
Old 06-29-2015
WARNING!

If you have a symbolic link under /tmp/test3 that points to anywhere else, then find may follow it and do untold damage.

Do not risk testing it.


You can limit the damage a little if you use the -xdev flag on the find command, but that just stops is jumping out the file-system.



Robin
This User Gave Thanks to rbatte1 For This Post:
# 5  
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:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question