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

2 Answers 2

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
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' 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.