Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want a simple list of files in a directory that is not my current directory. I run ls /other/directory/*.txt and get:


I want:


How can I get the second list?

share|improve this question

migrated from Nov 12 '10 at 0:47

This question came from our site for professional and enthusiast programmers.

Can you please tell us what linux distribution you are using? Because normally, ls outputs the second list you mentioned. – Ruel Nov 11 '10 at 16:01
just tried it, i can get only full path when I call 'ls /path/*' without asterisk it is not possible, what system || shell do you use ? – mpapis Nov 11 '10 at 16:02
I'm using CentOS. Also, I was simplifying the original question. I wanted only text files from the directory. I did not realize the *.txt made a difference. I updated the question. – User1 Nov 11 '10 at 16:10
up vote 11 down vote accepted

(cd /other/directory && ls)

share|improve this answer

Is there some reason why you can not use ls -1 ?

$ ls -1 /other/directory


I notice you've changed the question now - my solution won't work with your new example of ls /other/directory/*.txt. Use something like khachik's solution instead, e.g.

$ (cd /other/directory && ls -1 *.txt)
share|improve this answer

1) I'm not sure this shouldn't be on

2) ls doesn't print the full path anyway: ls -1 /your/dir

Edit The question has changed. Per Paul's comment below I am updating my answer. You can do it like this:

ls -1 /home/rich/*.txt | sed s/^.*\\/\//

That's a minus 1, not l, although l works too. Explanation: ls -l/-1 writes out the file names with the stuff you don't want. Each line is piped through sed, which here is doing a substitution, as specified by the s/. A substitution takes the form:


We are substituting everything from the beginning of the line ^ upto the last / (/ is a special character so we have to escape it \\/) with nothing - i.e removing it, and thus leaving you with just the filename.

share|improve this answer
Not an answer - should be a comment – Paul R Nov 11 '10 at 16:01
@Richard, Paul's answer has a solution. Yours don't have any. – Ruel Nov 11 '10 at 16:04
@Richard (1) is a comment on the question, (2) is incorrect (note the difference between 1 and l) – Paul R Nov 11 '10 at 16:05
fail answer copying :) – Ruel Nov 11 '10 at 16:07
By the way, your comment 1)*I'm not sure this shouldn't be on* is incorrect. ..this should be on – Ruel Nov 11 '10 at 16:12
for i in `ls /some/directory` ;do basename $i;done

The expression in `` gets expanded to the files, then each of them is passed to basename. Caveat: Does not work with files containing white spaces!

share|improve this answer

ls -1 /other/directory/*.txt |xargs basename

  • -1 will list one file per line with just the file name (or path)
  • Using a wildcard and/or giving ls a full path will output the full, absolute path for each file. basename will strip the path leaving you with just the file name.
share|improve this answer

You must log in to answer this question.

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