Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

/other/directory/file1.txt
/other/directory/file2.txt

I want:

file1.txt
file2.txt

How can I get the second list?

share|improve this question
1  
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
add comment

migrated from stackoverflow.com Nov 12 '10 at 0:47

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

4 Answers

up vote 10 down vote accepted

(cd /other/directory && ls)

share|improve this answer
add comment

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

$ ls -1 /other/directory
file1
file2

EDIT:

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
add comment

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

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:

s/text/replacement/

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
1  
Not an answer - should be a comment –  Paul R Nov 11 '10 at 16:01
1  
@Richard, Paul's answer has a solution. Yours don't have any. –  Ruel Nov 11 '10 at 16:04
1  
@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
1  
fail answer copying :) –  Ruel Nov 11 '10 at 16:07
1  
By the way, your comment 1)*I'm not sure this shouldn't be on superuser.com* is incorrect. ..this should be on superuser.com –  Ruel Nov 11 '10 at 16:12
show 14 more comments
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
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.