1

I have a directory with a bunch of images. Going there with the GUI and pressing CTRL+A (selecting all files) it counts 5058 images. Doing `ls . | wc -l files it counts 5060 files.

I have the suspsect that the "missing files" are images, otherwise a program that I'm using on it would have returned an error message (and it counts 5060 files too).

user6321
  • 175
  • Do you have a file with newline in the name? – heemayl Jan 22 '17 at 14:15
  • The files are from a dataset, I don't really now. But I suppose not. – user6321 Jan 22 '17 at 14:16
  • Whats the output of ls *$'\n'*? – heemayl Jan 22 '17 at 14:17
  • No such file or directory – user6321 Jan 22 '17 at 14:18
  • ls . does neither guarantee that there is only one file per line in the output (depends on terminal width and maximum file name length), nor does it list hidden files (file name starting with a dot .). You should rather try ls -1A (-1=one file per line, -A=almost all files, excluding only . and ..) – Byte Commander Jan 22 '17 at 14:21
  • @heemayl When I generate a file name containing a newline, ls replaces that with a ? character in its output for me, instead of an actual linebreak. I guess this can not be the problem here. – Byte Commander Jan 22 '17 at 14:22
  • @ByteCommander when piped to wc -l it should still see (and count) the newline though - try it – steeldriver Jan 22 '17 at 14:30
  • @steeldriver Oh, right. When ls is outputting to a pipe instead of the console, the newline character is not substituted, as can be seen with ls | cat. Thanks for the info, learned something today :) – Byte Commander Jan 22 '17 at 14:33
  • 1
    Do you have ls aliased to ls -A or ls -a by any chance? what do you get if you use \ls . | wc -l or command ls | wc -l? – steeldriver Jan 22 '17 at 14:38
  • Consider running find -maxdepth 1 -printf "%i\n" | wc -l That should be safe against any special characters in filenames. Let us know what the numbers are – Sergiy Kolodyazhnyy Jan 22 '17 at 17:59

2 Answers2

3

Hidden files can be shown using the GUI or the cli.

Using the GUI way, in Nautilus (file manager) hit ctrl+h to see all files, including the hidden files (filenames that begin with a period).

Hidden files can be metadata files, or image catalog files, or even application preferences... and they're hidden because they don't contribute to the general listing of image files that you have.

In the command line way, it's the difference between the ls command, and the ls -a command. The latter one shows the hidden files too.

heynnema
  • 70,711
  • While this answer certainly addresses how to show hidden files, it doesn't address the why there is a discrepancy between output of ls and GUI. – Sergiy Kolodyazhnyy Jan 22 '17 at 17:26
  • @Serg sure it does... because of hidden files whose filenames begin with a period... like metadata files, or image catalog files, or app pref files, etc. The OP wanted to know if cli was the only way to see these files. And the OP upvoted me 'cause it WAS the answer. – heynnema Jan 22 '17 at 17:28
  • @Serg please restore my up vote. – heynnema Jan 22 '17 at 18:05
  • I cannot, the vote is locked for me. I still think you should improve your post, because it's not exactly what OP wants. The core of the issue is that they have discrepancy between GUI and command-line listings of files – Sergiy Kolodyazhnyy Jan 22 '17 at 18:08
  • See http://imgur.com/a/3WquM – Sergiy Kolodyazhnyy Jan 22 '17 at 18:08
  • @Serg it's been edited. I think it's exactly the answer the OP wanted, they didn't need all of the cli command answers. – heynnema Jan 22 '17 at 18:16
0

If your ls command is aliased to ls -a or ls -A, its output includes two extra items , . and .. , which refer to directory itself . and its parent (..), as well as other files that start with leading . , so there's your difference from 5058 and 5060 files. They don't show up in GUI file manager simply because it's build that way, however most Unix applications are build to recognize the existence of those two.

Additionally, please never use ls | wc -l to count files. Parsing output of ls has a lot of issues, and it's a practice that is generally best to be avoided. If you need to count files in a directory via command line, please see Why does `ls -l` count more files than me? for proper methods.

In this specific case, I'd recommend you use find -maxdepth 1. find prints all of the files and directories, and does not hide those files or directories that begin with leading .

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497