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 .
ls *$'\n'*
? – heemayl Jan 22 '17 at 14:17ls .
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 tryls -1A
(-1
=one file per line,-A
=almost all files, excluding only.
and..
) – Byte Commander Jan 22 '17 at 14:21ls
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:22wc -l
it should still see (and count) the newline though - try it – steeldriver Jan 22 '17 at 14:30ls
is outputting to a pipe instead of the console, the newline character is not substituted, as can be seen withls | cat
. Thanks for the info, learned something today :) – Byte Commander Jan 22 '17 at 14:33ls
aliased tols -A
orls -a
by any chance? what do you get if you use\ls . | wc -l
orcommand ls | wc -l
? – steeldriver Jan 22 '17 at 14:38find -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