I use find to get a list of files, then I grep it to do a path matching :

$ find pc* | grep -i arthur
pc9/Arthur 4C
pc9/Arthur 4C/untitled-1.py
pc9/Arthur 4C/untitled-2.py

I would like to do like in ls -F and append a / for each directory.

Alternatively, how do I get find to show all directories matching case insensitive arthur ?


You can use disjunctions in find expressions to implement conditional processing; with GNU find:

find pc* -type d -printf "%p/\n" -o -print

will find everything in paths starting with the files and directories matching “pc*”, and print their names, followed by a “/” if they’re directories.

The way this works is as follows:

  • pc* specifies the paths to start from;
  • -type d matches directories;
  • -printf "%p/\n" prints the path to the currently-processed file, followed by “/” and a newline; because it follows -type d, it is only executed if -type d matched (there’s an implicit conjunction);
  • -o introduces a disjunction: the expression following it will be evaluated if the expression preceding it did not match (and the expression preceding it is -type d -printf "%p/\n" here, because conjunctions have higher precedence than disjunctions);
  • -print prints the path to the currently-processed file.

Showing all directories matching case-insensitive “arthur” can be done using other find expressions:

find . -type d -iname "*arthur*"
| improve this answer | |
  • Exactly what I was looking for, however I understand easily the second code and the third but not the first one, I would put it last and explain more in details. – Robert Vanden Eynde Dec 19 '18 at 8:53
  • You removerd the version find pc* \( -type d -printf "%p/\n" \) -o \( ! -type d -print \), just curious about what the parenthesis and the ! meant ? :) – Robert Vanden Eynde Dec 22 '18 at 21:13
  • 1
    The parentheses group expressions, and ! negates an expression. So \( -type d -printf "%p/\n" \) -o \( ! -type d -print \) tells find to consider -type d -printf "%p/\n" on the one hand, and ! -type d -print on the other; and ! -type d matches anything which isn’t a directory. – Stephen Kitt Dec 22 '18 at 21:25

find has a huge amount of options and details. Another variant is to use printf with %y

$ find . -printf "%p:%y\n"

where %y adds a "d" or a "f" just like ls -l

$ find . -printf "%p%y\n" | sed 's!d$!/! ; s!f$!!' 
| improve this answer | |

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.