Some advice please on non-recursive find


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Some advice please on non-recursive find
# 1  
Old 03-29-2012
Some advice please on non-recursive find

Hi all,

I am currently writing a find-and-remove kind of script that is to be used for Solaris and Linux.

Currently am using the find command below that I is in find only current directory (universal) | commandlinefu.com

Quote:
find /u01/tmp/* \( ! -name . -prune \) \( -type f -name "*.tmp" -mtime +10 \)
This however gives me the "ksh: /bin/find: arg list too long" error on Solaris and "ksh: find: /usr/bin/find: cannot execute [Argument list too long]" error in Linux.

Using the command below works but it is doing a recursive find which is not what I wanted to have

Quote:
find /u01/tmp -name '*.tmp' -mtime +10 -exec ls {} \;
How do I make "find /u01/tmp -name '*.tmp' -mtime +10 -exec ls {} \;" non-recursive? I can't used maxdepth as the find in Solaris does not recognize that option.

Going back to the first find that I am currently using. I noted that find only current directory (universal) | commandlinefu.com says "you must be in the current directory".

So I tried that, now I am doing

Quote:
cd /u01/tmp
find . \( ! -name . -prune \) \( -type f -name "*.tmp" -mtime +10 \)
Now am not getting the list too long anymore but problem is the list is now coming up with having ./ in the beginning. Sample list below:

Code:
./arc_maint_20147_5.tmp
./arc_maint_20150_5.tmp
./arc_maint_20167_8.tmp
./arc_maint_20168_2.tmp
./arc_maint_2019_8.tmp
./arc_maint_20262_1.tmp
./arc_maint_20264_7.tmp
./arc_maint_20272_c.tmp
./arc_maint_20338_2.tmp
./arc_maint_20388_6.tmp
./arc_maint_20398_2.tmp
./arc_maint_20425_6.tmp
./arc_maint_20480_9.tmp
./arc_maint_20509_7.tmp

Now I change my find to do

Code:
find . \( ! -name . -prune \) \( -type f -name "*.tmp" -mtime +10 \) | sed 's#^./##'

It gets rids of the ./ ... but ... I want to be able to have the listing to include the present working directory. How do I do that? Ideally, I want the listing to look like the one below:

Code:
/u01/tmp/arc_maint_20147_5.tmp
/u01/tmp/arc_maint_20150_5.tmp
/u01/tmp/arc_maint_20167_8.tmp
/u01/tmp/arc_maint_20168_2.tmp
/u01/tmp/arc_maint_2019_8.tmp
/u01/tmp/arc_maint_20262_1.tmp
/u01/tmp/arc_maint_20264_7.tmp
/u01/tmp/arc_maint_20272_c.tmp
/u01/tmp/arc_maint_20338_2.tmp
/u01/tmp/arc_maint_20388_6.tmp
/u01/tmp/arc_maint_20398_2.tmp
/u01/tmp/arc_maint_20425_6.tmp
/u01/tmp/arc_maint_20480_9.tmp
/u01/tmp/arc_maint_20509_7.tmp

At the moment, I re-direct the output to a file, then use a for-loop to read each line and append the directory path. Painful and a long process but suits me for the time being.

I tried using awk below and it fails

Code:
find . \( ! -name . -prune \) \( -type f -name "*.aud" -mtime +10 \) | sed 's#^./##' | awk '{sub(/^/, "`pwd`")};1'
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

Tried using sed below

Code:
find . \( ! -name . -prune \) \( -type f -name "*.aud" -mtime +10 \) | sed 's#^./##' | sed 's#^#`pwd`#'

But am getting the string `pwd` instead of the output from running the pwd command.

FYI, reason am wanting to have the full path is to be safe that am doing the rm of the right file since I can't undo the rm.

Any advice will be much appreciated. Thanks in advance.
# 2  
Old 03-29-2012
maybe I'm missing your point, but if you don't want to recurse, then don't use 'find'. If your favorite shell's filename expansion is too weak to let you use complex regular expressions, then use 'echo *' or 'ls' and pipe to your favorite 'grep'?
# 3  
Old 03-30-2012
Quote:
Originally Posted by Yeaboem
maybe I'm missing your point, but if you don't want to recurse, then don't use 'find'. If your favorite shell's filename expansion is too weak to let you use complex regular expressions, then use 'echo *' or 'ls' and pipe to your favorite 'grep'?
Hi,

Thanks for your response.

I need to use find 'coz I need to work on files that are x-number of day's old? Can I achieve the same with just using echo, ls and pipe?
# 4  
Old 03-30-2012
awk

Hi,

Try this one,

Code:
find . \( ! -name . -prune \) \( -type f -name "*.aud" -mtime +10 \) | awk -v base=`pwd` '{gsub(/^./,base);}1'

Note: I am tested your find command. If it works well, awk command do the rest.

Cheers,
RangaSmilie
# 5  
Old 03-30-2012
Using sed :
Code:
find . \( ! -name . -prune \) \( -type f -name "*.aud" -mtime +10 \) | sed "s#^./#$PWD/#g"

# 6  
Old 03-30-2012
Or (avoiding the usual Solaris issues with the default awk and sed).

Code:
DIR="/u01/tmp"
find . \( ! -name . -prune \) \( -type f -name "*.tmp" -mtime +10 \) |\
     while read filename1
     do
          filename2=$(echo "${filename1}"|cut -c3-)
          echo "${DIR}/${filename2}"
     done


Last edited by methyl; 03-30-2012 at 10:21 AM.. Reason: correct start directory
# 7  
Old 03-30-2012
Up find/sed works on solaris 10 using /usr/bin/sed

Regards
Peasant.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Help needed - find command for recursive search

Hi All I have a requirement to find the file that are most latest to be modified in each directory. Can somebody help with the command please? E.g of the problem. The directory A is having sub directory which are having subdirectory an so on. I need a command which will find the... (2 Replies)
Discussion started by: sudeep.id
2 Replies

2. UNIX for Dummies Questions & Answers

Recursive Find on file size

Is there a way to use the find command to recursively scan directories for files greater than 1Gb in size and print out the directory path and file name only? Thanks in advance. (6 Replies)
Discussion started by: jimbojames
6 Replies

3. UNIX for Dummies Questions & Answers

Non Recursive Find Command

Hello Unix Gurus, I am using the following find commands: 1) find Input_Path -name '*.' -exec mv -f {} Outputpath \; 2) find Inputpath -name '*.' -exec cp {} Outputpath \; 3) find Somepath -name '*.' Now the problem is my Unix version does not support maxdepth Option for find... (7 Replies)
Discussion started by: pchegoor
7 Replies

4. Shell Programming and Scripting

Tricky recursive removal (find with grep)

Tricky one: I want to do several things all at once to blow away a directory (rm -rf <dir>) 1) I want to find all files recursively that have a specific file extension (.ver) for example. 2) Then in that file, I want to grep for an expression ( "sp2" ) for example. 3) Then I want to... (1 Reply)
Discussion started by: jvsrvcs
1 Replies

5. UNIX for Advanced & Expert Users

Recursive directory search using ls instead of find

I was working on a shell script and found that the find command took too long, especially when I had to execute it multiple times. After some thought and research I came up with two functions. fileScan() filescan will cd into a directory and perform any operations you would like from within... (8 Replies)
Discussion started by: newreverie
8 Replies

6. UNIX for Advanced & Expert Users

Non recursive find command

Hi, I have question is related to find command. I want to find command should search in current folder only not recursive mode(sub-folders). I found a one way of, find . \( -name success -prune \) -o -name "Rajini*" How ever, my current folder is having lots sub-folders and am not... (7 Replies)
Discussion started by: Nagapandi
7 Replies

7. Shell Programming and Scripting

Recursive find and store

I HAVE A TEXT FILE CONTAINING THE VALUES 1.CPP 2.CPP 3.CPP 4.CPP 5.CPP 6.CPP I WANT TO TAKE EACH .CPP AND USE THE FIND COMMAND TO FIND THE LATEST VERSION OF THE FOLDER IN WHICH IT IS PRESENT. HOW DO I IMPLEMENT IT IN A WHILE LOOP I TRIED SOMETHING LIKE THIS WHILE CAT... (3 Replies)
Discussion started by: ultimatix
3 Replies

8. UNIX for Dummies Questions & Answers

making a recursive find more useful..

Hi everyone, I'm using a recursive find (you know the type, find . -name qwert*) to find a set of files. However, because I'm new to the system and there is not much documentation about these particular files I'm trying to find them using this recursive find. I started off at the location... (3 Replies)
Discussion started by: spanish_tony
3 Replies

9. UNIX for Dummies Questions & Answers

Question: non-recursive find syntax

Hello, I am trying to search a directory for all files matching "G*" without looking in sub-directories "success" and "error". I've searched this forum and found the following syntax, but can't make it work: find . \( ! -name success -prune -name error -prune \) -type f -name "G*" Have... (6 Replies)
Discussion started by: alexkav
6 Replies

10. UNIX for Advanced & Expert Users

Performing a non-recursive find in Unix

I need to perform a non-recursive find in Unix. Sounds simple, but it doesn't actually work. The command ALWAYS searches through the subdirectories. Any ideas? I am on DEC Unix :-( (3 Replies)
Discussion started by: christallott
3 Replies
Login or Register to Ask a Question