{"id":393045,"date":"2022-01-12T17:00:00","date_gmt":"2022-01-12T14:00:00","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-use-the-find-command-in-linux\/"},"modified":"2022-01-12T17:00:00","modified_gmt":"2022-01-12T14:00:00","slug":"how-to-use-the-find-command-in-linux","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-find-command-in-linux\/","title":{"rendered":"#How to Use the find Command in Linux"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a41fc70ce8eb\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a41fc70ce8eb\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-find-command-in-linux\/#The_Linux_find_Command\" >The Linux find Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-find-command-in-linux\/#Using_find_With_xargs\" >Using find With xargs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-find-command-in-linux\/#The_find_-exec_Option\" >The find -exec Option<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-find-command-in-linux\/#exec_Really_Means_exec\" >exec Really Means exec<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-find-command-in-linux\/#Using_the_Filename_More_Than_Once\" >Using the Filename More Than Once<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-use-the-find-command-in-linux\/#Horses_for_Courses\" >Horses for Courses<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Use the find Command in Linux&#8221;<\/strong><\/p>\n<div>\n<figure style=\"width: 1200px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage size-full wp-image-771470 \" data-pagespeed-lazy-srcset=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/hero-1.png?width=398&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 400w, https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/hero-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1 1200w\" sizes=\"auto, 400w, 1200w\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/hero-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Linux command line interface on a red background\" width=\"1200\" height=\"675\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><figcaption class=\"wp-caption-text\"><span class=\"type:primaryImage imagecredit\"><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/www.shutterstock.com\/image-vector\/linux-interface-screen-notebook-world-map-321627716\">fatmawati achmad zaenuri\/Shutterstock<\/a><\/span><\/figcaption><\/figure>\n<p>The Linux <code>find<\/code> command is great at searching for files and directories. But you can also pass the results of the search to other programs for further processing. We show you how.<\/p>\n<h2 id=\"the-linux-find-command\"><span class=\"ez-toc-section\" id=\"The_Linux_find_Command\"><\/span>The Linux find Command<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Linux <code>find<\/code> command is powerful and flexible. It can search for files and directories using a whole raft of different criteria, not just filenames. For example, it can search for empty files, executable files, or files owned by a particular user. It can find and list files by their accessed or modified times, you can use regex patterns, it is recursive by default, and it works with pseudo-files like named pipes (FIFO buffers).<\/p>\n<p>All of that is fantastically useful. The humble <code>find<\/code> command really packs some power. But there\u2019s a way to leverage that power and take things to another level. If we can take the output of the <code>find<\/code> command and use it automatically as the input of other commands, we can make something h<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>en to the files and directories that find uncovers for us.<\/p>\n<p>The principle of piping the output of one command into another command is a core characteristic of Unix-derived operating systems. The design principle of making a program do one thing and do it well, and to expect that its output could be the input of another program\u2014even an as yet unwritten program\u2014is often described as the \u201cUnix philosophy.\u201d And yet some core utilities, like <code>mkdir<\/code>, don\u2019t accept piped input.<\/p>\n<p>To address this shortcoming the <code>xargs<\/code> command can be used to parcel up piped input and to feed it into other commands as though they were command-line parameters to that command. This achieves almost the same thing as straightforward piping. That\u2019s \u201calmost the same\u201d thing, and not \u201cexactly the same\u201d thing because there can be unexpected differences with shell expansions and file name globbing.<\/p>\n<h2 id=\"using-find-with-xargs\"><span class=\"ez-toc-section\" id=\"Using_find_With_xargs\"><\/span>Using find With xargs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We can use <code>find<\/code> with <code>xargs<\/code> to some action performed on the files that are found. This is a long-winded way to go about it, but we could feed the files found by <code>find<\/code> into <code>xargs<\/code> , which then pipes them into <code>tar<\/code> to create an archive file of those files. We\u2019ll run this command in a directory that has many help system PAGE files in it.<\/p>\n<pre>find .\/ -name \"*.page\" -type f -print0 | xargs -0 tar -cvzf page_files.tar.gz<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771405\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/1-4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Piping the output from find through xargs and into tar\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The command is made up of different elements.<\/p>\n<ul>\n<li><strong>find .\/ -name \u201c*.page\u201d -type f -print0<\/strong>:\u00a0\u00a0The find action will start in the current directory, searching by name for files that match the \u201c*.page\u201d search string. Directories will not be listed because we\u2019re specifically telling it to look for files only, with <code>-type f<\/code>.\u00a0The <code>print0<\/code> argument tells\u00a0<code>find<\/code> to not treat whitespace as the end of a filename. This means that that filenames with spaces in them will be processed correctly.<\/li>\n<li><strong>xargs -o<\/strong>: The\u00a0<code>-0<\/code> arguments <code>xargs<\/code>\u00a0to not treat whitespace as the end of a filename.<\/li>\n<li><strong>tar -cvzf page_files.tar.gz<\/strong>: This is the command <code>xargs<\/code> is going to feed the file list from <code>find<\/code> to. The tar utility will create an archive file called \u201cpage_files.tar.gz.\u201d<\/li>\n<\/ul>\n<p>We can use <code>ls<\/code> to see the archive file that is created for us.<\/p>\n<p><code>ls *.gz<\/code><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771406\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/2-5.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The archive file created by piping the output of find through xargs and into tar\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The archive file is created for us. For this to work, all of the filenames need to be passed to <code>tar<\/code> <em>en masse<\/em>, which is what happened. All of the filenames were tagged onto the end of the <code>tar<\/code> command as a very long command line.<\/p>\n<p>You can choose to have the final command run on all the file names at once or invoked once per filename. We can see the difference quite easily by piping the output from <code>xargs<\/code>\u00a0to the line and character counting utility <code>wc<\/code>.<\/p>\n<p>This command pipes all the filenames into <code>wc<\/code> at once. Effectively, <code>xargs<\/code> constructs a long command line for <code>wc<\/code> with each of the filenames in it.<\/p>\n<pre>find . -name \"*.page\" -type f -print0 | xargs -0 wc<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771410\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/3-4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Piping multiple filenames to wc at once\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The lines, words, and characters for each file are printed, together with a total for all files.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771412\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/4-4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Word count statistics for many files, with a total for all files\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If we use <code>xarg<\/code>\u2018s\u00a0 <code>-I<\/code> (replace string) option and define a replacement string token\u2014in this case \u201d <code>{}<\/code>\u201c\u2014the token is replaced in the final command by each filename in turn. This means <code>wc<\/code> is called repeatedly, once for each file.<\/p>\n<pre>find . -name \"*.page\" -type f -print0 | xargs -0 -I \"{}\" wc \"{}\"<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771415\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/5-4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using a replace string to send filenames to a wc one at a time\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The output isn\u2019t nicely lined up. Each invocation of <code>wc<\/code> operates on a single file so <code>wc<\/code> has nothing to line the output up with. Each line of output is an independent line of text.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771417\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/6-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Output from multiple invocations of wc\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Because <code>wc<\/code> can only provide a total when it operates on multiple files at once, we don\u2019t get the summary statistics.<\/p>\n<h2 id=\"the-find--exec-option\"><span class=\"ez-toc-section\" id=\"The_find_-exec_Option\"><\/span>The find -exec Option<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>find<\/code> command has a built-in method of calling external programs to perform further processing on the filenames that it returns. The <code>-exec<\/code> (execute) option has a syntax similar to but different from the <code>xargs<\/code> command.<\/p>\n<pre>find . -name \"*.page\" -type f -exec wc -c \"{}\" ;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771431\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/7-4.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using -exec to send single filenames to wc\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This will count the words in the matching files. The command is made up of these elements.<\/p>\n<ul>\n<li><strong>find .<\/strong>: Start the search in the current directory. The <code>find<\/code> command is recursive by default, so subdirectories will be searched too.<\/li>\n<li><strong>-name \u201c*.page\u201d<\/strong>: We\u2019re looking for files with names that match the \u201c*.page\u201d search string.<\/li>\n<li><strong>-type f<\/strong>: We\u2019re only looking for files, not directories.<\/li>\n<li><strong>-exec wc<\/strong>: We\u2019re going to execute the <code>wc<\/code> command on the filenames that are matched with the search string.<\/li>\n<li><strong>-w<\/strong>: Any options that you want to pass to the command must be placed im<a href=\"https:\/\/buradabiliyorum.com\/en\/category\/social-mediaa\/\" data-internallinksmanager029f6b8e52c=\"1\" title=\"Social Media\" target=\"_blank\" rel=\"noopener\">media<\/a>tely following the command.<\/li>\n<li><strong>\u201c{}\u201d<\/strong>: The \u201c{}\u201d placeholder represents each filename and must be the last item in the parameter list.<\/li>\n<li>;: A semicolon \u201c;\u201d is used to indicate the end of the parameter list. It must be escaped with a backslash \u201c\u201d so that the shell doesn\u2019t interpret it.<\/li>\n<\/ul>\n<p>When we run that command we see the output of <code>wc<\/code>. The <code>-c<\/code> (byte count) limits its output to the number of bytes in each file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771432\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/8-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The output from using -exec to send many single filenames to wc\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>As you can see there is no total. The <code>wc<\/code> command is executed once per filename. By substituting a plus sign \u201c<code>+<\/code>\u201d for the terminating semicolon \u201c<code>;<\/code>\u201d we can change <code>-exec<\/code>\u2018s behaviour to operate on all files at once.<\/p>\n<pre>find . -name \"*.page\" -type f -exec wc -c \"{}\" +<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771437\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/9-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using -exec to send all filenames to wc at once\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We get the summary total and neatly tabulated results that tell us all files were passed to <code>wc<\/code> as one long command line.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771439\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/10-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Output from using -exec to send all filenames to wc at once\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"exec-really-means-exec\"><span class=\"ez-toc-section\" id=\"exec_Really_Means_exec\"><\/span>exec Really Means exec<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <code>-exec<\/code> (execute) option doesn\u2019t launch the command by running it in the current shell. It uses Linux\u2019s built-in\u00a0<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/man7.org\/linux\/man-pages\/man3\/exec.3.html\">exec to run the command<\/a>, replacing the current process\u2014your shell\u2014with the command.\u00a0So the command that is launched isn\u2019t running in a shell at all. Without a shell, you can\u2019t get shell expansion of wildcards, and you don\u2019t have access to aliases and shell functions.<\/p>\n<p>This computer has a shell function defined called <code>words-only<\/code>. This counts just the words in a file.<\/p>\n<pre>function words-only () &#13;\n{ &#13;\n\u00a0 wc -w $1&#13;\n}<\/pre>\n<p>A strange function perhaps, \u201cwords-only\u201d is much longer to type than \u201cwc -w\u201d but at least it means you don\u2019t need to remember the command-line options for <code>wc<\/code>. We can test what it does like this:<\/p>\n<pre>words-only user_commands.pages<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771444\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/11-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using a shell function to count the words in a single file\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>That works just fine with a normal command-line invocation. If we try to invoke that function using <code>find<\/code>\u2018s <code>-exec<\/code> option, it\u2019ll fail.<\/p>\n<pre>find . -name \"*.page\" -type f -exec words-only \"{}\" ;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771448\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/11a.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Trying to use a shell function with -exec\" width=\"644\" height=\"75\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The <code>find<\/code> command can\u2019t find the shell function, and the <code>-exec<\/code> action fails.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771449\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/12-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"-exec failing to find the shell function, due to find not running in a shell\" width=\"645\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>To overcome this we can have <code>find<\/code> launch a Bash shell, and pass the rest of the command line to it as arguments to the shell. We need to wrap the command line in double quotation marks. This means we need to escape the double quotation marks that are around the \u201c<code>{}<\/code>\u201d replace string.<\/p>\n<p>Before we can run the <code>find<\/code> command, we need to export our shell function with the <code>-f<\/code> (as a function) option:<\/p>\n<pre>export -f words-only<\/pre>\n<pre>find . -name \"*.page\" -type f -exec bash -c \"words-only \"{}\"\" ;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771450\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/13-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Using find to launch a shell to run the shell function in\" width=\"644\" height=\"95\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This runs as expected.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771452\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/14-3.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"The shell function being called in a new shell\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 id=\"using-the-filename-more-than-once\"><span class=\"ez-toc-section\" id=\"Using_the_Filename_More_Than_Once\"><\/span>Using the Filename More Than Once<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>If you want to chain several commands together you can do so, and you can use the \u201c<code>{}<\/code>\u201d replace string in each command.<\/p>\n<pre>find . -name \"*.page\" -type f -exec bash -c \"basename \"{}\" &amp;&amp; words-only \"{}\"\" ;<\/pre>\n<p>If we <code>cd<\/code> up a level out of the \u201cpages\u201d directory and run that command, <code>find<\/code> will still discover the PAGE files because it searches recursively. The filename and path are passed to our <code>words-only<\/code> function just as before. Purely for reasons of demonstrating using <code>-exec<\/code> with two commands, we\u2019re also calling the <code>basename<\/code> command to see the name of the file without its path.<\/p>\n<p>Both the <code>basename<\/code> command and the <code>words-only<\/code> shell function have the filenames passed to them using a \u201c<code>{}<\/code>\u201d replace string.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-771461\" data-pagespeed-lazy-src=\"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/16-2.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Calling the basename command and words-only shell function from the same -exec call\" width=\"644\" height=\"380\" src=\"\/pagespeed_static\/1.JiBnMqyl6S.gif\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Horses_for_Courses\"><\/span>Horses for Courses<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>There\u2019s a CPU load and time penalty for repeatedly calling a command when you could call it once and pass all the filenames to it in one go. And if you\u2019re invoking a new shell each time to launch the command, that overhead gets worse.<\/p>\n<p>But sometimes\u2014depending on what you\u2019re trying to achieve\u2014you may not have another option. Whatever method your situation requires, no\u00a0one should be surprised that Linux provides enough options that you can find the one that suits your particular needs.<\/p>\n<\/div>\n<p><script>\n setTimeout(function(){\n  !function(f,b,e,v,n,t,s)\n  {if(f.fbq)return;n=f.fbq=function(){n.callMethod?\n  n.callMethod.apply(n,arguments):n.queue.push(arguments)};\n  if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';\n  n.queue=[];t=b.createElement(e);t.async=!0;\n  t.src=v;s=b.getElementsByTagName(e)[0];\n  s.parentNode.insertBefore(t,s) } (window, document,'script',\n  'https:\/\/connect.facebook.net\/en_US\/fbevents.js');\n   fbq('init', '335401813750447');\n   fbq('track', 'PageView');\n  },3000);\n<\/script><\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.howtogeek.com\/771399\/how-to-use-the-find-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Use the find Command in Linux&#8221; fatmawati achmad zaenuri\/Shutterstock The Linux find command is great at searching for files and directories. But you can also pass the results of the search to other programs for further processing. We show you how. The Linux find Command The Linux find command is powerful and flexible&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":393046,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.howtogeek.com\/wp-content\/uploads\/2021\/11\/hero-1.png?height=200p&trim=2,2,2,2","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-393045","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/393045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=393045"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/393045\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/393046"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=393045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=393045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=393045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}