Solved

How to use find command in UNIX

Posted on 2007-11-26
10
2,492 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…

751 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