If ls lists the content of the current directory, is there a similar command that list the content of a non current directory, without using cd?
3 Answers
After ls
just type the path of the directory you want to list and you will get it. Imagine you are in your home directory but want to list /etc
. Just run ls /etc
and you will get it listed.
ls
will do this.
The syntax for ls
is:
ls flags file-or-directory-names
For what I'm calling file-or-directory-names
, you can list files in the current directory, to have just certain files listed. For example, ls -l foo bar
will list information on just foo
and bar
(in long form, as I've given the -l
flag). ls baz*go
will list all files whose names start with baz
and end with go
(if any).
But when you give a directory name to ls
, it lists the contents of that directory instead of the current directory.
So, to list the contents of /var/log
, you would simply run:
ls /var/log
If you give a relative path for the directory--that is, one that doesn't start with a /
--then it will look for that directory inside the current directory (just as it looks for files). But it will still list the contents of the directory, as you want.
As a side note, sometimes you might find you don't want to list the contents of a directory, but just list the directory the same as a file would be listed. To do this, you can pass the -d
flag. For example, this shows /var/log
in long form (not its contents, but /var/log
itself):
ls -ld /var/log
-
1Very detailed. However, remember that
ls
is not responsible for expandingbaz*go
to all files that start withbaz
and end withgo
-- it's the shell that does this.– krlmlrMar 23, 2013 at 13:21 -
@krlmlr Right,
ls baz*go
is an example ofls
receiving (potentially) multiple arguments forfile-or-directory-names
(and in this way is similar tols foo bar
). Mar 23, 2013 at 13:25
Having seen the accepted answer, I was still not satisfied. How did Daniel know the solution? You bet I have read the manpage myself and still couldn't figure out how.
The man page of ls
reads:
Description: List information about the FILEs (the current directory by default)...
Nothing useful there. Then I checked POSIX Programmer's Manual:
For each operand that names a file of type directory, ls shall write the names of files contained within the directory as well as any requested, associated information.
This line explains the behavior of the command in the accepted solution. The FILE
operand of ls
CAN be a directory, and ls
will list the content of that directory. Exactly what the OP wanted. BUT the implementation on Linux may differ from the specification here. So we are on the right track but not there yet.
Then I searched for the manual of GNU coreutils
package, which ls
is a part of. Here is what I found:
For non-option command-line arguments that are directories, by default ls lists the contents of directories ...
This explains it.
-
Almost certainly the responder knew the solution because they'd tried using
ls
with directory names, or saw it used, or just knew about Unix philosophy ("everything is a file"), or copied thedir
(MS-DOS) behaviour, rather than poring over the coreutils manpage. You can feedls
a list of files, referencing a directory does that. The manpage says "FILEs". A directory is, essentially, a list of files.– pbhjJan 19, 2023 at 9:55