Deleting tons of files in a directory in Linux

If you are trying to delete a very large number of files you may get this error.

/bin/rm: argument list too long

when you type  “rm -rf *”, the “*” is replaced with a list of every matching file, like “rm -rf file1 file2 file3 file4” and so on. There is a small buffer of memory allocated to storing this list of arguments and if it gets filled then shell will not execute the program.

more efficient way of removing tons of files in a directory is by using find command with delete option some thing like this.

cd /directory to delete
find . -type f -delete

we can also show the filenames while deleting them

find . -type f -print -delete

output:

./dblog.txt1405418
./dblog.txt799222
./dblog.txt248899
./dblog.txt1264370
./dblog.txt1787419
./dblog.txt2565511
./dblog.txt2043335
./dblog.txt266967
./dblog.txt2030396
./dblog.txt1597576
./dblog.txt996214
./dblog.txt565343
Advertisements
This entry was posted in Linux. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s