Visit Our UNIX and Linux User Community

find with prune option help needed

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find with prune option help needed
# 1  
Old 01-03-2012
find with prune option help needed

Hello, I am using ksh93 (/usr/dt/bin/dtksh) on Solaris and am stuck when trying to use find with the -prune option.

I need to search a directory (supplied in a variable) for files matching a certain pattern, but ignore any sub-directories.

I have tried:
find ${full_path_to_dir_to_search} -type f -name "*.dat" -print -o -type d -prune

which produces no output. If I cd to that directory and issue this command:
find * -type f -name "*.dat" -print -o -type d -prune

it works as expected since it is working on a list of filenames.

Why doesn't it work when using a full path? I suspect it is being omitted by the -type d -prune option since I am using a full path? If so, how else to do it?

Oh and the sub-directories could change so I can't exclude them specifically in the find command.

Thanks for your consideration,

Last edited by gary_w; 01-03-2012 at 05:52 PM.. Reason: added more info
# 2  
Old 01-03-2012
Hi gary_w,

As I understand, the option you are looking for is:
-maxdepth 1

which doesn't descend into directories.

Substitute '-prune' with '-maxdepth 1', and an absolute path doesn't mind.

This User Gave Thanks to birei For This Post:
# 3  
Old 01-03-2012
This version of find does not appear to have the maxdepth option. I will study the man page for an alternative hopefully.
# 4  
Old 01-03-2012
Using "-prune" is essentialy the same as using "-maxdepth 1"

I'm assuming "full_path_to_dir_to_search" already contains a splat in order to create the list?

find ${full_path_to_dir_to_search}/* -type f -name "*.dat" -print -o -type d -prune

This User Gave Thanks to verdepollo For This Post:
# 5  
Old 01-03-2012
No it is a full pathname. Note that using a period while in the directory does not work either (no output):
find . -type f -name "*.dat" -print -o -type d -prune

where this does work (my test file is found):
find * -type f -name "*.dat" -print -o -type d -prune

I assume because the period is a directory and the splat is a list of files?
# 6  
Old 01-03-2012
I think the problem is that you're using -prune with a full path which behaves as if you were using a dot.

So it means it will go 0 levels down looking for the files. If you want the command to go down just one level you have to explicitly "add one more level" by appending "/*" to the path.

.       = go down 0 levels
./*     = go down 1 level
./*/*   = go down 2 levels

This User Gave Thanks to verdepollo For This Post:
# 7  
Old 01-03-2012
One way of achieving the effect of "-maxdepth 1" is to work from the directory above the one you are actually interrogating. Then apply the "-prune" to stop find searching deeper.

if [ "${DIR}""X" = "X" ]
if [ ! -d "${DIR}" ]
        echo "${PN}: Directory missing: ${DIR}"
# Processing starts here
echo "Directory: ${DIR}"
DIRA=`basename "${DIR}"`        # Directory name
cd "${DIR}"
echo "DIRA=${DIRA}"
find ../"${DIRA}" \( ! -name "${DIRA}" -prune \) -type f -print | sort | \
while read FILENAME
        FILENAME2=`basename "${FILENAME}"`
        echo "${FILENAME2}"

These 2 Users Gave Thanks to methyl For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #885
Difficulty: Easy
Denial of service attacks (DoS) are designed to make a machine or network resource unavailable to its intended users.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Prune Option for Find Command on AIX

I need to delete all files from the working directory and its sub directories using the find command, for that I am using -prune option but some how I am having a syntax issue. I have tried the below, please help me correct the syntax find . -name \* -type f -exec rm -f {} \; >> Works but... (4 Replies)
Discussion started by: rosebud123
4 Replies

2. Shell Programming and Scripting

Using prune with find

Hi, I have two files under two separate directories as in: find . -name ./ ./abc/ I want my find to only look for the file that is under the current directory and not one under /abc How do I use prune to achieve this? I am on AIX (3 Replies)
Discussion started by: swasid
3 Replies

3. UNIX for Dummies Questions & Answers

AIX find command using prune option

Hi, I am trying to find some files in a directory and then remove/list them if they are 30 days old. I also have 2 directories in that directory which I need to skip. Can someone please tell me what is the correct syntax? find /developer/. -name "lost+found" "projects" -prune -o -type f... (2 Replies)
Discussion started by: tkhan9
2 Replies

4. Shell Programming and Scripting

recently introduced to the newer option for find...does an older option exist?

To find all the files in your home directory that have been edited in some way since the last tar file, use this command: find . -newer backup.tar.gz Is anyone familiar with an older solution? looking to identify files older then 15mins across several directories. thanks, manny (2 Replies)
Discussion started by: mr_manny
2 Replies

5. Shell Programming and Scripting

help with find command and prune option

Hi I have a directory say mydir and inside it there are many files and subdirectories and also a directory called lost+found owned by root user I want to print all files directories and subdirectorres from my directory using find command except lost+found If i do find . \( -name... (3 Replies)
Discussion started by: xiamin
3 Replies

6. Shell Programming and Scripting

find with prune option

Hi, I want to list files only from the current dir and its child dir (not from child's child dir). i have the following files, ./ABC/1.log ./ABC/2.log ./ABC/ABC1/A.log ./ABC/ABC1/B.log ./ABC/ABC1/XYZ/A1.log ./ABC/ABC1/XYZ/A2.log Here i want to list only the log file from current... (1 Reply)
Discussion started by: apsprabhu
1 Replies

7. UNIX for Dummies Questions & Answers

help me out with find command , -prune option

Hi , Kindly help me out .:) i want to find only the file t4 in directory t3. i am in dir t . the tree is as follows. if i give, find . o/p is . ./t4 ./t1 ./t1/t2 ./t1/t2/t3 ./t1/t2/t3/t4 ./t1/t2/t4 ./t1/t4 directories are like t/t1/t2/t3 and each directory has file t4. my... (7 Replies)
Discussion started by: bhuvaneshlal
7 Replies

8. Linux

doubt in -prune option

i want to find only the file t4 in directory t3. i am in dir t . the tree is as follows. if i give, find . o/p is . ./t4 ./t1 ./t1/t2 ./t1/t2/t3 ./t1/t2/t3/t4 ./t1/t2/t4 ./t1/t4 directories are like t/t1/t2/t3 and each directory has file t4. my question is , i want to find file... (0 Replies)
Discussion started by: bhuvaneshlal
0 Replies

9. Solaris

correct usage of find's -prune option

I know one of the more seasoned veterans probably opened this thread looking for their chance to refer me to the site's search feature and let me tell you. I'VE LOOKED!!!! And I didn't find anything helpful... So, I've got a windows background and I'm fond of its search feature which comes... (6 Replies)
Discussion started by: ProGrammar
6 Replies

10. Shell Programming and Scripting

finding files using prune option

Hi All, I am trying to find files in a directory and don't want to search in the sub directories and using the command find . \( ! -name . -prune \) -mtime +1 -name '*.log' and is working fine. But when I am trying with absolute path then is not working like find /home/subodh \( ! -name... (1 Reply)
Discussion started by: subodh.sharma
1 Replies

Featured Tech Videos