How can I list the files in other (non-current) directory?
Solution 1:
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.
Solution 2:
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