Is there a simpler form of find . -name "*substring_of_filename*" on Mac OS X and Linux?

6,232

Solution 1

On Mac OS X you may also put an alias using mdfind into .bashrc:

# mdfind uses Spotlight 
alias findf='mdfind -onlyin . -name'

findf app_config

Solution 2

Here's your bash function, put it in .bashrc:

function findfiles { find "$1" -iname "*$2*"; }

(Note that -iname makes the search case-insensitive. If you don't want that, use -name instead.)

Solution 3

Au contraire! There is an even more complicated one, but one, which works:

find . -name "*app_config*"

The dot isn't neccessary for gnu-find:

find -name "*app_config*"

But you have to mask the asterixs, else they will be interpreted by the shell, and you will only find filenames which match the ones in the current directory, which match "app_config".

Solution 4

Put this either in your ~/.profile or ~/.bashrc, not sure which one Mac uses:

findname () 
{ 
    find . -name "*$1*"
}

then call the function by findname name_of_file

if you wanted to use multiple words, you'd have to get more complicated..the easiest way I can think of offhand is this:

findname() 
{ 
    find .|grep $1|grep $2|grep $3|grep $4 
}

That will find every file in your current dir, search for string 1, then search that result for string 2, and etc, till it gets to 4.

Share:
6,232
Mohammad Reza Kargar
Author by

Mohammad Reza Kargar

Updated on September 18, 2022

Comments

  • Mohammad Reza Kargar
    Mohammad Reza Kargar over 1 year

    My web app (java/spring) application in localhost sends email successfully but when i deploy it on host i got following error:

    javax.mail.MessagingException: 501 5.5.1 HELO/EHLO requires domain address
    

    I use java mail library.

    web server: tomcat 7

    thanks.

    • nerkn
      nerkn about 13 years
      locate app_config
    • user unknown
      user unknown over 11 years
      @knittl: But locate is not restricted to the current dir and it's subdirs.
    • Zuko
      Zuko over 9 years
      which domain were u using on your localhost and which one is on your production host..? Also, change the domain in your code from localhost to that of your host atleast.
    • Mohammad Reza Kargar
      Mohammad Reza Kargar over 9 years
      what do you mean from 'domain'?
  • Amozoss
    Amozoss about 13 years
    seems not true at least on Mac OS X
  • user unknown
    user unknown about 13 years
    What seems not true? That you can omit the dot? Or that a file which contains 'app_config' in its name and which is in the current directory, will restrict the search to his name.
  • Amozoss
    Amozoss about 13 years
    I mean on Mac OS X, you can use find . -name *app_config* without the quotes
  • user unknown
    user unknown about 13 years
    But do you have a file called fooapp_configbar in the starting directory? You can use the command without quotes on Linux, for example, too, and it will leave the asterix untouched, if there is no matching file in the current directory at the very first level.
  • Wuffers
    Wuffers about 13 years
    You can also run this in a bash session if you want to use it just for the current session.
  • Eroen
    Eroen about 12 years
    I suspect "all files having the app_config in it" really meant "all files having app_config in their names".
  • BenU
    BenU over 11 years
    Thanks, @user_unknown. I couldn't figure out why I wasn't getting find ~ -name "*.pdf" to work on my mac os x until I tried leaving out the quotes. In Mac OS X, it seems that one must leave out the quotes.
  • user unknown
    user unknown over 11 years
    @BenU: I don't have a System with Mac OS X, but on Linux, the need for quotes arises from the shell. If I don't quote the "*", the shell tries to expand it. It is not needed needed for find, and not, if the * doesn't match something in the current dir - then it is passed unchanged to find. You may try single quotes, which are allowed too in bash, but would prevent variable expansion (which isn't the case, here, but sometimes else) -name "*.pdf.$id" for example.
  • user unknown
    user unknown over 11 years
    iname is not equivalent, name is. It might be your favorite usage, but was not the question.
  • Arne Stenström
    Arne Stenström about 11 years
    For the record, quotes should be used in OS X just the same way as in Linux, since OS X has the same Bash shell as Linux.
  • Arne Stenström
    Arne Stenström about 11 years
    The second function doesn't work unless the variables are quoted like find . |grep "$1" |grep "$2" |grep "$3" |grep "$4". Also, it searches for parts of the path rather than parts of filenames.
  • Alex Hirzel
    Alex Hirzel about 11 years
    If anyone ports these to their shell of choice, feel free to edit this post and add your version.
  • Bill Shannon
    Bill Shannon over 9 years
    The root cause is usually that your machine's name service is not configured correctly. Perhaps you're using DHCP and there's no host name associated with the IP address you're using? If the JDK can't get a proper host name for your machine, it doesn't know what to send to your mail server in the HELO command. The mail.smtp.localhost property overrides that. (The other properties are unrelated to this error.)
  • Mohammad Reza Kargar
    Mohammad Reza Kargar over 9 years
    thanks Bill. my app is running on centos server. as you said the problem was hostname. i modified etc/hosts file and add hostname for 127.0.0.1 ip and the problem is went. thanks again.