How to only get file name with linux`find`?

How to only get file name with linux`find`?

Asked on October 23, 2018 in Linux.
Add Comment


  • 5 Answer(s)

    Use this code:

    find /dir1 -type f -printf "%f\n"
    
    

     

    Answered on October 23, 2018.
    Add Comment

    If your find doesn’t have a -printf option you can also use base name:

    find ./dir1 -type f -exec base name {} \;
    
    
    Answered on October 23, 2018.
    Add Comment

    For newest file name,use this code

    ls -1tr * | tail -1
    
    

    I used it for cp:

    cp $(ls -1tr * | tail -1) /tmp/
    
    
    Answered on October 23, 2018.
    Add Comment

    You can use -execdir parameter which would print the file without path, e.g.:

    find . -name "*.po" -execdir echo {} ';'
    

    Files without extensions:

    find . -name "*.txt" -execdir basename {} .po ';'
    

    Note: Since it’s not POSIX, BSD find will print clean filenames, however using GNU find will print extra ./.

    Answered on January 16, 2019.
    Add Comment

    You could do this using sed (which executed faster for me, than the basename approach):

    find trunk/messages/ -name "*.po" | sed 's!.*/!!' | sed 's!.po!!'
    Answered on January 16, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.