execute a command on every file that has an specific extension - bash

as simple as the title. I have nested files and i want to execute a command on them if they end in .toml.
Trying with
find . -type f -exec sed -i '3i value = 1' {} \;
runs over every file, but is there a way to do it over only the files ending in .toml?

There sure is; use the -name switch:
find . -name '*.toml' -type f -exec sed -i '3i value = 1' {} \;
This matches any file ending in .toml. Careful to include the quotes to prevent the * from being expanded by the shell - you want it to be passed to find as-is.
By the way, you can speed up the execution of your script by using -exec {} + instead:
find . -name '*.toml' -type f -exec sed -i '3i value = 1' {} +
This passes multiple results to the same instance of sed, rather than spawning a separate one for every result.

find . -type f -name '*.toml' -exec sed -i '3i value = 1' {} \;

Related

From Bash I would like to use find to execute 2 commands on the same file, so I can output the file name and metadata about the file

From bash I need to execute 2 commands on specific files recursively. I am trying to print the filename and meta information at the same time. I need to combine the following 2 commands so first a filename is printed, then the metadata for that file is printed, and then repeat for the next file.
I print the file name with:
find . -wholename '*word/test.wsp -exec echo {} \;
And I print the meta-data with:
find . -wholename ‘*word/test\.wsp’ -exec whisper-info {} \;
However, the second command does not print the filename, so I am unsure which files the meta-data belongs to.
How can I execute the 2 commands simutaneously?
I've tried:
find . -wholename '*word/test.wsp -exec echo {} && whisper-info {} \;
find . -wholename '*word/test.wsp -exec echo && whisper-info {} \;
find . -wholename '*word/test.wsp -exec echo {} && -exec whisper-info {} \;
find . -wholename '*word/test.wsp -exec echo {} \; && whisper-info {} \;
find . -wholename '*word/test.wsp -exec echo {} \; && -exec whisper-info {} \;
And a lot of other combinations.
Just put two -execs.
find . -wholename '*word/test.wsp' -exec echo {} \; -exec whisper-info {} \;
If you have GNU find, perhaps prefer its built-in printf rather than an external echo.
You also consistently seem to have misplaced the closing quote. The string with the wildcard needr to be quoted or escaped to prevent the shell from expanding the wildcard before find runs.
-exec require a single command, but that command can be a shell that executes an arbitrary script consisting of your two commands.
find . -wholename '*word/test.wsp' -exec sh -c 'echo "$1"; whisper-info "$1"' _ {} \;
A few notes:
{} is not embedded in the command. Let the command be a static script that accepts an argument, instead.
_ is a dummy value used to set $0 in the shell.
You can avoid spawning quite so many shells by including a loop in the shell to iterate over multiple arguments provided by find -exec ... +
find . -wholename '*word/test.wsp' -exec sh -c 'for f; do echo "$f"; whisper-info "$f"; done' _ {} +
for f; do ...; done is short for for f in "$#"; do ...; done.

How to add a new line (\n) between each file being concatenated

How should I modify this line to add a new line (\n) between each file being concatenated?
find /disk/data/source/* -name '*.xml' -exec cat {} \; > /home/userid/merged-file.xml
find accepts multiple -execs in one command. For example:
find /disk/data/source/* -name '*.xml' -exec cat {} \; -exec echo "" \; > /home/userid/merged-file.xml
Using awk instead of cat:
find /disk/data/source/* -name '*.xml' \
-exec awk 'NR!=FNR&&FNR==1{print ""} 1' {} + > /home/userid/merged-file.xml
Since you are already using find, try this:
find /disk/data/source/* -name '*.xml' -exec cat {} \; -exec echo \; > /home/userid/merged-file.xml
And if you want to get rid of the extra newline at the end, you can add | head -n-1.

Find and append to files in Unix

I am trying to find files by filename and write to them.
find ./ -name "filename" -type f -exec echo "some string" >> {} \;
This creates a file named {} and writes the string to it for every match instead of writing to the files found.
Try:
... -exec sed '$asome string' -i {} \;
You can spawn a subshell as part of the -exec and then use redirection as usual:
find . -name "filename" -type f -exec bash -c 'echo "some string" >> "$1"' _ {} \;
_ is a dummy handle for $0 within bash -c.

Bash: execute a command on all files with extension recursively

I'm trying to use the following command:
herbalizer file_name.haml > file_name.erb
Here the file_name.haml is the file name, obviously.
How can I apply this command to all haml files in current directory recursively to all sub-directories? Filename should stay the same as mentioned above, so applying on abc.haml would be herbalizer abc.haml > abc.erb
So far: find . -type f -exec herbalizer {} \;
You're pretty close. You can use basename to strip the extension of the name of each file you find:
find . -type f -name \*.haml -exec sh -c 'herbalizer "{}" > "$(dirname {})/$(basename {} .haml).erb"' \;
I wrapped the i/o redirection in a shell command line. Filenames are enclosed in quotes in case a filename or path component contains spaces.
PS. That got you the job done, but it wasn't very elegant; so here's an alternative that uses bash's built-in substitution:
find . -type f -name \*.haml -exec bash -c 'FN="{}"; herbalizer "{}" > "${FN%.haml}.erb"' \;
You can use basename, dirname and find to get your desired results:
find . -type f -name "*.haml" | while read fname
> do
> herbalizer ${fname} > $(dirname $fname)/$(basename $fname .haml).erb
> done
Another simple method as suggested in the comments sections by #Dummy00001 is:
find . -type f -name "*.haml" | while read fname
> do
> herbalizer ${fname} > ${fname%.haml}.erb
> done
Further reference: Parameter substitution

How do I recursively search and replace directory paths in terminal Mac?

I've looked at the following other threads which recommend a particular 'sed' command:
https://superuser.com/questions/428493/how-can-i-do-a-recursive-find-and-replace-from-the-command-line
Find/sed: How can I recursively search/replace a string in files but only for lines that match a particular regexp
Recursive search and replace in text files on Mac and Linux
perl -pi -w -e 's/SEARCH_FOR/REPLACE_WITH/g;' *.txt
And
find . -type f -name '*.txt' -exec sed -i '' s/this/that/ {} +
I am trying to replace this sample below that occurs in multiple files in the directory:
<script src="http://localhost/massignition/wp-content/themes/twentythirteen/js/html5.js"></script>
with the following (substituting localhost/massignition with www.site.com)
I type the following:
find . -type f -name '*.txt' -exec sed -i '' s%localhost/massignition%www.site.com%/ {} +
But nothing changes.
Remove / which is present in before curly braces in your command, that is
find . -type f -name '*.txt' -exec sed -i '' s%localhost/massignition%www.site.com% {} +
instead of
find . -type f -name '*.txt' -exec sed -i '' s%localhost/massignition%www.site.com%/ {} +

Resources