The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Delete files using locate database

Discussion in 'General Discussion' started by ManXP, Sep 18, 2010.

  1. ManXP

    ManXP Well-Known Member

    Joined:
    Feb 3, 2005
    Messages:
    62
    Likes Received:
    0
    Trophy Points:
    6
    Hi,

    I want to make a simple script which could locate and delete some files using locate database. That's right, I want script to use LOCATE (not find) command because it's much faster.

    Script should also display files located/deleted. At the moment I have a script like this:


    find /home -name error_log -exec rm -f {} \;
    find /home -name "Thumbs.db" -exec rm -f {} \;

    However, it does not display deleted files, and if I change "find" to "locate", it simply doesn't work. Could anyone provide me a working example of this simple script using locate, please? Google didn't return me what I wanted...
     
  2. u001b

    u001b Registered

    Joined:
    Aug 26, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Locate doesn't have the built-in -exec option like find does, but you can pipe to xargs to complete the task:
    Code:
    locate error_log | xargs rm -f
    locate Thumbs.db | xargs rm -f
    And if filenames/directories contain whitespace:
    Code:
    locate -0 error_log | xargs -0 rm -f
    locate Thumbs.db | xargs -0 rm -f
     
Loading...

Share This Page