Skip to content
Menu
i do, therefore i am
  • Home
  • Blog
  • Contact us
i do, therefore i am
May 5, 2022November 7, 2022

linux: find -exec

Find is a fine command of itself, however what elevates find to the next level is its ability to execute other commands. The argument for that is -exec.

-exec consists of 3 parts:

  1. command
  2. arguments
  3. delimiter

The syntax is:

find <path/to/search> [search expression] -exec <command> <arguments> <delimiter>

The command

This is just any command that can be executed by the shell. There are caveats here, as aliases and user defined functions are not available. The reason is that is exec running your command and not the shell.

The arguments

These are the arguments, options, etc. to be passed to the command just like they would be given to the shell.

The results argument {}

When using -exec, find provides a special argument {} which contains the result of the current search. It can be used as needed in your arguments.

The delimiter

The delimiter is used to indicate the end of the command to -exec. It comes in two flavors: + and \;.

The + delimiter concatenates all results in one single string and puts them in {}. The command (with all the arguments), will be run only once.

The \; delimiter (the actual delimiter is ;, however it must be escaped with a backslash to prevent the shell from interpreting it) will call command once per result, {} will contain only this one result.

Examples:

Count the lines of each Fortran file:

find . -name "*.f90" -exec wc -l {} \;

Find all Fortran files with comments containing the string “J.L.” (case-insensitive):

find . -name ".f90" -exec grep -iE "!\.*j.l." {} \;

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • vim as Hex-Editor
  • installing pico-sdk in wsl
  • A WordPress theme from scratch 5
  • A WordPress theme from scratch 4
  • Serial Port Windows

Recent Comments

    Archives

    • May 2023
    • January 2023
    • November 2022
    • October 2022
    • September 2022
    • May 2022
    • April 2022
    • March 2022
    • February 2022
    • January 2021

    Categories

    • Fortran
    • linux
    • Postgresql
    • Programming
    • Python
    • Tools, tips and tricks
    • Wordpress

    Meta

    • Log in
    • Entries feed
    • Comments feed
    • WordPress.org

    Postgres

    • postgres-docs

    Python

    • data model
    • the standard library
    • python tips
    • import system
    • asyncio
    • built-in functions

    Open_VMS

    • lexicals at marc’s place
    • documentation and manuals
    • system services
    • rms services reference
    ©2025 i do, therefore i am | Powered by WordPress and Superb Themes!