find command on a empty directory - bad status


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find command on a empty directory - bad status
# 1  
Old 04-01-2016
find command on a empty directory - bad status

when I run the following command in AIX (bash),
Code:
find ./*

I get the following error.
Code:
find: bad status-- ./*

Thats becasuse, its an empty directory. The same works, when there the directory is not empty. Even though the find deesnt have to rerun any result.


My full find command would look something like below.
Code:
find <dir>/* -type f \( ! -name '*.extn1' -a ! -name '*.extn2' \) -mtime +45 -exec rm -f {} \;

I form the above find command during the script run time.
But for simplicity sake, I had given just the simple fine command in the example.
How to get it fixed?

Thanks
# 2  
Old 04-01-2016
Just give it the directory as an argument; not all of the files in the directory as separate arguments. I.e., change:
Code:
find <dir>/* -type f \( ! -name '*.extn1' -a ! -name '*.extn2' \) -mtime +45 -exec rm -f {} \;

to:
Code:
find <dir> -type f \( ! -name '*.extn1' -a ! -name '*.extn2' \) -mtime +45 -exec rm -f {} \;

and, you can get the same results with:
Code:
find <dir> -type f ! -name '*.extn[12]' -mtime +45 -exec rm -f {} \;

and, it will run a LOT faster if you use -exec command initial-args {} + to minimize the number of times you invoke rm:
Code:
find <dir> -type f ! -name '*.extn[12]' -mtime +45 -exec rm -f {} +

These 2 Users Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-04-2016
Thanks for your time and effort Don!!

As I mentioned, I form the find command based on parameter. So there will be a separate parameter for Directory and a separate param for File names.

eg:

Code:
find #DirName##Pattern#

So the pattern sometimes be "*" or sometime be "*.csv". Should I manually check for "*" and make it blank? Or is there any other options available?

Thanks for simplifying the extension expressions.
I just mentioned it for an example.

The actual value is

Code:
-type  f \( ! -name '*.fs' -a ! -name '*.ds' \)


Iam going to try the below now. Thanks for the Suggestions!
Code:
-exec rm -f {} +

May I know the difference between
Code:
-exec rm -f {} + vs -exec rm -f {} \;

# 4  
Old 04-04-2016
The + collects arguments (up to a certain limit) and runs one (or few) rm -f arg1 arg2 ...,
while the ; each time runs the program with one argument rm -f arg1; rm -f arg2; ....
# 5  
Old 04-05-2016
Thanks for the Clarification!

Quote:
Originally Posted by MadeInGermany
The + collects arguments (up to a certain limit) and runs one (or few) rm -f arg1 arg2 ...,
while the ; each time runs the program with one argument rm -f arg1; rm -f arg2; ....
This User Gave Thanks to deepakwins For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find command giving bad status error

In a fastload teradata utility I am trying to delete the files which are older than 30days using the find and rm command as following. find . -name 'xxx_*' -mtime +30 -exec rm -f {} \; I expect it to delete all the files older than 30 days but sometimes it gives an error : find: bad status--... (3 Replies)
Discussion started by: stelkar
3 Replies

2. Shell Programming and Scripting

Trapping the return status from FIND command

I have the following code in a script: find . \( ! -name . -prune \) -name "cg*" -exec cp -p {} "${temp_dir}" \; ret_stat=$? I think the return status is only captured for the 'find' command and not for the 'cp' command. Is there a way to get the return status for the 'cp' command... (7 Replies)
Discussion started by: vskr72
7 Replies

3. UNIX for Dummies Questions & Answers

Exit Status Of Find Command

Hello All, I am trying to capture the exit status of find command and want to delete the files only when it is successful. But it is always returning me as success even if the pattern of that file doesn't exist in the current directory. please help, checked manual page but couldn't able to figure... (6 Replies)
Discussion started by: Ariean
6 Replies

4. Shell Programming and Scripting

Delete empty files from a directory entered in command prompt

My code to "Delete empty files from a directory entered in command promt" #/bin/sh echo "Enter directory" read gh for file in `ls $gh` do # to get the size of file a=$( ls -l file | awk ' {print $7} '); echo $a if then echo "removing file " rm file fi done (6 Replies)
Discussion started by: adirajup
6 Replies

5. Shell Programming and Scripting

How to find empty files in a directory and write their file names in a text?

I need to find empty files in a directory and write them into a text file. Directory will contain old files as well, i need to get the empty files for the last one hour only. (1 Reply)
Discussion started by: vel4ever
1 Replies

6. UNIX for Dummies Questions & Answers

Find command returning bad status--

would like to remove the post (8 Replies)
Discussion started by: vk39221
8 Replies

7. UNIX for Advanced & Expert Users

Equivalents of tee command to find exit status of command

Hi, Want to log the output of command & check the exit status to find whether it succeeded or failed. > ls abc ls: abc: No such file or directory > echo $? 1 > ls abc 2>&1 | tee log ls: abc: No such file or directory > echo $? 0 Tee commands changes my exit status to be always... (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

8. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

9. UNIX for Dummies Questions & Answers

how to find the exit status for the last executed command

I am executing a find command in my script i.e find $2 -type f -name '*.gif' -mtime +$1 -exec rm {} \; how do i check that this command is executed properly.. i would lke t trap the errror and display my error message kinly help.. this is an urgent issue. (1 Reply)
Discussion started by: vijay.amirthraj
1 Replies

10. Programming

How to find the exit status of last command in Unix?

Hi, I want to find the exit status of the last executed command in C Shell. Tried $? but getting the error Variable syntax...$? does not seem to work in C shell.. is there any other command in C shell to find the exit status of last command? Thanks in advance, raju (1 Reply)
Discussion started by: rajugp1
1 Replies
Login or Register to Ask a Question