Thursday, December 9, 2010

Linux: Interesting, obscure commands

# First, the most important place for interesting commands:

# Now, a bunch of cut-and-pasted stuff, from a thread...

# to fix the termincal

# or try Ctrl-v Ctrl-o

Or try:
reset='echo "X[mX(BX)0OX[?5lX7X[rX8" | tr "XO" "\033\017" && /usr/bin/reset'
ESC [m (actually ESC [0m) Character Attributes: Normal (not bold f.i.)
ESC (B Select G0 Character Set: United States (USASCII)
ESC )0 Select G1 Character Set: Special Character and Line Drawing Set
O ( Ctrl-O ) Switch to Standard Character Set
ESC [?5l DEC Private Mode Reset: Normal Video
ESC 7 Save Cursor
ESC [r weird (actually 'ESC [0;0r' ? Set Scrolling Region [top;bottom] )
ESC 8 Restore Cursor

# to turn off display
xset dpms force off

# for virtual terminal
Personally, I think every Linux user should know how to use the virtual terminals. Just hit Ctrl+Alt+F1 and that should take you to a bash prompt. Usually the main one you're on with X running is F7, so you can switch back to that.
If X locks up on me, just a simple:
login, and run
$ sudo /etc/init.d/gdm restart
Note that it could be gdm, kdm or xdm depending on your distro.
On RedHat or Ubuntu, you could instead:
$ sudo service gdm restart
$ invoke-rc.d gdm restart # for ubuntu/debian

# Others
GNU-screen (or tmux) is an excellent command (won't have to use nohup again), if you don't have it you should install it and try it.
If you're on a Red Hat based distro, yum and rpm are good to know. If it's Debian based, apt-get anddpkg for installing stuff.
pingtraceroute (or mtr --curses or nstat)ifconfig are all handy for networking stuff.
Look into htop its a much nicer version of top but you may need to enable additional 3rd party repositories if using yum or apt-get (or aptitude). Or nmon, or atop. And pgrep?

# More on screen:
screen (start a screen session)
screen -dr (detach said screen session and reattach it in current sess)
screen -ls (show active screen sessions)
screen -dr [screen session] (detach and resume a specific session)
# And for pair programming:
screen -S sessionname (start a session with a name) screen -x sessionname (attach the named session, even if it's attached elsewhere)
Those are essentially the only two I use, with the occasional "screen -ls". I much prefer -x over -r as you can attach in multiple places. So at home I always leave stuff running in screen and when I log in from work or where ever I can attach that same screen session without first detaching it form my terminal at home. Plus you could have two people working together in one "screen" which is good for pair programming.
 # The magic SysRq key

# General stuff


  • grep
  • awk
  • uniq, sort, sort -n
  • seq
  • cut
  • wc


  • rsync
  • lsof
  • find | xargs
  • locate
  • df -H
  • du -cks | sort -n
  • scp
  • strings
  • file
  • touch
  • z* (zgrep, zcat, etc)
  • tail -f, head


  • man
  • ps auxf (f only on GNU)
  • kill, -HUP, -9
  • sudo
  • screen
  • /etc/init.d/ scripts
  • id
  • ^Z, fg, jobs, &


  • nmap
  • dig
  • tcpdump
  • ifconfig


  • The knowledge that bash is a programming language that provides all your basic constructs (ifs, loops, variables, functions), but instead of having a library of functions, you execute simple programs instead
  • |
  • <, >, >>
  • - as stdin, e.g. "cat somefile.txt | vi -"
  • for i in a b c d; do echo $i; something_else $i; done
  • alias
  • All the goodies at

# And more

netstat -ano views your open TCP and UDP connections
netstat -tulp # what is listening on which port
# or lsof -i
top -b | grep processname # continuous info about a process, you have to Ctrl+C out of it though
nmap -sS -sV -O localhost # local listening ports and what versions of daemons are running.
# maybe -p 1-65535
xsel --clipboard --input # stdin to clipboard

pbcopy # stdin to clipboard

diff this that | vim -
pgrep firef
watch sensors #?

ncdu # to find out where all space is being used
htop > ps # not a redirect

# For bigger programs

diff -wyW160 this that | less  #compare side-by-side
diff -u this that >other       #write unified diff

No comments:

Post a Comment