get all file names in subdirectories
Solution 1
find /usr/share/terminfo -type f -exec basename {} \;
Solution 2
1 Using tree
(man tree for details)
tree -i /usr/shar/terminfo
or if you want do remove the directories:
tree -iF /usr/shar/terminfo | grep -v /
2 Using ls
ls -R /usr/share/terminfo | grep -v /
ls /usr/share/terminfo/*/* | grep -o '[^/]*$'
basename -a $(ls /usr/share/terminfo/*/*)
Solution 3
Since we know that the files we'd like to have the names of are two levels under /usr/share/terminfo
, and that there are no further directories beneath that, this would be a fast solution (only executes find
and cut
once):
$ find /usr/share/terminfo -type f | cut -d '/' -f 6
Alternatively, with one slightly "heavier" sed
substitution:
$ find /usr/share/terminfo -type f | sed 's#^.*/##'
The sed
substitution removes everything up to and including the last /
of each line of output from find
.
Related videos on Youtube
Abhinav Gauniyal
Passionate software engineer skilled in C++ and System Design. Specializes in performance, concurrency, security and defensive programming. Generalist in many other domains. Here's my Github profile. Currently mastering : c++ python rust css javascript nodejs git react php sql mongodb php networking network-programming Send mail to mail.agauniyal at google's email site if you want to get in touch.
Updated on September 18, 2022Comments
-
Abhinav Gauniyal over 1 year
I need to get all file names, not including the paths eg
file1
is correct and./folder/file1
is incorrect, from subdirectories. My use case is retrieving all available terminfo file names which are located inside/usr/share/terminfo
directory. But they are located inside another set of subdirectories as follows -terminfo ア ls 1 3 5 7 9 A c e f h j l m n o P Q s u w X 2 4 6 8 a b d E g i k L M N p q r t v x z terminfo ア cd 1 1 ア ls 1178 1730-lm
You can see files are located inside these
1
3
orA
subdirectories and I need all file names from each subdirectories. There are a total of42 directories, 2720 files
so manual process would be exhausting. -
Abhinav Gauniyal about 7 years
find /usr/share/terminfo -type f -exec basename {} \; | wc -l
gives2718
whiletree
reports42 directories, 2720 files
.. -
Kusalananda about 7 years@AbhinavGauniyal I can't reproduce that discrepancy.
-
GreyWolf about 7 years
[usr@server terminfo]$ tree | tail -n 1 21 directories, 119 files [usr@server terminfo]$ find /usr/share/terminfo -type f -exec basename {} \; | wc -l 119
-
Abhinav Gauniyal about 7 yearsokay my VM doesn't shows this as well, only happens on my laptop.