1

It seems this question has been asked in a number of ways. But I would like to use rm instead of find.

I followed a number of explanations as to how to use find, but I'm having an issue with executing the find command. When I use locate I get the following output:

maxgitt@mgpc:/etc$ locate omero
/etc/nginx/conf.d/omeroweb.conf
/etc/rc0.d/K01omero-web
/etc/rc1.d/K01omero-web
/etc/rc2.d/S03omero-web
/etc/rc3.d/S03omero-web
/etc/rc4.d/S03omero-web
/etc/rc5.d/S03omero-web
/etc/rc6.d/K01omero-web

But then when I try to use find to delete the respective files, I get the following error.

maxgitt@mgpc:/etc$ find -type f -name '*omero*'
./etc/rc0.d/K01omero-web
./etc/rc1.d/K01omero-web
./etc/rc2.d/S03omero-web
./etc/rc3.d/S03omero-web
./etc/rc4.d/S03omero-web
./etc/rc5.d/S03omero-web
./etc/rc6.d/K01omero-web
find: ‘./cups/ssl’: Permission denied
find: ‘./ppp/peers’: Permission denied
find: ‘./polkit-1/localauthority’: Permission denied
find: ‘./ssl/private’: Permission denied
./nginx/conf.d/omeroweb.conf
find: ‘./chatscripts’: Permission denied
find: ‘./docker’: Permission denied

So I opted instead to use the command:

maxgitt@mgpc:/etc$ sudo rm -rf /etc/*/*omero-web*

But I'd prefer to be able to remove all files within the etc/ directory and its respective subdirectories. I am left with one file because of my hacky rm command:

maxgitt@mgpc:/etc$ locate omero
/etc/nginx/conf.d/omeroweb.conf
  • Please simple run sudo find -type f -name '*omero*' and error will go away! – George Udosen Oct 5 '17 at 17:14
4

Use sudo

And always do a dry run before deleting to ensure you are not removing more than bargained for.

sudo find -type f -name '*omero*'

Then, assuming list looks good

    sudo find -type f -name '*omero*' -delete

or specify a location to search (you already know these files are in /etc)

sudo find /etc -iname '*omero*' -delete

-delete is faster than -exec rm {} \; or -print0 | xargs -0 rm as it does not have to spawn another process.

EDIT: From the comments

for i in $(locate omero) ; do rm $i ; done
  • I should be more clear. The dry run DOES NOT LOOK GOOD. I don't want to remove the extra packages that find finds that locate did not find. – Max Oct 5 '17 at 17:53
  • +1 to dry run. Hard to say without seeing the output and what you do and do not want to remove, I am guessing pass the location /etc , but just a guess. Update your question or use a loop for i in $(locate locate omero) .... – Panther Oct 5 '17 at 17:56
  • I am a little confused. I would infact like to remove everything that locate shows me but not the extra files that are found by found as shown above I would not like to remove. Is there a way to remove the files found only through locate – Max Oct 5 '17 at 18:03
  • Look at bash loops tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html . for i in $(locate omero) ; do rm $i ; done . Can have problems with spaces in file names, etc, but should suffice. OR refine your find either by refining the search parameters or search path, hard to tell from what little you have posted. – Panther Oct 5 '17 at 18:13
  • 1
    locate can do null-terminated output, so instead of for i in $(locate ...); locate -0 ... | xargs -0 ... would be better. – muru Oct 6 '17 at 0:56
3

You just need to add the sudo to your command:

sudo find /etc -type f -name '*omero*' -delete

and the error will go away. Reason being that those files are owned by root user and can only be remove by those in the sudo group and root user.

  • 3
    Well, technically can only be removed by root and users in the sudo group can run the command as root. The command will not work without sudo even if you are in the sudo group. – Panther Oct 5 '17 at 17:20
1

There are many many reasons to use find rather than rm. Especially if your version of find supports the "-delete" option:

  • If your filenames contain spaces or newlines, your rm command will not work and may even delete the wrong files.
  • If there are a lot of files to delete, the command will fail for exceeding the maximum command line length.

If your find supports "-delete":

find -type f -path "*omero*" -delete

If your find does not support "-delete" and requires a starting path:

find . -type f -path "*omero*" -print0 | xargs -0 rm

That '-print0 ... -0' is important. This tells find to output filenames delimited by NUL, and xargs -0 will expect NUL delimited filenames. This way spaces and newlines in your filenames won't cause problems.

Also, I'm using -path instead of -name to more closely match what "locate" gives you. However, this means that a file will be deleted if it happens to be underneath a directory with "omero" in it. This is probably not what you want, and you should use -name instead.

With that out of the way, if you're sure of what you got in the end, put sudo at the front of the first version. Or sudo at the front of both find and xargs in the second.

  • How about -exec rm -i {} +? I think it's a good idea to ask for every file before deleting it here… – dessert Oct 5 '17 at 22:24
  • @dessert I did not know about the {} + variation! Thank you! – Chris Cogdon Oct 6 '17 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.