4 ms·
I use xargs a lot for refactoring work when I cannot simply use sed, e.g. vim $(grep -lr foo | xargs) and doing what I need to do on a file by file basis. Ot
by sonnym 14y ago
I use xargs a lot for refactoring work when I cannot simply use sed, e.g.
vim $(grep -lr foo | xargs)
and doing what I need to do on a file by file basis. Otherwise, for renaming functions and the like, I do a lot of:
find . -name foo_fn exec sed -i s/foo_fn/bar_fn/g '{}' \;
I generally love abusing bash. Just today I was asked about how to rename a bunch of files, specifically containing spaces, and came up with either of these two options:
find -name foo_bar -exec cp "'{}'"{,.bak} \;
and
for file in $(find -name foo_bar); do cp "$file"{,.bak}; done
Ultimately, the great thing is, if you learn CTRL-R, you can always search for these types of commands and modify them as necessary for the particular task at hand and not necessarily remember them. One I use all the time, to push git branches upstream is the following:
CTRL-R --set-
which gives me:
git push -f --set-upstream origin `git rev-parse --abbrev-ref HEAD`
This is entirely unique in my history, a common part of my workflow, and trivially searchable.
I also enjoy being able to perform something along the lines of:
vim $(bundle show foo-gem)
- ralph 14y agoSorry, what do you think vim $(grep -lr foo | xargs) is doing? Assuming a missing directory after the `foo' why can't it just be vim $(grep -lr foo .) I don't see that xargs's default behaviour is adding anything. For find . -name foo_fn exec sed -i s/foo_fn/bar_fn/g '{}' \; you may find -exec's + of use. The above has the fork/execve overhead for each file found. find -name '*.[ch]' -exec sed -i 's/\<foo\>/bar/g' {} +
- sonnym 14y agoYou are correct on both counts regarding the vim and grep example - I guess I just assumed I would have to have all the files on a single line before handing them off to vim. Thanks for the suggestion about -exec +; I will have to remember it in the future.
- ralph 14y agoBackticks, ``, and $(), use IFS to parse the command's output into words which then become the $()'s replacement and IFS normally contains \n.