How could I strip the paths from a list of file names on standard input? I basically need something like basename that iterates over standard input.
            Asked
            
        
        
            Active
            
        
            Viewed 232 times
        
    0
            
            
        3 Answers
1
            while read path; do basename "$path" ; done
Simple loop.
 
    
    
        Mihir Luthra
        
- 6,059
- 3
- 14
- 39
- 
                    I turned that into a function that works perfectly. function basename_pipe() { while read path; do basename "$path" ; done } – Aug 08 '19 at 14:58
0
            
            
        Try xargs basename -a. It'll run faster than a simple loop as basename won't be run for each filename.
$ printf "%s\n" foo/bar bar/baz | xargs basename -a
bar
baz
Note that the above won't correctly handle filenames with spaces. You'll want to use the -0 option to xargs and ensure the input is NUL terminated.
$ printf "%s\0" 'foo/foo bar' bar/baz | xargs -0 basename -a
foo bar
baz
 
    
    
        Jon
        
- 3,573
- 2
- 17
- 24
0
            
            
        According to this answer
If your filename is in the form of a variable such as $fullpathname
nopathfile="${fullpathname##*/}"
It's in the bash manpage at the section called "Parameter Expansion"
 
    
    
        ThanhPhanLe
        
- 1,315
- 3
- 14
- 25
 
    
    
        oksage
        
- 33
- 5