Just a note to self. Useful for trimming away useless information from a log file for example.
# Output result
$ sed "/pattern/d" file.log
# Overwrite file
$ sed "/pattern/d" file.log > file.log
# Inplace deletion (requires GNU sed)
$ sed -i "/pattern/d" file.log
In the pattern, things like capturing groups and alternations needs to be escaped with a slash,
\. If you have RegexBuddy you can use the GNU BRE flavor to help you construct the pattern.
I keep seeing people typing these lines in the console:
$ tail /path/to/some.log
$ tail -f /path/to/some.log
This is often a dumb thing to do. Why? Because you can’t really do anything with tail. What if you discovered you needed to look at something right above the lines you got printed out? Or what if you were -f’ing and something flew past you that you needed to investigate further? You’d have to leave tail and run it again with more lines or use a different tool instead. Not very practical.
What more people should do is to use less tail and more less
Things you can do with less
||Up one line|
||Down one line|
||Up one page|
||Down one page|
||Beginning of file|
||End of file|
|ctrl + c
||Next search result|
||Previous search result|
Much more flexible and handy than tail! Know your tools ;) Now back to work…
Just a note to self on how to administer users and groups on Unix systems, and some related tasks.
# Add group
groupadd -g <n> foobar
# Add user
useradd -G foobar -d /path/to/home -m -s /usr/bin/bash alice
# Add user to group
user -G foobar -a alice
# Change user home
usermod -d /path/to/new/home -m alice
# Change default group of user
usermod -g foobar alice
# Change password of user
# Change ownership
chown -R alice:foobar /path/to/target
In a Unix Bash shell we can scroll through previous commands by using the up and down arrow keys, but we can also search for previous commands. I keep forgetting how, so time to write it down
||Start a command search|
||Run the command directly|
|Ctrl+e or Ctrl+a
||Exit search and jump to the end or beginning of command.|
|← or →
||Exit search and move through command as usual|
||Remove everything after the current cursor position and to the end of the command|
Some servers at work has varying degrees of useful prompts when I connect to them through SSH. Usually they are quite annoying and for example showing the shell type and version, which I frankly don’t care much about. Here’s how to make it show the current user, hostname and working directory. In addition the hostname is made more visible, which is a nice effect.
$ bold=$(tput bold)
$ reset=$(tput sgr0)
$ export PS1="\u@\[$bold\]\h\[$reset\]:\w\$ "
Stick it in your
.bash_profile or whatever else you’ve got going on to make it more permanent.
Needed to check some XML output from a CalDAV service so I used curl, which is nice and simple. Only problem was that all the XML came on a single long unreadable line. Figured out this was quite simple to fix.
$ curl --digest --user usr:pwd -X PROPFIND http://cal.example.com/cal.php/principals/usr/default | xmllint --format -
The key part here is of course the piping into
--format tells it to format the XML and the
- tells it to read the XML from standard in. The dash can be swapped with the path to an XML file, if you need to format already downloaded XML.
$ xmllint --format file.xml
Simple pimple dimple
Needed to check the HEAD of a URL on two Unix servers today. Goal was to check if routing, firewall and load balancer rules were all good. One server only had curl, and the other only had wget, so here are commands for both:
$ wget -S --spider http://geekality.net
$ curl -i -X HEAD http://geekality.net