Bash Builtins – Understanding the Use of ‘type’ Command

bashshell-script

I came across a command in Bash script in which I found:

find /var/log/abcd -type f

The above command was in context of cleaning the log files.
I know what find does.

After having seen -type f, I looked manual page for it.
I got to see it in man page of BASH_BUILTINS(1)

The description of -f flag under type command is :-

The -f option suppresses shell function lookup, as with the command builtin.

Following are my questions:

  1. What is the use of type ?
  2. What is the significance of -f flag?
  3. What is the use of using type with find command?

[EDIT]:- After having read all the comments and answers till now, I would like to mention the cause for my misinterpretation of -type option in command find Vs type command .
This all happened because I was assuming and till date have seen only the short options(Tests in case of the find command) with a single minus sign '-', example, ls -l. Most of the times I have seen long options with double minus sign '–' , example, ls --version.

Best Answer

In this case type has nothing to do with the bash built-in type, but more on that later on.

A little about "type"

The BASH built-in type command gives you information about commands. Thus:

$ type type
type is a shell builtin

The syntax is:

type [-tap] [name ...]
  • -t: print only type, if found
  • -a: print all occurrences of the command, both built-in and other.
  • -p: print the disk file that would be executed on call to command, or nothing.

If we look at time, kill and cat as an example:

$ type time kill cat
time is a shell keyword
kill is a shell builtin
cat is /bin/cat

$ type -t time kill cat
keyword
builtin
file

$ type -a time kill cat
time is a shell keyword
time is /usr/bin/time
kill is a shell builtin
kill is /bin/kill
cat is /bin/cat

$ type -ta time kill cat
keyword
file
builtin
file
file

Now, this specify that if you are in a Bash shell and type time some_cmd, the bash builtin time is used. To use the system time you can do /usr/bin/time some_cmd.

One way often used to ensure that the system, and not built-in, command is used is by using which.

tt=$(which time)

and then use $tt to call system time.


The command in question

In this case the -type is an option to the command find. The option takes one argument of by which specify the type of entity. Example

find . -type f  # File
find . -type d  # Directory

There are more, check man find for the rest.

To search for the specific option you can do (whilst in man):

/^\s*-typeEnter

Then use n for next until you find it.


A little about shell command

This is a bit of a personal interpretation.

Some of the things worth mentioning, in this specific case, are commands, options, arguments and pipes.

This is somewhat loosely used, but in my vocabulary we have in short:

  • command: a program or built-in.
  • parameter: an entity after the command word.
  • option: an optional parameter.
  • argument: a required parameter.

In a command specification square brackets are used to specify options and, optionally less/greater then, used to specify arguments. Thus:

foo [-abs] [-t <bar>] <file> ...
foo [-abs] [-t bar] file ...

Gives -a -b and -s as optional parameters, and file a required one. -t is optional, but if specified takes the required argument bar. Dots represent that it can take several files.

This is no exact specification, and often man or help is required to be sure.

Positioning of arguments options and input can often be mixed up, but it is generally best to keep to a position based approach as some systems does not handle mixed positioning of arguments. As an example:

chmod -R nick 722 foo
chmod nick 722 foo -R

Both work on some systems, whilst the latter does not on other.


In your exact command all parameters belongs to find – thus if you wonder about a property man find is the correct place to look. In cases where you need to look at man pages for the shell etc. could be in e.g.:

find . $(some command)
find . `some command`
find . $some_var
find . -type f -exec some_command {} \;
find . -type f | some_command
...

The -exec is a special one where -exec some_command {} \; are all parameters given to find, but the some_command {} \; part is expanded, within find to some_command string_of_found_entity.


Further on

  • quoting
  • expansion
  • command substitution
  • and so much more

You might find this useful.