4

What is the difference between find * and find ~ for searching a file? In terminal when my present working directory on root,then in terminal

root@devils-ey3:~# find * -print -quit
~

On same directory

root@devils-ey3:~# find ~ -print -quit
/root

But if I change the pwd then the output of find ~ -print -quit is same as before but the other is change. What is the working purpose of * and ~ for find file ?

  • 1
    Do you have a file (or directory) named ~ in your /root directory? – Dubu Jun 16 '15 at 10:52
8

The basic format of find is

find WHERE WHAT

So, in find *, the * is taken as the WHERE. Now, * is a wildcard. It matches everything in the current directory (except, by default, files/directories starting with a .). The Windows equivalent is *.*. This means that * is expanded to all files and directories in your current directory before it is passed to find. To illustrate, consider this directory:

$ ls
file  file2

If we run set -x to enable debugging info and then run your find command, we see:

$ find * -print -quit
+ find file file2 -print -quit
file

As you can see above, the * is expanded to all files in the directory and what is actually run is

find file file2 -print -quit

Because of -quit, this prints the first file name of the ones you told it to look for and exits. In your case, you seem to have a file or directory called ~ so that is the one that is printed.

The tilde (~), however, also has a special meaning. It is a shortcut to your $HOME directory:

$ echo ~
/home/terdon

So, when you run find ~, as root, the ~ is expanded to /home/root and the command you run is actually:

# find ~ -print -quit
+ find /root -print -quit
/root

Again, you are telling find to search for files or directories in a specific location and exit after printing the first one. Since the first file or directory matching /root is itself, that's what is printed.

0

First of all kept in mind the syntax of find:-

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

What is the difference between find * and find ~ for searching a file?

  • * is while card which matches everything.

    Here find * passes the list of files/dirs in the current directory and those as target names to search for. so, it will find all files with the same name as the ones in pwd

    (thanks to @terdon for explaining what 'find *' does ; see that answer)

  • ~ used for $HOME Directory.

    So, find ~ finds for files and directory (recursively) within ~ (home directory)


root@devils-ey3:~# find * -print -quit
~

There must be file or directory named ~ inside /root which case above output. (not confuse with ~ as $HOME directory)


root@devils-ey3:~# find ~ -print -quit
/root

Explanation for find ~ -print -quit:-

Considerable option: -quit:

-quit  Exit  immediately.  No child processes will be left running, but no more paths specified on the command
              line will be processed.  For example, find /tmp/foo /tmp/bar -print -quit  will  print  only  /tmp/foo.
              Any  command  lines  which have been built up with -execdir ... {} + will be invoked before find exits.
              The exit status may or may not be zero, depending on whether an error has already occurred.
  1. ~ is taken as path i.e. /root because you are on rooted terminal.
  2. -quit prints directory name only

But if I change the pwd then the output of find ~ -print -quit is same as before...

As explained above:- find ~ -print -quit prints /root every-time as path is set to ~ (which is /root because of rooted shell) and used -quite. (independent of pwd!)

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.