Home » Linux » How do we use find -exec?

About Bhagvan Kommadi

Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.

How do we use find -exec?

1. Introduction

In this article, we will see how the find -exec Linux command is used. Find command is used -exec argument in Linux to find the files with different extensions in the directories.

2. Linux Find command and exec option

Find command in Linux has expression and action. The expression part is the first one that is used for filtering the files selected. Action is to act upon the filtered one.

2.1 Prerequisites

You need Linux or macOS or windows environment with bash shell.

2.2 Download

On windows, you can download Unix emulator like cygwin.

2.3 Find Command

Find command can be used to find in a directory the files with an extension. Below is the sample to search for .wav in the Audio directory.

Find files with extension

~ bhagvan.kommadi$ find Audio/ -name *.wav
Audio//amy.wav

you can also add -exec flag to act on the filtered files. Below is the sample.

Find -exec

~ bhagvan.kommadi$ find Audio/ -name *.wav -exec file {} \;
Audio//amy.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 44100 Hz

2.4  The -exec expression 

In Linux, -exec expression has a command, placeholder, and command delimiter. Delimiters can be ; (semi-colon) and + (plus sign). the semicolon is used to repeat each filtered file separately. plus sign is used to concatenate the filtered files.

2.5 Custom function example 

You can have a custom function written which can be executed through the -exec option. Below is the example.

Custom Function

~ bhagvan.kommadi$ do hello () { echo "Doing  with $1";}
~ bhagvan.kommadi$ export -f dohello
~ bhagvan.kommadi$ find . -exec bash -c 'dohello "$0"' {} \;
Doing  with .
Doing  with ./.mlnet
Doing  with ./.mlnet/log.txt
Doing  with ./.eclipse
Doing  with ./.eclipse/240486851_macosx_cocoa_x86_64
Doing  with ./.eclipse/240486851_macosx_cocoa_x86_64/configuration
Doing  with ./.eclipse/240486851_macosx_cocoa_x86_64/configuration/org.eclipse.update
Doing  with ./.eclipse/240486851_macosx_cocoa_x86_64/configuration/org.eclipse.update/platform.xml
Doing  with ./.eclipse/240486851_macosx_cocoa_x86_64/configuration/org.eclipse.core.runtime
Doing  with ./.eclipse/240486851_macosx_cocoa_x86_64/configuration/org.eclipse.core.runtime/.mainData.1

2.6 The Results Placeholder 

The results placeholder is represented by two curly braces {}. Below is the example where place holder for results is used at multiple places.

Results Placeholder

~ bhagvan.kommadi$ find . -name "*.wav" -exec bash -c "basename \"{}\" && file \"{}\" | awk -F: '{\$1=\"\"; print \$0 }'" \;
voice 4.wav
  RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 8000 Hz
voice 5.wav
  RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 16000 Hz
voice 2.wav
  RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 16000 Hz
us2.wav
  RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 16000 Hz
voice 3.wav
  RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, mono 16000 Hz
2020-02-04_13-27-54.wav
  RIFF (little-endian) data, WAVE audio, stereo 44100 Hz
voice 1.wav

2.7 The Delimiter.

Below is the example for delimiter in find -exec command and option.

Delimiter

~ bhagvan.kommadi$ find . -name "*.mp3" -exec echo {} \;
./Music/Music/Media.localized/Music/AcousticSheep, LLC/Unknown Album/01 Noise_Sleep_Induction.mp3
./Music/Music/Media.localized/Music/AcousticSheep, LLC/Ginarator/01 Pentatonic Waves.mp3
./Music/Music/Media.localized/Music/AcousticSheep, LLC/Ginarator/02 a minor thing.mp3
./Music/Music/Media.localized/Music/Unknown Artist/Unknown Album/ToDreamsUnsettled.mp3


2.8 Summary

We looked at the Linux command find with -exec option. Find command is highly used for search the files based on condition passed through exec expression. It can be used for a deep search in a directory hierarchy.

3. Download the Source Code

Download
You can download the full source code of this example here: How do we use find -exec?

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments