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.

Deleting files

Discussion in 'General Discussion' started by DigitalSurgeon, Aug 30, 2005.

  1. DigitalSurgeon

    DigitalSurgeon Well-Known Member

    Joined:
    Aug 13, 2005
    Messages:
    134
    Likes Received:
    0
    Trophy Points:
    16
    Its a simple question but I have to know that...

    "How can I delete files in a folder without deleting the folder" e.g I have a folder named "folder1" that contains 100 files and two conf files .I want to delete all files except two conf files and the folder1 itself.
     
  2. chirpy

    chirpy Well-Known Member

    Joined:
    Jun 15, 2002
    Messages:
    13,475
    Likes Received:
    20
    Trophy Points:
    38
    Location:
    Go on, have a guess
    From shell I presume - well, you could just use an SFTP GUI client, or there are probably many ways to do this, but this should work:

    cd /path/to/folder1/
    find ./ * | grep -v .conf | xargs rm -fv


    As always, you use commands involving rm at your own risk ;)
     
  3. DigitalSurgeon

    DigitalSurgeon Well-Known Member

    Joined:
    Aug 13, 2005
    Messages:
    134
    Likes Received:
    0
    Trophy Points:
    16
    Thanks but can you elaborate this command that how it works
    find ./ * | grep -v .conf | xargs rm -fv
     
  4. carlosferns2004

    carlosferns2004 Active Member

    Joined:
    May 5, 2005
    Messages:
    41
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    india
    Hi

    Hi

    It is very simple

    Just copy the two conf file to different folder.

    n run this command rm -Rf *.*

    the hundred files will be deleted

    then restore the two conf files to the folder.


    Regards
    carlos
     
    #4 carlosferns2004, Aug 31, 2005
    Last edited by a moderator: Aug 31, 2005
  5. chirpy

    chirpy Well-Known Member

    Joined:
    Jun 15, 2002
    Messages:
    13,475
    Likes Received:
    20
    Trophy Points:
    38
    Location:
    Go on, have a guess
    That's a very dangerous command to recommend to someone who isn't sure what to do.

    Sure. The pipe characters | have the output from the previous command passed to the next one for processing, so:

    find ./ *

    lists all files in the directory tree

    | grep -v .conf

    The output of which is sent to grep which finds matches on all files except those that contain .conf (that's what the -v forces)

    | xargs

    The result of which is passed to a special binary called xargs which passes the results as arguments to the following command

    rm -fv

    which deletes the files that are passed to it. As I said, there are many ways to do it and this is just one of those.
     
  6. DigitalSurgeon

    DigitalSurgeon Well-Known Member

    Joined:
    Aug 13, 2005
    Messages:
    134
    Likes Received:
    0
    Trophy Points:
    16
    Thanks chirpy :)
     
Loading...

Share This Page