23

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 3

27

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.

12

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
2
  • 1
    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.
    – krlmlr
    Mar 23, 2013 at 13:21
  • @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). Mar 23, 2013 at 13:25
0

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.

1
  • 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 the dir (MS-DOS) behaviour, rather than poring over the coreutils manpage. You can feed ls a list of files, referencing a directory does that. The manpage says "FILEs". A directory is, essentially, a list of files.
    – pbhj
    Jan 19, 2023 at 9:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .