Solved

How to use find command in UNIX

Posted on 2007-11-26
10
2,482 Views
Last Modified: 2008-02-01
Hello experts,
I need your help with "find' command. I have directory tree below with files inside each folder. I need to delete files older then x days but only in root directory and not search accross all sub-folders

a/some_files
a/b/some_files
a/b/c/some_files
etc ...

Thanks
0
Comment
Question by:IKeystone
10 Comments
 
LVL 19

Expert Comment

by:darron_chapman
ID: 20351655
This came from this site:  http://www.unix.com/showthread.php?t=9253

find /my_current_directory -mtime +5 -exec rm {} \;


Note
To find files created more than 5 days use -mtime +5
To find files created less than 5 days use -mtime -5
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20352483
Hi,

If the files you are interested in deleting are on a particular file system and you do not want to delete files on other file system which is mounted under the first one, then use the -mount option along with the find command given by darron_chapman.

Be careful when you run such command (with -exec rm ) since files deleted can not be restored. You may first list the files (with -exec ls ) and then look at the list and if it is ok run the find command with -exec rm

If you want to limit the depth to which find command goes to , you need to use the GNU find. Please see:

http://www.gnu.org/software/findutils/
http://www.gnu.org/software/findutils/manual/html_node/find_html/index.html
http://www.gnu.org/software/findutils/manual/html_node/find_html/Directories.html#Directories
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20352986
Unfortunately, Solaris doesn't have GNU find which has the -maxdepth option.  You need to use -prune to stop find from processing subdirectories, eg:

cd /a
find . -type f -mtime +5 -name "[a-z]/" -prune | xargs rm
0
 

Author Comment

by:IKeystone
ID: 20358833
All directory tree is located on one files system.
/a/*.txt
/a/b/*.txt
/a/b/c/*.txt
I need to delete only /a/*.txt older that number of days. I can use
find /my_current_directory -mtime +5 -exec rm {} \; or find /my_current_directory -mtime +5 | xargs rm but it is going over directories and can remove wrong files

0
 

Author Comment

by:IKeystone
ID: 20359259
BTW - I work on HP-UX 11i
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 40

Expert Comment

by:omarfarid
ID: 20359888
Hi,

Any chance that you move all files in the dir to a new dir (with no subdirectories) and then run the find command?

0
 

Author Comment

by:IKeystone
ID: 20360216
I can do it but it is not nice solution. Some files f.e can be in use
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20360304
OK

try

mkdir temp
for file in *
do
if test -f $file
then
ln $file temp/$file
fi
cd temp
find . -mtime +5 > files.$$
cd ..
for oldfile in `cat temp/files.$$`
do
rm $oldfile
done
0
 

Author Comment

by:IKeystone
ID: 20488455
It is a good solution, but I have different FS, and UNIX not allow to create links across different FS.
What to do in this case ?
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 20489792
Please note that the scrip provided creates temp dir in the same dir where you want to run the find command which is on the same file system and hence you should be able to run ln to link files.

Have you tried the script? You may try the script with echo $file to be sure about the files that were found then if every thing is ok replace echo with rm.

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now