Why does the ls command show deleted files?

6,005

Solution 1

You may use

printf '%s\n' *

echo *

ls -U

any one of those instead of ls reference here

The ~ symbol may runtime cache or backup files.For that refer here

I edited this after seeing your comment;use this command to delete those backup files (files with ~ symbol)

find ./ -name '*~' | xargs rm

Solution 2

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~'

Solution 3

The name followed by "~" is actually a cache file. When you delete the file it may still remain as an hidden file for the nautilus but the "ls" command will show them.

Run rm *~ to remove those files.

Share:
6,005

Related videos on Youtube

Vasu Dev Garg
Author by

Vasu Dev Garg

Updated on September 18, 2022

Comments

  • Vasu Dev Garg
    Vasu Dev Garg almost 2 years

    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 (~).

    • Ravan
      Ravan almost 9 years
      Does it shows any tild(~) symbol at the end of deleted files @Vasu
    • l0b0
      l0b0 almost 9 years
      Please include the output of ls verbatim in your question (formatted as code).
    • Vasu Dev Garg
      Vasu Dev Garg almost 9 years
      @Ravan Yes it shows tilt symbol at the end of deleted files
    • Ron
      Ron almost 9 years
      they are mostly back-up files created by text editors. You can view them and If you are sure you don't want them, you can delete them as well.
    • Vasu Dev Garg
      Vasu Dev Garg almost 9 years
      @Ron How to delete them and why do i want them? What is their use?
    • Vasu Dev Garg
      Vasu Dev Garg almost 9 years
      @Ron using rm command i have to delete them one by one? But i have such 50 files? Suggest some other way?
    • Ron
      Ron almost 9 years
      @VasuDevGarg if you find any of the posts below useful, do not forget to mark it as the answer to your question :)
    • Vasu Dev Garg
      Vasu Dev Garg almost 9 years
      @Ron Why the tilt files are not shown in GUI?
    • Vasu Dev Garg
      Vasu Dev Garg almost 9 years
      @Ron That's not working!!!
    • Ron
      Ron almost 9 years
      of course, it will. :) see linux.about.com/od/ubuntu_doc/a/ubudg10t7.htm for more
    • Vasu Dev Garg
      Vasu Dev Garg almost 9 years
      @Ron Yes it worked!! The problem was that i was trying Clt-H in Desktop which is not working but in home it is. Thanks!!
  • Vasu Dev Garg
    Vasu Dev Garg almost 9 years
    how to delete them ?
  • Ravan
    Ravan almost 9 years
    Just know I edited @Ron....OP asking to see only current files..
  • Ron
    Ron almost 9 years
    better now. find has a -delete option as well. So find ./ -name '*~' -delete will work as well.
  • Vasu Dev Garg
    Vasu Dev Garg almost 9 years
    @Ron the edit was helpful!!
  • Vasu Dev Garg
    Vasu Dev Garg almost 9 years
    Is there an alternative such that the tilt(~) files that are created by the editors are not shown which are shown in ls command so that i have not to delete them?
  • Vasu Dev Garg
    Vasu Dev Garg almost 9 years
    @Ravan I found that I could hide tilt files by making an alias for ls and putting it into .bashrc ..... code to put in .bashrc is alias ls="ls --ignore-backups $@" but it is not working?
  • Vasu Dev Garg
    Vasu Dev Garg almost 9 years
    @Ravan I firstly want to delete them..... but now I want to modify my ls command so that it doesn't show those files
  • Ron
    Ron almost 9 years
    @VasuDevGarg just use ls without -a and you won't see hidden files.
  • Ravan
    Ravan almost 9 years
  • Ravan
    Ravan over 8 years
    @Wildcard sorry to reject your edit--but I purposely used ls -U but you changed to ls -a--.sorry