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?
ls will do this.
The syntax for
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
bar (in long form, as I've given the
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:
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
ls -ld /var/log