How can I list the files in other (non-current) directory?

49,920

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
Share:
49,920
TifatulS
Author by

TifatulS

Updated on September 18, 2022

Comments

  • TifatulS
    TifatulS over 1 year

    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?

  • krlmlr
    krlmlr about 11 years
    Very detailed. However, remember that ls is not responsible for expanding baz*go to all files that start with baz and end with go -- it's the shell that does this.
  • Eliah Kagan
    Eliah Kagan about 11 years
    @krlmlr Right, ls baz*go is an example of ls receiving (potentially) multiple arguments for file-or-directory-names (and in this way is similar to ls foo bar).