Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Echo text to multiple files using find

I'd like to add some simple text into some files. Specifically, I do this on Linux lpfc drivers:

ls -1 /sys/class/scsi_host/host* | awk -F '@' '{system("echo 0x0 > "$1"/lpfc_log_verbose")}'

But thinking about common case I need to handle spaces in file names. Thus I turned to find:

find -L /sys/class/scsi_host -nowarn -maxdepth 2 -type f -name 'lpfc_log_verbose' -exec echo 0x0 > {} \; 2>/dev/null

But this seems not working.

find -L /sys/class/scsi_host -maxdepth 2 -type f -name 'lpfc_log_verbose' -exec cat {} \; 2>/dev/null

is fine but shows my edit didn't success. So can we use redirect in find -exec? What is the correct work-around?

like image 220
MeaCulpa Avatar asked Jan 12 '12 00:01

MeaCulpa


People also ask

How do I search for a string in multiple files?

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character. The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters. You can append as many filenames as needed.

Can you use sed on multiple files?

Here is a quick command that you can use to search and replace a piece of text across multiple files using find, xargs, and sed.

How do I echo content of a file?

Use the echo command, used with the append redirection operator, to add a single line of text to a file. This adds the message Remember to back up mail files by end of week. to the end of the file notes.


1 Answers

So can we use redirect in find -exec?

No, because the > {} is handled by Bash before invoking find. Technically, instead of running

find ... -exec echo 0x0 > {} ...

you could run

find ... -exec bash -c 'echo 0x0 > {}' ...

but I think it's simpler to write:

for dir in /sys/class/scsi_host/host* ; do
    echo 0x0 > "$dir"/lpfc_log_verbose
done

(which — fear not — does handle spaces and newlines and control characters).

like image 107
ruakh Avatar answered Oct 02 '22 11:10

ruakh