0

Unix newbie could use your help.

I'm using Solaris 10 and need to find all files, excluding all hidden files and directories. The ultimate goal is to put this in a script that will delete files 60+ days old on a server.

I tried:

find . ! ( -name '.*' -prune )

but it finds no files at all.

Any suggestions are appreciated.

anurag kohli
  • 57
  • 1
  • 2
  • 7

3 Answers3

2

I believe the problem is that you are excluding everything named ".*", and you are starting your search at "." (which matches your exclusion), so you are excluding everything. Also, I believe you are misusing the -prune flag (it's an action, like -print, and so isn't necessarily useful as part of a negated expression). Try this:

find . \( -name '.*' \! -name '.' -prune \) -o -print

This explicitly includes '.' in the search, and then excludes everything else matching .*. If you know that your starting point doesn't include any dotfiles, you can simplify this a bit:

find * \( -name '.*' -prune \) -o -print
larsks
  • 41,276
  • 13
  • 117
  • 170
  • larsks, thanks for your response. yes this does indeed work. i'm also curious, since i also need to delete the files, is the following syntax correct: find . \( -name '.*' \! -name '.' -prune \) -o -exec rm -f {} \ – anurag kohli Nov 20 '10 at 22:38
  • That is, although note that for a large number of files, it's generally better to pipe the output of find to "xargs rm". E.g., something like "find ... -print0 | xargs -0 rm -f" (the -0 tells find (and xargs) to null-terminate, instead of whitespace-terminate, filenames, which is important if your filesystem has filenames that contain spaces). Also, if this answer was helpful, please consider accepting it. Thanks! – larsks Nov 21 '10 at 01:31
  • +1 For versions of `find` that support it, you can get the performance provided by `xargs` (without using it) by using `+` to terminate the `-exec` portion of `find` instead of `\;`. – Dennis Williamson Nov 21 '10 at 01:44
  • dennis, solaris does not accept "xargs -0" so i will explore your suggestion. – anurag kohli Nov 22 '10 at 03:08
0

This should work:

find * | grep -v /\\.
jlliagre
  • 8,691
  • 16
  • 36
  • jlliagre, thanks for your response. while it does work, i don't believe it will let me remove those files. that is, i need to tack on a "-exec rm -f {} \" (without quotes). please correct me if i'm wrong. – anurag kohli Nov 20 '10 at 22:36
  • If your directories and files have "regular" names, i.e. no embedded spaces or similar, you might use this: "find * -mtime +60 -type f | grep -v /\\. | xargs rm -f" . However, a find only command like the one suggested by larsks is more robust as it will properly handle odd names. – jlliagre Nov 20 '10 at 23:00
  • If you are okay using GNU grep (and find and xargs?), you can use: "find * -mtime +60 -type f -print0 | grep -zZv /\\. | xargs -0 rm -f" (not sure if the -Z option to grep is necessary; I think it isn't) – Slartibartfast Nov 20 '10 at 23:25
  • You need also gnu find and gnu xargs for this to work. These utilities are generally not all available on Solaris 10. They are with OpenSolaris/Solaris 11 though. The only advantage of my solution was it was shorter and simpler to understand than the "-prune" one. That is probably not the case anymore with this convoluted Gnuish one ... – jlliagre Nov 21 '10 at 08:15
0

need to find all files excluding all hidden (files and directories).

find . -name '[^.]*'

excluding all (hidden files) and directories.

find . -type f -name '[^.]*'

The ultimate goal is to put this in a script that will delete files 60+ days old on a server

find . -name '[^.]*' -mtime +59 | xargs rm -f

  • eduardo, unfortunately this doesn't work in solaris. it returns no files. however i haven't tried this on linux. thanks. – anurag kohli Nov 21 '10 at 00:50
  • This kind of works with Gnu find, however, non hidden files located in hidden directories are detected. This is probably not what hsw is looking for. – jlliagre Nov 21 '10 at 08:24