How does the 'find' command differ when using the '-print' option instead of the '-exec' option?

The find command in Linux is used to search for files and directories based on various criteria. The -print and -exec options serve different purposes:

-print Option

  • The -print option is used to display the paths of the files or directories that match the search criteria directly to the standard output (usually the terminal).
  • It is the default action if no other action is specified.

Example:

find /path/to/search -name "*.txt" -print

This command will find all .txt files in the specified path and print their paths to the terminal.

-exec Option

  • The -exec option allows you to execute a command on each file or directory that matches the search criteria.
  • You can specify any command to run, and it will be executed for each found item.

Example:

find /path/to/search -name "*.txt" -exec cat {} \;

This command will find all .txt files and execute the cat command on each of them, displaying their contents.

Summary

  • Use -print to simply display the matching file paths.
  • Use -exec to perform actions on each matching file, allowing for more complex operations.

0 Comments

no data
Be the first to share your comment!