Skip to main content
added explanations
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 118
$ 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:

$ find /path/to -mindepth 1 -maxdepth 1 -exec sh -c ' [ -d "$1" ] && printf "%s/\n" "${1##*/}" || printf "%s\n" "${1##*/}" ' _ {} \; aDirectory/ afile 
  • ${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.
$ find /path/to -mindepth 1 -maxdepth 1 -exec sh -c ' [ -d "$1" ] && printf "%s/\n" "${1##*/}" || printf "%s\n" "${1##*/}" ' _ {} \; aDirectory/ afile 
$ 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.
added 19 characters in body
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 118
$ find /path/to -mindepth 1 -maxdepth 1 -exec sh -c ' [ -d "$1" ] && printf "%s/\n" "${1##.1##*/}/\n"" || printf "%s\n" "${1##.1##*/}\n"" ' _ {} \; aDirectory/ afile 
$ find -mindepth 1 -maxdepth 1 -exec sh -c ' [ -d "$1" ] && printf "${1##./}/\n" || printf "${1##./}\n" ' _ {} \; aDirectory/ afile 
$ find /path/to -mindepth 1 -maxdepth 1 -exec sh -c ' [ -d "$1" ] && printf "%s/\n" "${1##*/}" || printf "%s\n" "${1##*/}" ' _ {} \; aDirectory/ afile 
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 118

$ find -mindepth 1 -maxdepth 1 -exec sh -c ' [ -d "$1" ] && printf "${1##./}/\n" || printf "${1##./}\n" ' _ {} \; aDirectory/ afile