To show only file name without the entire directory path

ShellUnixDirectoryLs

Shell Problem Overview


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.

Shell Solutions


Solution 1 - Shell

ls whateveryouwant | xargs -n 1 basename

Does that work for you?

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

Solution 2 - Shell

No need for Xargs and all , ls is more than enough.

ls -1 *.txt

displays row wise

Solution 3 - Shell

There are several ways you can achieve this. One would be something like:

for filepath in /path/to/dir/*
do
    filename=$(basename $filepath)

    ... whatever you want to do with the file here
done

Solution 4 - Shell

Use the basename command:

basename /home/user/new/*.txt

Solution 5 - Shell

(cd dir && ls)

will only output filenames in dir. Use ls -1 if you want one per line.

(Changed ; to && as per Sactiw's comment).

Solution 6 - Shell

you could add an sed script to your commandline:

ls /home/user/new/*.txt | sed -r 's/^.+\///'

Solution 7 - Shell

A fancy way to solve it is by using twice "rev" and "cut":

find ./ -name "*.txt" | rev | cut -d '/' -f1 | rev

Solution 8 - Shell

The selected answer did not work for me, as I had spaces, quotes and other strange characters in my filenames. To quote the input for basename, you should use:

ls /path/to/my/directory | xargs -n1 -I{} basename "{}"

This is guaranteed to work, regardless of what the files are called.

Solution 9 - Shell

I prefer the base name which is already answered by fge. Another way is :

ls /home/user/new/*.txt|awk -F"/" '{print $NF}'

one more ugly way is :

ls /home/user/new/*.txt| perl -pe 's/\//\n/g'|tail -1

Solution 10 - Shell

just hoping to be helpful to someone as old problems seem to come back every now and again and I always find good tips here.

My problem was to list in a text file all the names of the "*.txt" files in a certain directory without path and without extension from a Datastage 7.5 sequence.

The solution we used is:

ls /home/user/new/*.txt | xargs -n 1 basename | cut -d '.' -f1 > name_list.txt

Solution 11 - Shell

There are lots of way we can do that and simply you can try following.

ls /home/user/new | tr '\n' '\n' | grep .txt

Another method:

cd /home/user/new && ls *.txt

Solution 12 - Shell

When you want to list names in a path but they have different file extensions.

me@server:/var/backups$ ls -1 *.zip && ls -1 *.gz

Solution 13 - Shell

Here is another way:

ls -1 /home/user/new/*.txt|rev|cut -d'/' -f1|rev

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAshishView Question on Stackoverflow
Solution 1 - ShellfgeView Answer on Stackoverflow
Solution 2 - ShellVRVigneshwaraView Answer on Stackoverflow
Solution 3 - ShellpglView Answer on Stackoverflow
Solution 4 - ShellDwayneView Answer on Stackoverflow
Solution 5 - ShellmrpralineView Answer on Stackoverflow
Solution 6 - ShellzulooView Answer on Stackoverflow
Solution 7 - ShellJohn RumpelView Answer on Stackoverflow
Solution 8 - ShellAddisonView Answer on Stackoverflow
Solution 9 - ShellVijayView Answer on Stackoverflow
Solution 10 - ShellMauriView Answer on Stackoverflow
Solution 11 - ShellChaminda BandaraView Answer on Stackoverflow
Solution 12 - ShellJKEView Answer on Stackoverflow
Solution 13 - ShellRafael MarquesView Answer on Stackoverflow