Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
How to delete X number of files in a directory

To get X number of files in a directory, I can do:

$ ls -U | head -40000

How would I then delete these 40,000 files? For example, something like:

$ "rm -rf" (ls -U | head -40000)
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

What about using awk as the filter?

find "$FOLDER" -maxdepth 1 -mindepth 1 -print0 \
    | awk -v limit=40000 'NR<=limit;NR>limit{exit}' RS="\0" ORS="\0" \
    | xargs -0 rm -rf

It will reliably remove at most 40.000 files (or folders). Reliably means regardless of which characters the filenames may contain.


Btw, to get the number of files in a directory reliably you can do:

find FOLDER -mindepth 1 -maxdepth 1 -printf '.' | wc -c 
over 4 years ago · Santiago Trujillo Denunciar

0

The tool you need for this is xargs. It will convert standard input into arguments to a command that you specify. Each line of the input is treated as a single argument.

Thus, something like this would work (see the comment below, though, ls shouldn't be parsed this way normally):

ls -U | head -40000 | xargs rm -rf

I would recommend before trying this to start with a small head size and use xargs echo to print out the filenames being passed so you understand what you'll be deleting.

Be aware if you have files with weird characters that this can sometimes be a problem. If you are on a modern GNU system you may also wish to use the arguments to these commands that use null characters to separate each element. Since a filename cannot contain a null character that will safely parse all possible names. I am not aware of a simple way to take the top X items when they are zero separated.

So, for example you can use this to delete all files in a directory

find . -maxdepth 1 -print0 | xargs -0 rm -rf
over 4 years ago · Santiago Trujillo Denunciar

0

Use a bash array and slice it. If the number and size of arguments is likely to get close to the system's limits, you can still use xargs to split up the remainder.

files=( * )
printf '%s\0' "${files[@]:0:40000}" | xargs -0 rm
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda