0

Nee to find command to generate output exactly as if ls -p would generate?

With find /path/to/ -mindepth 1 -maxdepth 1 -exec basename {} \; the directories don't have trailing slash.. i need the output folder names to have trailing slash

sample output:

folder 1/
my-file-1.sh

find command to list directory contents without full path and folders with a trailing slash

0
$ find /path/to -mindepth 1 -maxdepth 1 -exec sh -c '
     [ -d "$1" ] && printf "%s/\n" "${1##*/}" || printf "%s\n" "${1##*/}" ' _ {} \;
aDirectory/
afile

Explanations:

  • [ -d "$1" ], this checks if its a directory, if yes then run followed printf:

    printf "%s/\n" "${1##*/}"
    

    else, run below printf:

    printf "%s\n" "${1##*/}"
    
    • ${1##*/}: this removes longest match of everything * till last slash / seen start from begging of the file/directory path, which it will result only last directory/file name only.
0

At least with GNU find, you can use the built-in -printf e.g.

find . -mindepth 1 -maxdepth 1 -type d -printf '%f/\n' -o -type f -printf '%f\n'

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.