1

When I run ls command I get files like opencv.sh~ in my output which is not visible if I check the home directory.

Basically, it also lists hidden files without supplying any other parameter. How can I prevent this ?

My dist is Ubuntu 14.04

1
  • 1
    ls is aliased to `ls --color=auto' ls is /bin/ls
    – SpiderRico
    Jul 27 '15 at 23:13
3

opencv.sh~ is not a hidden file. POSIX defines a hidden file as one starting with a dot, but opencv.sh~ does not start with a dot.

If you don't want to see these files, you can explicitly avoid them by using a glob that doesn't include them (for example, shopt -s extglob followed by ls !(*\~)), or (as I assume these are from vim), you can configure vim not to put backup files in the same directory.

6
  • You're correct. Then how can I prevent ls from listing files like that? Is it possible ?
    – SpiderRico
    Jul 27 '15 at 23:16
  • But still, that opencv.sh~ is not visible when I check the home directory.
    – SpiderRico
    Jul 27 '15 at 23:16
  • What do you mean by "is not visible"? If your file manager hides that from you, that's file manager specific logic.
    – Chris Down
    Jul 27 '15 at 23:18
  • Yes that's what I meant. So, can I configure ls so that it lists only what file manager shows ?
    – SpiderRico
    Jul 27 '15 at 23:19
  • 1
    @SpiderRico That would require you to know what your file manager does and does not show :-) I've shown how to exclude this specific example in the answer -- you can customise this appropriately and make it into an alias if desired.
    – Chris Down
    Jul 27 '15 at 23:20
3

There's no property on a file that indicates that it's hidden. Early versions of Unix had an ls command that hid files whose name begins with . (“dot files”) and that tradition stuck. The ls command, by default, hides files whose name begins with . and shows all other files.

Some graphical file managers hide files whose name begins with ., just like ls, because that's what many users expect. Gnome has launched a new convention, which is that directories can contain a file called .hidden which contains a list of files to hide (one file name per line, no patterns allowed).

You didn't mention which file manager you used, it's possible that it has different rules for hiding files. If opencv.sh~ is hidden, that follows decidedly non-standard rules, so you can't legitimately complain that ls isn't following these non-standard rules.

Adding a ~ at the end of a file name is the convention for a backup file. That is, opencv.sh~ is an earlier version of opencv.sh, created by your editor when you saved opencv.sh.

The GNU version of ls supports an option -I to ignore extra patterns (in addition to dot files). If you want to hide backup files from ls, you can make an alias that adds -I "*~" to the ls options:

alias ls='ls -I "*~"'

(plus any other option you wish, e.g. alias ls='ls -I "*~"' --color to use colors to indicate file types). Put that line in your .bashrc, .zshrc or whatever initialization file your shell uses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.