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 or A subdirectories and I need all file names from each subdirectories. There are a total of 42 directories, 2720 files so manual process would be exhausting.


find /usr/share/terminfo -type f -exec basename {} \;

  • find /usr/share/terminfo -type f -exec basename {} \; | wc -l gives 2718 while tree reports 42 directories, 2720 files.. Feb 18 '17 at 9:38
  • @AbhinavGauniyal I can't reproduce that discrepancy.
    – Kusalananda
    Feb 18 '17 at 9:48
  • [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
    – GreyWolf
    Feb 18 '17 at 9:50
  • okay my VM doesn't shows this as well, only happens on my laptop. Feb 18 '17 at 9:58

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.


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/*/*)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.