How to use find to append a string to every file in a directory

Carcophan

I'm trying to append a fixed string to each file in a folder (and its subfolders) whilst skipping the .git directory.

I expected that something like this would work

 find . -type f ! -path "./.git/*" -exec "echo \"hello world\" >> {}" \;

and if I run it with an additional echo it does generate commands that look right

bash> find . -type f ! -path "./.git/*" -exec echo "echo \"hello world\" >> {}" \;
echo "hello world" >> ./foo.txt
echo "hello world" >> ./bar.txt
...

and those commands do what I want when I run them directly from the shell but when I run it from find I get this:

bash> find . -type f ! -path "./.git/*" -exec "echo \"hello world\" >> {}" \;
find: echo "hello world" >> ./foo.txt: No such file or directory
find: echo "hello world" >> ./bar.txt: No such file or directory
...

but those files do exist because when I list the directory I get this:

bash> ls
bar.txt  baz.txt  foo.txt  subfolder/

I guess that I'm not quoting something I need to but I've tried everything I can think of (double and single quote, escaping and not escaping the inner quotes etc...).

Can someone explain what is wrong with my command and how to achieve the the addition of the fixed string to the files?

devnull

You need to instruct the shell to do it:

find . -type f ! -path "./.git/*" -exec sh -c "echo hello world >> {}" \;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

run `nvm use` automatically every time there's a .nvmrc file on the directory

From Dev

How to open file and append a string in it, swift

From Dev

How to append string to file if it is not included in the file?

From Dev

How to use sed to append multiple lines to a file?

From Dev

How can we append asterisk (*) at the end of last line(content) of each and every text file within same directory in Ubuntu 20.10?

From Dev

How can i append every string in a list into a list?

From Dev

Find every occurence of string in a file and store it in a List

From Dev

how to copy the dynamic file name and append some string while copying into other directory in unix

From Dev

Use of find in unix on strange file/directory names

From Dev

How do I create a directory for every file in a parent directory

From Dev

use jquery append a string to DOM then use jquery selector cannot find it

From Dev

Move every file that is not a directory

From Dev

How can I append an incremental count to every line of a text file?

From Dev

How to change file extention of every file in directory unix/linux

From Dev

How to append a string to the first column of a matrix file?

From Dev

How do I touch every file in a directory?

From Dev

How to find a file matching a specific pattern and date while copying every found file to another web server's directory?

From Dev

How to use sed to append multiple lines to a file?

From Dev

How to use Sed to find a file pattern and append a variable value at end of line

From Dev

linux find file in directory with given string in content

From Dev

How to append string to file in windows store app?

From Dev

Shell how to use a command on every file in a directory

From Dev

How to use sed and regex to find and replace a string present inside a file?

From Dev

How do I use `find` to go to directory of that file

From Dev

How to append List[String] to every row of DataFrame?

From Dev

find string and append contents in new file

From Dev

How to treat an output of find as a string not a refrence to a file or directory

From Dev

How to use FOR to find string in file, strip it of spaces, set variable to it for token?

From Dev

How can we append asterisk (*) at the end of last line(content) of each and every text file within same directory in Ubuntu 20.10?

Related Related

  1. 1

    run `nvm use` automatically every time there's a .nvmrc file on the directory

  2. 2

    How to open file and append a string in it, swift

  3. 3

    How to append string to file if it is not included in the file?

  4. 4

    How to use sed to append multiple lines to a file?

  5. 5

    How can we append asterisk (*) at the end of last line(content) of each and every text file within same directory in Ubuntu 20.10?

  6. 6

    How can i append every string in a list into a list?

  7. 7

    Find every occurence of string in a file and store it in a List

  8. 8

    how to copy the dynamic file name and append some string while copying into other directory in unix

  9. 9

    Use of find in unix on strange file/directory names

  10. 10

    How do I create a directory for every file in a parent directory

  11. 11

    use jquery append a string to DOM then use jquery selector cannot find it

  12. 12

    Move every file that is not a directory

  13. 13

    How can I append an incremental count to every line of a text file?

  14. 14

    How to change file extention of every file in directory unix/linux

  15. 15

    How to append a string to the first column of a matrix file?

  16. 16

    How do I touch every file in a directory?

  17. 17

    How to find a file matching a specific pattern and date while copying every found file to another web server's directory?

  18. 18

    How to use sed to append multiple lines to a file?

  19. 19

    How to use Sed to find a file pattern and append a variable value at end of line

  20. 20

    linux find file in directory with given string in content

  21. 21

    How to append string to file in windows store app?

  22. 22

    Shell how to use a command on every file in a directory

  23. 23

    How to use sed and regex to find and replace a string present inside a file?

  24. 24

    How do I use `find` to go to directory of that file

  25. 25

    How to append List[String] to every row of DataFrame?

  26. 26

    find string and append contents in new file

  27. 27

    How to treat an output of find as a string not a refrence to a file or directory

  28. 28

    How to use FOR to find string in file, strip it of spaces, set variable to it for token?

  29. 29

    How can we append asterisk (*) at the end of last line(content) of each and every text file within same directory in Ubuntu 20.10?

HotTag

Archive