Results 1 to 5 of 5

Thread: delete files in the working directory and sub directories?

  1. #1
    Join Date
    Oct 2008
    Location
    /usr/bin/
    Beans
    429
    Distro
    Ubuntu

    delete files in the working directory and sub directories?

    Hello,

    How can I delete all files called 001.txt in the working directory and its sub directories from the command line?

    I am in /home/tom/test/ I have 40 directories and I want to remove all the files called 001.txt in all 40 directories

    thanks
    Last edited by sohlinux; April 18th, 2013 at 12:42 AM.
    please donate a sig

  2. #2
    Join Date
    Oct 2008
    Location
    /usr/bin/
    Beans
    429
    Distro
    Ubuntu

    Re: delete files in the working directory and sub directories?

    sussed it out

    find . -type f -name "001.txt" -exec rm -f {} \;
    please donate a sig

  3. #3
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,243
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: delete files in the working directory and sub directories?

    find supports -delete flag that makes -exec rm unnecessary
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  4. #4
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: delete files in the working directory and sub directories?

    Cool! Almost perfect.

    You should put the -name test before the -file test in order to avoid having to check each file if it's a regular file or not. You only need to check the files with the name `001.txt'.


    And `rm' accepts one or more files as an argument, so (in Ubuntu; GNU/find) you could use the `-exec command {} +' form of the -exec action:
    Code:
    find ./ -name '001.txt' -type f -exec rm -- {} +
    Please check out http://mywiki.wooledge.org/UsingFind

    From your OP, is not clear for me if you have multiple levels of subdirectories or not.

    If you have a single level of directories, then you could use a glob to match each file:
    Code:
    printf '%s\n' ./*/001.txt
    If not, then, in bash 4 you can use the globstar shell option to match the files recursively in the directory and all subdirectories:
    Code:
    printf '%s\n' ./**/001.txt

  5. #5
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: delete files in the working directory and sub directories?

    Quote Originally Posted by Vaphell View Post
    GNU/find supports -delete flag that makes -exec rm unnecessary
    fixed


    @OP
    Oh and, rm by default (without the -r, -R or --recursive(GNU) flag) does not remove directories; so if you want to remove any files (including character/block specials, symbolic links, named pipes...) except directories, then you don't even have to use the -type option of the find command.

Bookmarks

Posting Permissions

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