Answer
find /usr/local/lib/systemd -type f -exec basename {} \; | xargs -L1 -I {} bash -c "echo 'stopping' {}; systemctl stop {}; systemctl disable {}"
Explanation
First, take a look at the output of find
find /usr/local/lib/systemd -type f
/usr/local/lib/systemd/file1.service
/usr/local/lib/systemd/file2.service
/usr/local/lib/systemd/file3.service
If you use -exec
, that's essentially a map. For example
find /usr/local/lib/systemd -type f -exec basename {} \;
file1.service
file2.service
file3.service
You can then pipe that to xargs
and use the -L
option to pass in N arguments at a time to a command. In this case, -L1
will repeat the xargs
command for every 1
line in the input. Look at an example where the command just prints "test" and the filename:
find /usr/local/lib/systemd -type f -exec basename {} \; | xargs -L1 echo "test"
test file1.service
test file2.service
test file3.service
You can use the -I
option of xargs
to substitute in the arguments multiple times in a single bash command, like this:
find /usr/local/lib/systemd -type f -exec basename {} \; | xargs -L1 -I {} echo hello {} world {}
hello file1.service world file1.service
hello file2.service world file2.service
hello file3.service world file3.service
Finally, you can use bash -c "..."
to run multiple commands as one command. Use this as the xargs
command, like this:
find /usr/local/lib/systemd -type f -exec basename {} \; | xargs -L1 -I {} bash -c "echo 'stopping' {}; systemctl stop {}; systemctl disable {}"