5

I would like to know if a particular folder is present or not. I used the following command

find /mnt/md0/ -maxdepth 1 -name 'dcn'||'DCN'

I want to know if folder name is DCN or dcn .
How would I do this ?

3
  • 1
    an easy one too: find /mnt/md0 -type d -maxdepth 1 -print | grep -i '/dcn$' Feb 4, 2014 at 14:56
  • @OlivierDulac that's a major complication of process when the binary offers the possibility already, though it can be usefull on UNIX system without GNU binary.
    – Kiwy
    Feb 4, 2014 at 15:04
  • @Kiwy: don't mind me, I always propose "portable" alternative, as I almost don't have any GNU version at work (but instead I have very ancient "historical" ones, even "tar who uncompress keeping the leading '/'" ...). I'm very concerned about portability (even if it means a bit more verbose solution, hence I just put as a comment here...). [on the other hand, I don't gracefully handle "weird filenames", but I never encountered filenames with embedded "returns" so far (whereas I use old utilities everyday)] [I know there was a Linux tag, but still, it's to give alternatives for other viewers] Feb 4, 2014 at 15:14

3 Answers 3

12

You're looking for the option -iname, which stands for "ignore case" on GNU find along with the option -type d for selecting only directories.

find /mnt/md0/ -type d -maxdepth 1 -iname dcn

For more a detail explanation on find switches you consult explainshells.com's explanation of find. (This will match any case: dcn , DcN, DCn)

Edit 1:

As state in comment by Olivier Dulac to use with non GNU find or old find version you could use :

find /mnt/md0 -type d -maxdepth 1 -print | grep -i '/dcn$'

see this answer to have a real compatibility with non GNU and old find version

4
  • @slm I do love that website, it's really nice when having to deal with lot of option on GNU binaries. thanks for the edit by the way.
    – Kiwy
    Feb 4, 2014 at 14:20
  • 1
    Note that it matches dcn and DCN, but also Dcn, dCN... Note that beside GNU, like -maxdepth, -iname is also available in modern BSDs find and in busybox find. Feb 4, 2014 at 15:30
  • 1
    The find implementations that don't have -iname are also likely not to have -maxdepth. Also note that you can't use grep on filenames as grep works on lines while filenames can be made of several line. See my answer for the standard/portable way. Feb 5, 2014 at 9:05
  • @StephaneChazelas you're perfectly right, I do check on AIX and indeed no maxdepth argument
    – Kiwy
    Feb 5, 2014 at 9:08
5

You can use this:

find /mnt/md0/ -type d -maxdepth 1 \( -name 'dcn' -o -name 'DCN' \)
0
4

Standardly:

find /mnt/md0/. ! -name . -prune -type d \( -name dcn -o -name DCN \)

Or for case insensitive match:

find /mnt/md0/. ! -name . -prune -type d -name '[dD][cC][nN]'

You don't necessarily need find though.

printf '%s\n' /mnt/md0/[dD][cC][nN]/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .