A couple of things I wish I knew earlier with linux
Anyone who's used a computer in any capacity knows ping, tracert/traceroute, ls, cat etc. There are a couple of intermediate tools however,
which are easily used and have saved me so much time. I don't profess to be a *nix god, but I have found these to be awesome.
I should stress this is merely an introductory look at the subset of features for each of these tools, they have many more features I'm not going to even be aware of.
xargs - bash foreach
Higher-level programmers may think of this as like a foreach() over your lines in bash.
Many commands in linux only work on a single file, using xargs you can perform the same operation over an entire set of lines easily.
An example
By way of contrived example, lets prepend a directory listing.
xargs will run the command for each line and the -i flag will allow the string being piped in, to be replaced with the given '{}' characters.
thus...
ls | xargs -i echo '/an_additional_directory/'{}
... will take each file or folder and prepend it with the above string, taking:
exampleDir
exampleFile
and making them
/an_additional_directory/exampleDir
/an_additional_directory/exampleFile
This is a contrived example of string manipulation, but a below I'll give an actually useful example.
grep - search for string
It's a well-known command, but I'll cover it here. Grep allows to find a given regex string in a file, an input stream or an entire folder hierarchy. This allows for enormously powerful searching.
grep 'search string' file
Will search for a string in a file (very, very fast)
grep 'search string' -r -i -l . w
Will search an entire directory structure and subfolders (-r) starting from here (with '.') while ignoring case (-i) and only list the files that match rather than the matching string (-l)
ls | grep 'search string'
Will search in the given set of files and folders for a search string.
sed - stream editor
sed is for stream-editing, ie. modifying lines as they are given to it. It can do string modification mid-pipe or to files, among many other things. Actually covering it's features is beyond this
humble tutorial, but here's one thing that it can do which is awesome:
sed -i -s 's/match regex/replace regex/' somefile
... which will go through the file, finding matching string and replacing it.
The -i flag tells sed to do this inline, therefore not needing to worry about piping problems (just use it or else it'll have trouble reading and writing to the same file).
Now these have all been contrived and not that useful. However, what about when one needs to remove say, an email, from an entire website; perhaps hundreds of html files.
There are doubtlessly many ways to achieve this, but using xargs, grep and sed we can do so easily and with a one-liner.
grep -r -l 'bad-email@domain.com' .
... will find all the instances of the files in the current directory containing the offending email and list them only.
xargs
... will send the file to:
sed -i -s 's/bad-email@domain\.com/good-email@domain.com/'
... which will replace the offending email in each of the files.
Thus, the full command is:
grep -r -l 'bad-email@domain.com' . | xargs sed -i -s 's/bad-email@domain\.com/good-email@domain.com/'
MTR - ping + traceroute
On a rather different note, mtr is a simple command which allows for an excellent combination of traceroute and ping to diagnose network problems, particularly lost packets and latency issues.
It's not installed by default, so you may have to grab it via package manager:
sudo apt-get install mtr
... or the equivalent on your system (works for mac too using brew).
Vipe - Vim + pipe
Vipe allows you to grab the contents while piping and edit before sending them on. It is part of the package of more-utils so to install its:
sudo apt-get install moreutils
And to use it simply pipe to and from it as any other program:
ls | vipe | xargs echo
Apropos
a simple search tool for when you can't find the command you're looking for.
Written by David Porter
Related protips
6 Responses
Some more for you:
awk: mini programming language, can do a lot especially if you're good with regexes
find: POWERFUL once you learn the command line switches
cmatrix: absolutely useless
+1 for awk. Absolutely awesome.
Don't forget about Silver Searcher:
sudo apt-get install silversearcher-ag
It lets you do ag foo
, which is roughly equivalent to find -type f|xargs grep foo
, but executes way faster.
Si.ver Searcher is definitely an integral part of my workflow.
nice!
If you enjoy grep you may want to checkout ack-grep which is way more intuitive and in general more awesome for developers.