Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8187593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:36:44+00:00 2026-06-07T02:36:44+00:00

As I know the commands like find <dir> -type f -exec rm {} \;

  • 0

As I know the commands like

find <dir> -type f -exec rm {} \;

are not the best variant to remove large amount of files (total files, including subfolder). It works good if you have small amount of files, but if you have 10+ mlns files in subfolders, it can hang a server.

Does anyone know any specific linux commands to solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:36:46+00:00Added an answer on June 7, 2026 at 2:36 am

    Here’s an example bash script:

    #!/bin/bash
    
    local LOCKFILE=/tmp/rmHugeNumberOfFiles.lock
    
    # this process gets ultra-low priority
    ionice -c2 -n7 -p $$ > /dev/null
    if [ $? ]; then
        echo "Could not set disk IO priority. Exiting..."
        exit
    fi
    renice +19 -p $$ > /dev/null
    if [ $? ]; then
        echo "Could not renice process. Exiting..."
        exit
    fi
    
    # check if there's an instance running already. If so--exit
    if [ -e ${LOCKFILE} ] && kill -0 `cat ${LOCKFILE}`; then
        echo "An instance of this script is already running."
        exit
    fi
    
    # make sure the lockfile is removed when we exit. Then: claim the lock
    trap "command rm -f -- $LOCKFILE; exit" INT TERM EXIT
    echo $$ > $LOCKFILE
    
    # also create a tempfile, and make sure that's removed too upon exit
    tmp=$(tempfile) || exit
    trap "command rm -f -- '$tmp'" INT TERM EXIT
    
    
    
    # ----------------------------------------
    # option 1
    # ----------------------------------------
    # find your specific files
    find "$1" -type f [INSERT SPECIFIC SEARCH PATTERN HERE] > "$tmp"
    cat $tmp | rm 
    
    # ----------------------------------------
    # option 2
    # ----------------------------------------
    command rm -r "$1"
    
    
    
    # remove the lockfile, tempfile
    command rm -f -- "$tmp" $LOCKFILE
    

    This script starts by setting its own process priority and diskIO priority to very low values, to ensure other running processes are as unaffected as possible.

    Then it makes sure that it is the ONLY such process running.

    The core of the script is really up to your preference. You can use rm -r if you are sure that the whole dir can be deleted indesciminately (option 2), or you can use find for more specific file deletion (option 1, possibly using command line options “$2” and onw. for convenience).

    In the implementation above, Option 1 (find) first outputs everything to a tempfile, so that the rm function is only called once instead of after each file found by find. When the number of files is indeed huge, this can amount to a significant time saving. On the downside, the size of the tempfile may become an issue, but this is only likely if you’re deleting literally billions of files, plus, because the diskIO has such low priority, using a tempfile followed by a single rm may in total be slower than using the find (...) -exec rm {} \; option. As always, you should experiment a bit to see what best fits your needs.

    EDIT: As suggested by user946850, you can also skip the whole tempfile and use find (...) -print0 | xargs -0 rm. This has a larger memory footprint, since all full paths to all matching files will be inserted in RAM until the find command is completely finished. On the upside: there is no additional file IO due to writes to the tempfile. Which one to choose depends on your use-case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know how to programmatically execute commands (like dir C:\ or shutdown
I would like to know how to enter commands into the cmd.exe (command prompt
I have multiple piped commands, like this: find [options] | grep [options] | xargs
rephrase... I'd like to know how to best to parse functions/conditionals. so if you
I've seen some zf commands using a flag like this: -p; I couldn't find
I would like to know how to perform multiple commands in a list comprehension.
I know there exists a command like jQuery.noConflict. But for this it first registers
In Ruby, I know I can execute a shell command with backticks like so:
I know there is a command that updates the changes like c:\svn up <working
Id like to know how to perform the functionality of the 'execfile' Python command

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.