Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use find command in UNIX

Posted on 2007-11-26
10
Medium Priority
?
2,495 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 

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
 
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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

715 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