ls on home directory very slow [duplicate]

A while ago, I accidentally created hundreds of thousands of files in my home directory which I subsequently deleted.

Since then however, running ls in my home directory has been extremely slow (taking tens of seconds to print). It has also been slow in other directories (as in taking 1s to print) and tab completion has been slow everywhere (also tens of seconds).

What might the problem be?

Edit: As suggested in the comments, the output of the

find ~/ -maxdepth 1 | wc

is 67 and the output of df -h is

/dev/sda2        74G  8.4G   62G  12% / 
none            4.0K     0  4.0K   0% /sys/fs/cgroup 
udev            3.9G   12K  3.9G   1% /dev 
tmpfs           795M  1.1M  794M   1% /run 
none            5.0M     0  5.0M   0% /run/lock 
none            3.9G  4.9M  3.9G   1% /run/shm 
none            100M   52K  100M   1% /run/user
/dev/sda1       825G  408G  376G  53% /home
AFS             8.6G     0  8.6G   0% /afs
/dev/sdb2       1.9T  584G  1.3T  32% /media/sj1/ResearchData1 
/dev/sdc1       932G  409G  524G  44% /media/sj1/ResearchData2 

Solution 1:

I had the same problem and the reason was a link to a network drive folder which was not working anymore. After removing it everything starts loading instantly.

Solution 2:

Try ls -1 -f /directory if we have huge number of files in directory.

  • -1 lists filename one per line.
  • -f disables sorting.

Solution 3:

creating the hundreds of thousands of files might have created a lot of filesystem extents that do not necessarily get removed when the files are removed. Get a "fragmentation score" with:

sudo e4defrag -c ~/

and if the score is bad (higher than 50, say), use the same command without the '-c' flag to defragment the filesystem.

Solution 4:

Most file systems do not compact directories after deletions. What worked for me is the answer to this older question:

  • Create a new folder
  • Copy existing files and folders to the new folder
  • Delete the slow folder
  • Rename the new folder name to the old folder name