Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

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|improve this answer
    
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 '13 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). –  Eliah Kagan Mar 23 '13 at 13:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.