get all file names in subdirectories

6,444

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.

Share:
6,444

Related videos on Youtube

Abhinav Gauniyal
Author by

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, 2022

Comments

  • Abhinav Gauniyal
    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 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.

  • Abhinav Gauniyal
    Abhinav Gauniyal about 7 years
    find /usr/share/terminfo -type f -exec basename {} \; | wc -l gives 2718 while tree reports 42 directories, 2720 files..
  • Kusalananda
    Kusalananda about 7 years
    @AbhinavGauniyal I can't reproduce that discrepancy.
  • GreyWolf
    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
    Abhinav Gauniyal about 7 years
    okay my VM doesn't shows this as well, only happens on my laptop.