6 ms·
This is how I learned about xargs ... I once typed onto a client's production mail and web server that basically ran the whole business for about 50 staff, as
by dools 11y ago
This is how I learned about xargs ...
I once typed onto a client's production mail and web server that basically ran the whole business for about 50 staff, as root, from the root directory:
chmod -R 644 /dirname/ *
I seem to recall the reason was that tab completion put a space at the end of the dirname, and I was expecting there to be multiple files with that name ... anyway the upshot was that everything broke and some guy had to spend ages making it right because they didn't have the non-data parts of the file system backed up.
I learned that whenever you do anything you should:
find . -name "*.whatever you want" | more
then make sure you're looking at expected output, then hit the up arrow and pipe it into xargs to do the actual operation.
- jwr 11y agoWhile you're there, it's worth spending a couple of minutes reading about the '-print0' option to find and the correspondingly useful '-0' option to xargs. This might prevent another catastrophe due to spaces in filenames.
- cnvogel 11y agoAnd the "--" option to most utilties that make everything follow the "--" parse NOT as an option. e.g. "rm -- -r -f blubb" will delete the three files "-r", "-f" and "blubb".
- __david__ 11y agoI prefer using the `-d\\n` to `-print0`. It makes transitioning from `| less` to `| xargs` easier. It handles everything except embedded newlines in filenames, which is good enough for me.
- verytrivial 11y agoWhen root or prod, the paranoid (like me) always start every mutating command with a '#' to prevent a sneeze from prematurely sending the command and doing damage.
- Tiksi 11y agoI'm a fan of echo, especially for loops: for i in some/files/*; do echo mkdir -p ${i%%.*}; echo mv "${i}" "${i%%.*}/${i}"; done That way I can see what it's gonna evaluate to first.
- elcct 11y ago[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” For a good measure at the end of the day
- int0x80 11y agoOh, the good ol' shell russian roulete.
- vidarh 11y agoI also tend to pipe to "xargs echo [the rest of the command]" first, to verify that the generated command lines looks sane. Still plenty of fun potential escaping caveats, of course.
- fiatjaf 11y agohttps://github.com/p-e-w/maybe https://github.com/p-e-w/maybe
- jdcarter 11y agoI had a friend that was a system administrator. One of his favorite "puzzle questions" was: what just happened when you see this line in your terminal? .o: file not found (This was back when most people wrote C, compiled to .o files, then linked to a final executable.) The answer was you had typed: rm * .o While meaning to type: rm *.o Oops.
- donretag 11y ago"I learned that whenever you do anything you should: find . -name "*.whatever you want" | more" Many years ago at one company, our servers had a shared root login, no individual accounts (hey, I'm a dev, not ops). I was executing 'find' commands and I decided to use !find to re-execute my last find. Problem was, with a shared account, was that the history was also shared. Turns out the last find command was something like "find . ... -exec rm {} \;". It deleted most of the content from the content directory of our CMS. Backups restored the content, and I never used the ! operator again. Nowadays I use ^r instead so that I can view what I am about to execute.
- chrisshroba 11y agoTo add to this, zsh (or maybe just oh-my-zsh) has tab complete for globbing, so if you type echo foo/bar/*.py and hit tab, it will automatically expand into echo foo/bar/quux.py foo/bar/quux2.py foo/bar/quux3.py (assuming quux.py, quux2.py, and quux3.py are the only 3 python files in foo/bar/) This way, you can preview all the files that you are affecting before actually hitting enter and running the command. You can also recursively glob by doing echo foo/bar/**/*.py which will find all .py files in foo/bar and any subdirectories It's definitely worth installing and trying for an hour, you can decide if you like it or not! Here are more cool features! (Not my website) http://code.joejag.com/2014/why-zsh.html http://code.joejag.com/2014/why-zsh.html