Need help to find the files under a directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need help to find the files under a directory
# 1  
Old 10-22-2008
Need help to find the files under a directory

Hi,

I wanted to delete all the files under a directory "/apps/tmp/" which are two weeks older. But i should not delete the sub-directories and the contents of sub-directories.

I also have searched in forum and found the following command,

HTML Code:
find . \( ! -name . -prune \) -mtime +13 -print
can some one explain me the meaning of this statement. And also is this the correct way to find the files under a directory excluding the search in sub-directories? can someone please guide me on this.

The above statement displays the sub-directories. I dont want to display the sub-directory names.

Please help me on this.

Thanks in advance.
# 2  
Old 10-22-2008
Try:
Code:
find . \( ! -name . \) -prune -o -mtime +13 -print

There are plenty examples in the web and you can also just try it out without deleting/removing them while trying out.

The above should work - means
- look into . (your working directory)
- if there is something, that's name is not ".", prune it
- or check if it's older than 13 days on it's modification time and print it

If you are sure you get printed what you are looking for, you can add a "-exec rm {} \;" at the end for example.
# 3  
Old 10-22-2008
Quote:
Originally Posted by Sheethal
Hi,

I wanted to delete all the files under a directory "/apps/tmp/" which are two weeks older. But i should not delete the sub-directories and the contents of sub-directories.

I also have searched in forum and found the following command,

HTML Code:
find . \( ! -name . -prune \) -mtime +13 -print
can some one explain me the meaning of this statement. And also is this the correct way to find the files under a directory excluding the search in sub-directories? can someone please guide me on this.

The above statement displays the sub-directories. I dont want to display the sub-directory names.

Please help me on this.

Thanks in advance.
Try This,
find . -maxdepth 1 -name \*.log -mtime +13 -exec rm "{}" ;

This wil find the *.log files and delete files older than 13 days, to avoid searching the subdirectories use maxdepth option.

Regards
Bala
# 4  
Old 10-22-2008
Hi,
Thanks for your quick reply
1) when i execute the command,

find . -maxdepth 1 -name \*.log -mtime +13 -exec rm "{}" ;

I am getting the syntax error,

find: bad option -maxdepth
find: path-list predicate-list

I am using SUN OS 5.8 version

2) find . \( ! -name . -prune \) -mtime +13 -print

If i modify the above command to
find "/apps/tmp" \( ! -name "/apps/tmp" -prune \) -mtime +13 -print

Nothing is getting displayed. That is if i mention the full path instead of dot(cur dir) then nothing is getting displayed. I dont know what i am doing wrong here.
I am writing a script to search for files in a particular dir which is older than two weeks and then i have to delete those files which meets this condition. But i should not delete the sub dir's and the contents of sub-dir's.
I am very new to UNIX. Please help me on this.

Thanks in advance.
# 5  
Old 10-22-2008
Other options:

[for find without the maxdepth option]

Code:
find /apps/tmp/. \( -name . -o -prune \) \
  -type f -mtime +13 -exec rm {} +

You may need to modify the code for some old versions of find that do not support the above syntax:

Code:
find /apps/tmp/. \( -name . -o -prune \) \
  -type f -mtime +13 | xargs rm

Note: the above will break if the filenames contain spaces, embedded new lines or other pathological characters and you should switch to the inefficient -exec rm {} \; or modify the IFS.

If you are lucky (i.e. you have zsh):

(. is for regular files, you may need to add some modifiers if you want to delete links and sockets too)

Code:
rm -- /apps/tmp/*(.Nmw+2)

With Perl:

Code:
perl -e'unlink grep { -f and -M > 13 } glob("/apps/tmp/*")'

You should modify the glob to include the dot-files.

Last edited by radoulov; 10-22-2008 at 08:17 AM..
# 6  
Old 10-29-2008
Hi radoulov,

Thanks for your help.

Since i have lot of files under the directory, i have modified the command like the below,

dbalog=/apps/tmp
if [ -d "$dbalog" ]
then
find $dbalog/* -prune -type f -name "*" -mtime +13 -print | while read FILENAME
do
echo "Deleting file $FILENAME "
rm "$FILENAME"
done
else
echo "dbalog incorrect or missing: $dbalog"
fi

This was working fine. Can you please tell me is there any difference between the command,

Code:
rm "$FILENAME"

and 

rm -- $FILENAME

I know that if we have special characters in the file name then we have to specify the filename in double quotes or have to use -- before the file name. But what is the difference between these two commands. I dont find any difference while executing the command . Please help me on this.

Thanks in advance.
# 7  
Old 10-29-2008
Quote:
Originally Posted by Sheethal
Can you please tell me is there any difference between the command,

Code:
rm "$FILENAME"

and 

rm -- $FILENAME

I know that if we have special characters in the file name then we have to specify the filename in double quotes or have to use -- before the file name. But what is the difference between these two commands. I dont find any difference while executing the command . Please help me on this.
A -- denotes the end of flags that the command can take accept. What follows after that would be arguments to be operated upon. See the following sets of commands and notice that -- is needed to mark the beginning of file names.
Code:
[/tmp]$ mkdir "-f"
mkdir: invalid option -- f
Try `mkdir --help' for more information.
[/tmp]$ mkdir -f
mkdir: invalid option -- f
Try `mkdir --help' for more information.
[/tmp]$ mkdir -- -f
[/tmp]$ ls -l 
drwxr-xr-x  2 xxx xxx 4096 Oct 29 01:00 -f

[/tmp]$ rm -f
[/tmp]$ rm "-f"
[/tmp]$ rm '-f'
[/tmp]$ rm -fd -f
[/tmp]$ ls -l 
drwxr-xr-x  2 xxx xxx 4096 Oct 29 01:00 -f

[/tmp]$ rm -rd -- -f
rm: remove directory `-f'? y
[/tmp]$ ls -l -- -f
ls: -f: No such file or directory

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

find Files in sub-directory

Hi Just want to ask, Is it possible to find a file from a directory up to its sub-directories? Thanks, cmarzan (10 Replies)
Discussion started by: cmarzan
10 Replies

2. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files. For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided. `find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

3. UNIX for Dummies Questions & Answers

How to Find Files other than specified directory ?

Hi All, I am creating one script to Archive the older log files to Archive folder and deleting older files. For example below path contains different sub folders. So searching for log files older than 2 days then zip and moving to Archive directory in the same directory. Source files :-... (4 Replies)
Discussion started by: vadlamudy
4 Replies

4. UNIX for Dummies Questions & Answers

How to find and copy files from one directory to another

Ok i have three directories Destination - /u/dir1 (has subdirectories dir2 which also has subdirectory dir3) Source1 - /u/test/files/dir1/dir2/dir3 Source2 - /u/out/images/dir1/dir2/dir3 What i would like to do is copy everything from Source1 and Source2 into the Destination directory.... (3 Replies)
Discussion started by: ziggy25
3 Replies

5. UNIX for Advanced & Expert Users

How can I find out the open files in a directory

Hello all, Is there any other way of finding the open files in a directory apart from command 'lsof'. thanks (3 Replies)
Discussion started by: joshi123
3 Replies

6. Shell Programming and Scripting

Find files ONLY in current directory

Hello all, I am having a hard type in figuring out how to only gather certain files in the current directory without exploring its subdirectories. I tried: find . -name "*.ksh" -prune this also returns ksh files from lower subdirectories. I also tried find . -ls -name "*.ksh" This also... (8 Replies)
Discussion started by: gio001
8 Replies

7. UNIX for Dummies Questions & Answers

Find files and display only directory list containing those files

I have a directory (and many sub dirs beneath) on AIX system, containing thousands of file. I'm looking to get a list of all directory containing "*.pdf" file. I know basic syntax of find command, but it gives me list of all pdf files, which numbers in thousands. All I need to know is, which... (4 Replies)
Discussion started by: r7p
4 Replies

8. UNIX for Dummies Questions & Answers

How to find a word in a all the files in a Directory??

I want to find a specific word present in all the files ina directory....Please tell me the command to be used?? Thanks (6 Replies)
Discussion started by: shikhakaul
6 Replies

9. Shell Programming and Scripting

Find files in directory

Hi all I want to find a particular file type lets say .abc under /home/oracle/, the file name is start with 'D' and followed by ddmmyyyy date format, the file name should look like this D19092008.abc To my question, how can i perform the searching from the date 19/09/2008 to 29/09/2008. The... (3 Replies)
Discussion started by: coldstarhk
3 Replies

10. Filesystems, Disks and Memory

find the 5o largest files in a directory

I'm trying to find the 50 largest file in a directory named /sasdb and its' subdirectories. I'm using the find command and a pipe to awk Not sure if I'm actually getting the largest files from this directory and its subdirectories. Here is the code I used... find /sasdb -ls | awk '{print... (8 Replies)
Discussion started by: igidttam
8 Replies
Login or Register to Ask a Question