Find the answer to your Linux question:
Results 1 to 10 of 10
I am trying to find a one line command with linux find to delete all files in a directory. I know the directories are called "logs" but the filenames in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2011
    Posts
    1

    use LINUX find commands to delete files in directory


    I am trying to find a one line command with linux find to delete all files in a directory.

    I know the directories are called "logs" but the filenames in them vary widely.

    I tried a lot of approaches centered around

    find `pwd` -name logs -type d -exec rm {}/* \;
    [ this doesn't expand the star ]

    find `pwd` -name logs -type d -exec ls {} \; | xargs rm
    rm $(find `pwd` -name logs -type d -print)
    [ this cannot find the files as the filenames are listed without the path - just filename ]

    I appreciate if anyone knows the answer.
    Ralf

  2. #2
    Just Joined!
    Join Date
    May 2011
    Posts
    44
    How about:

    Code:
    find -name 'logs' -type d | perl -ne 'print `rm -vrf $_`'
    Works for me anyway.

    (to be clear, the -n implies a while(<>){ } around your code, it is the same as writing
    Code:
    find -name 'logs' -type d | perl -e 'while(<>){print `rm -vrf $_`}'

  3. #3
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664
    I'm sure it can be done more elegantly, but
    Code:
    find `pwd` -name logs -type d|while
    read dir
    do
    rm $dir/*
    done
    should work.

  4. #4
    Just Joined!
    Join Date
    May 2011
    Posts
    44

  5. #5
    Just Joined!
    Join Date
    Sep 2007
    Location
    Silver Spring, MD
    Posts
    95

    Remove files from a directory called logs

    Code:
    find $PWD -name logs -type d -exec ls {} \; | xargs rm -rf

  6. #6
    Just Joined!
    Join Date
    May 2011
    Posts
    44
    Given that parameter,

    Code:
      find ./logs | perl -ne 'print `rm -vrf $_`'
    or

    Code:
      find ./logs | perl -ne  '`rm -rf $_`'

  7. #7
    Just Joined!
    Join Date
    May 2011
    Posts
    44
    Or, even better still:

    Code:
      cd logs; \
      find | perl -ne '`rm -rf $_`'

  8. #8
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664
    Quote Originally Posted by three18ti View Post
    So do I. Concurrent responses to a then six hour old question.

  9. #9
    Just Joined!
    Join Date
    May 2011
    Posts
    44
    I KNOW, it is sooooo LAME when someone responds to a QUESTION!?!?!?!?

    Where in VA are you? I was born in Leesburg, VA. IDR the name of the hospital, I was very young.

  10. #10
    Linux Enthusiast Mudgen's Avatar
    Join Date
    Feb 2007
    Location
    Virginia
    Posts
    664
    I'm near Richmond. Leesburg is in the state of Northern Virginia.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •