To show only file name without the entire directory path

Ashish picture Ashish · Dec 15, 2011 · Viewed 157.9k times · Source

ls /home/user/new/*.txt prints all txt files in that directory. However it prints the output as follows:

[me@comp]$ ls /home/user/new/*.txt
/home/user/new/file1.txt    /home/user/new/file2.txt    /home/user/new/file3.txt

and so on.

I want to run the ls command not from the /home/user/new/ directory thus I have to give the full directory name, yet I want the output to be only as

[me@comp]$ ls /home/user/new/*.txt
file1.txt    file2.txt    file3.txt 

I don't want the entire path. Only filename is needed. This issues has to be solved using ls command, as its output is meant for another program.

Answer

fge picture fge · Dec 15, 2011

ls whateveryouwant | xargs -n 1 basename

Does that work for you?

Otherwise you can (cd /the/directory && ls) (yes, parentheses intended)