Ubuntu – Why does the ls command show deleted files

command linels

When I type ls in the terminal, it shows the files that are present as well as the files that I have deleted. How can I see the current files only, and why does ls keep a record of deleted files?

ls shows the deleted file names followed by a tilde (~).

Best Answer

ls does not cache file names. It really does show you exactly what is currently on the file system. However, as @Ravan hinted, there may be similarly named files in your directory. These are typically runtime caches or lock files created by programs such as Vim and Emacs, but could be pretty much anything. For example, if I do the following in one terminal:

$ touch foo
$ vim foo

Then I'll see the following in another terminal:

$ ls -A
foo  .foo.swp

.foo.swp is a hidden temporary file which will be deleted if Vim exits nicely.

Such files can be safely deleted if they are left around from earlier editing sessions which are no longer open. Just make sure you quote the file names, since they may contain special characters like tilde (~):

$ rm '.foo~'
Related Question