Handy tips / tricks and one liners

shadow_man

Executive Member
Joined
May 27, 2005
Messages
7,025
Reaction score
1,745
Location
Cape Town
Lets share some handy tips / tricks or one liners you use frequently.

ENSURES HISTORY WRITES IMMEDIATELY AND NOT ONLY ON A CLEAN EXIT. IF THE USER EXITS THE SSH SESSION UNCLEANLY THEN HISTORY NEVER WRITES - UNLESS THIS IS SET:

NOTE: Add to /etc/profile for ALL USERS
Code:
export PROMPT_COMMAND='history -a'

ENSURES HISTORY RESULTS ARE TIMESTAMPED:

NOTE: Add to /etc/profile for ALL USERS
Code:
export HISTTIMEFORMAT="%d/%m/%y %T "

CHANGE NETWORK ADAPTER TO SET NAME - BINDS VIA MAC ADDRESS (UBUNTU):

NOTE: Add to /etc/udev/rules.d/70-persistent-net.rules
Code:
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="0c:c4:7a:bc:3a:7d", NAME="eth0"
 
Last edited:
Oooh thanks for this, I always wondered why some of my sessions history doesn't save and some do. Laziness.
 
Another one I tend to use which lots of people seem to ignore:

USE !NUMBER TO EXECUTE A COMMAND FROM HISTORY:

Code:
$ history

  331  mv server.txt server.crt
  332  rm -rf *
  333  ls
  334  nano root.crt
  335  cat root
  336  cat root.crt
  337  cat server.crt intermediate_server_ca.crt root.crt > ssl-bundle.crt

!331 <ENTER> would enter mv server.txt server.crt and hit RETURN to execute it in your terminal.

Useful, but DANGEROUS as we could automatically execute a dodgy command. Imagine executing line 332 automatically? Painful right?

So we can take this one step further and STOP the RETURN, so that it doesn't execute. It will then only load the command on the command line, but won't run it - we can then confirm if it's the correct command or not. This is useful and stops us automatically running potentially dangerous commands if we fat finger things.

To do this we do:

NOTE: Add to /etc/profile for ALL USERS

Code:
shopt -s histverify
 
Code:
:(){ :|:& };:

Fork bombing people isn't nice :(

To explain to others what this does:

https://en.wikipedia.org/wiki/Fork_bomb
http://askubuntu.com/questions/1594...d-make-my-system-lag-so-badly-i-had-to-reboot

Code:
:() means you are defining a function called :

{:|: &} means run the function : and send its output to the : function again and run that in the background.

The ; is a command separator, like &&.

: runs the function the first time.

Essentially you are creating a function that calls itself twice every call and doesn't have any way to terminate itself. It will keep doubling up until you run out of system resources.
 
Last edited:
Create an alias on the "rm" command for your root user.
Code:
alias rm='rm -i'
Place that in your ".bashrc" or ".bash_aliases".
You will then get prompted for every file you delete, unless you specify the "-f" option as well.
 
Create an alias on the "rm" command for your root user.
Code:
alias rm='rm -i'
Place that in your ".bashrc" or ".bash_aliases".
You will then get prompted for every file you delete, unless you specify the "-f" option as well.

That's kind of how copy is aliased in most distro's currently. Forces interactive.
 
That's kind of how copy is aliased in most distro's currently. Forces interactive.

Some distro's also have it already aliased for "rm", but still useful to implement for the older Unix systems.
Had customers that practices their new found unix skills on the production Solaris server, forcing us to test the backup recovery strategy ...
 
Some distro's also have it already aliased for "rm", but still useful to implement for the older Unix systems.
Had customers that practices their new found unix skills on the production Solaris server, forcing us to test the backup recovery strategy ...

Definitely makes sense.

We get the new guys to to append a dot to their command e.g. rm file becomes rm ./file so they make sure they're only deleting from the current directory. Any safeguards are better than none :D
 
Top
Sign up to the MyBroadband newsletter
X