LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to execute -depth in find command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-execute-depth-in-find-command-803791/)

shravee 04-23-2010 12:00 PM

how to execute -depth in find command
 
hi all,

within my bobje directory, I have many directories and some log files . I want to delete those log files without traversing the subdirectories.

so i performed these commands and getting the mentioned errors.

$ find /optware/boste/xir2sp4/bnbodvg/bobje -name \*.log -maxdepth 0 -exec ls -l {} \;
find: bad option -maxdepth
$ find /optware/boste/xir2sp4/bnbodvg/bobje -name \*.log -depth 0 -exec ls -l {} \;
find: missing conjunction
$ find /optware/boste/xir2sp4/bnbodvg/bobje -name \*.log -depth 0 -exec ls -l `{}` \;
ksh: {}: not found
find: missing conjunction

regards
shravee

MensaWater 04-23-2010 12:44 PM

The missing conjunction usually occurs when it doesn't understand your pattern. Try "*.log" in quotes like that.

pixellany 04-23-2010 02:16 PM

I have learned to ALWAYS quote the search string used with -name.

MensaWater 04-23-2010 02:20 PM

Quote:

Originally Posted by pixellany (Post 3945587)
I have learned to ALWAYS quote the search string used with -name.

Yep Linux seems to almost always require it. Other *NIXes not so much though on occasion they don't like something and quoting helps.

pixellany 04-23-2010 02:27 PM

Quote:

Originally Posted by MensaWater (Post 3945591)
Yep Linux seems to almost always require it. Other *NIXes not so much though on occasion they don't like something and quoting helps.

AFAIK, it's not a Linux vs. *nix thing. The issue is how characters are read by the shell vs. a specific utility.

In BASH, "*" means almost literally "whatever is lying around". This is why:
find <path> -name *stuf
blows up more often than not. Any shell using wildcards is prone to similar issues.

MensaWater 04-23-2010 02:41 PM

You'd think that is the case but I run into it almost always on Linux and only in certain cases on HP-UX. However, I run into it often enough either way that these days I tend to always put quotes around it.

colucix 04-23-2010 05:20 PM

Quote:

Originally Posted by shravee (Post 3945455)
within my bobje directory, I have many directories and some log files . I want to delete those log files without traversing the subdirectories.

Which OS and which version of find are you running? Since it does not accept the -maxdepth option, most likely it's not GNU find.

Regarding the -depth option, it does not accept any argument. It is intended to process the content of a directory before the directory itself, for example
Code:

$ ls ./test
myfile readme
$ find .
.
./test
./test/readme
./test/myfile
$ find . -depth
./test/readme
./test/myfile
./test
.

this is useful when using find in conjunction with some archiving applications, like cpio or tar. Despite its name, this option does not affect how deeply you descend into a directory tree.

I don't know about a simple way to avoid traversing subdirectories in non-GNU find versions, but if your task is to remove log files in /optware/boste/xir2sp4/bnbodvg/bobje, why not simply
Code:

rm /optware/boste/xir2sp4/bnbodvg/bobje/*.log
or am I missing something?

10110111 04-23-2010 05:29 PM

Quote:

You'd think that is the case but I run into it almost always on Linux and only in certain cases on HP-UX. However, I run into it often enough either way that these days I tend to always put quotes around it.
This is just because in linux the default shell is bash (or sometimes sh), while on other systems other shells may be default.

colucix 04-23-2010 05:54 PM

A little add-on. Here is a not simple way to avoid descending into subdirectories in non-GNU versions of find (maybe...):
Code:

find /optware/boste/xir2sp4/bnbodvg/bobje/* -type d -prune -o -name '*.log' -exec ls -l {} \;


All times are GMT -5. The time now is 09:18 AM.