How can I list the current directory or any directory path contents without using ls command? Can we do it using echo command?
printf '%s\n' *
as a shell command will list the non-hidden files in the current directory, one per line. If there's no non-hidden file, it will display
* alone except in those shells where that issue has been fixed (csh, tcsh, fish, zsh, bash -O failglob).
Will list the non-hidden files separated by space characters except (depending on the shell/echo implementation) when the first file name starts with
- or file names contain backslash characters.
It's important to note that it's the shell expanding that
* into the list of files before passing it to the command. You can use any command here like,
head -- * to display the first few lines (with those
head implementations that accept several files),
stat -- *...
I you want to include hidden files:
printf '%s\n' .* *
(depending on the shell, that will also include
printf '%s\n' *(D)
Among the other applications (beside shell globs and
ls) that can list the content of a directory, there's also
find . ! -name . -prune
(includes hidden files except
lsattr (lists the Linux extended file attributes):
lsattr lsattr -a # to include hidden files like with ls
If you just want a list of directory contents:
find . -maxdepth 1
or for any other dir:
find <dir> -maxdepth 1