Tag Archives: Unix
Ubuntu server upgrade
sudo apt-get update
# Upgrade packages
sudo apt-get upgrade
# Remove no longer needed packages
sudo apt-get autoremove
# Upgrade distro
sudo apt-get dist-upgrade
Remove old kernels
sudo shutdown -r now
# Remove all old kernels
sudo apt-get remove --purge $(dpkg -l 'linux-image-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d')
Unix: Remove lines from a file based on regular expression
Just a note to self. Useful for trimming away useless information from a log file for example.
$ 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.
Source: StackOverflow
Tail less
I keep seeing people typing these lines in the console:
$ 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
Key | Function |
---|---|
↑ | Up one line |
↓ | Down one line |
b | Up one page |
space | Down one page |
g | Beginning of file |
G | End of file |
F | Follow |
ctrl + c | Stop follow |
q | Quit |
/ | Search forward |
? | Search backwards |
n | Next search result |
N | Previous search result |
Much more flexible and handy than tail! Know your tools 😉 Now back to work…
Unix: Managing users and groups
Just a note to self on how to administer users and groups on Unix systems, and some related tasks.
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
passwd alice
# Change ownership
chown -R alice:foobar /path/to/target
Unix: Command history search
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 😛
Ctrl+r | 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 |
Ctrl+k | Remove everything after the current cursor position and to the end of the command |
Unix: Useful and pretty prompt
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.
$ reset=$(tput sgr0)
$ export PS1="\u@\[$bold\]\h\[$reset\]:\w\$ "
Stick it in your .profile
, .bash_profile
or whatever else you’ve got going on to make it more permanent.