Find the answer to your Linux question:
Results 1 to 5 of 5
I want to delete all files within a specific folder without actually deleting the folder, what is a good bash command for this?. I found this one but encountered some ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    10

    Bash command to remove all files within a specific folder


    I want to delete all files within a specific folder without actually deleting the folder, what is a good bash command for this?. I found this one but encountered some errors even though I am executing it within the specific folder:

    useratdebian:/home/user/folder# find . -type f -exec rm -rf {} \;
    [1] 5052
    useratdebian:/home/user/folder# find: missing argument to `-exec'

    [1]+ Exit 1 find . -type f -exec rm -rf

    The command as it appears is:

    find . -type f -exec rm -rf {} \;

    Any ideas of how to delete only the files contained within the folder called "folder" for example?

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,440
    You copy&paste is wrong.

    The command should be something like
    Code:
    find <FOLDER> -type f
    Replace <FOLDER> with the actual path
    If that returns *only* the files you want to delete:
    Code:
    find <FOLDER> -type f -delete
    fyi:
    You can also work with -exec, the syntax would be
    Code:
    find <FOLDER> -type f -exec rm -f {} \;
    Last edited by Irithori; 10-19-2010 at 10:14 AM.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    10
    Thanks/Danke Irithori!

    Your 'find' command worked great!

    I have another question:

    I have about 36 other folders where I have to basically enter the folder pathname everytime, can the command be modified to delete the files contained within these folders, the folders are in this format:

    A-ABC-20101020-0001 (numbers are in format DDMMYY-NN)
    A-ABC-20100408-0002
    A-ABC-20100306-0003
    etc.....................................

    So the command as it is, is:

    find A-ABC-DDMMYY-NN/folder/subfolder -type f -delete

    Can the command above be adjusted to delete files for each of the 36 folders in just one command without doing it for each separate folder?

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,440
    Use globbing:
    Code:
    find /<PATH_TO_DATADIR>/A-ABC-* -type f
    You must always face the curtain with a bow.

  6. #5
    Just Joined!
    Join Date
    Oct 2009
    Posts
    10
    Danke schoen Irithori.

    The command works great, very effective for a simple command.

    Now I will add a simple crontab job to execute it!

Posting Permissions

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