Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - Print only filenames for the directory and sub-directories

Tags:

linux

ls

I'm very new at Linux world.

I've the following directories on my linux (centos rhel fedora"):

Folder_Root:
    /root/main
        /root/main/files
            /root/main/files/file_1.txt
            /root/main/files/file_2.ssh
        /root/main/files/file_2.txt
    /root/main/file_3.txt

I'm trying to make a print of all the files in all the directories. Basically I am trying to get the following list:

file_1.txt
file_2.ssh
file_2.txt
file_3.txt

I already try 'ls' command and 'ls -al': but it prints also the direcotry name.

I also try to use 'ls -lR | more': but it prints a lot of details that I don't want to use.

Do you recommend any command?


1 Answers

How about using:

find . -type f -exec basename {} \;

or even:

find . -type f -printf "%f\n"

There is a similar question asked here and it has many answers, hope this helps:

List only file names in directories and subdirectories in bash

like image 163
Atul Gopinathan Avatar answered Jan 26 '26 05:01

Atul Gopinathan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!