Ubuntu cheat sheet

2011-06-08 22:39:58

This is just for myself. So ignore, or use as you please, but at your own risk. This list will grow as I figure out more shortcuts. Ktnxbai.

The script ran when logging in can be found in ~/.bash_profile, however the script ran when you open a new terminal is ~/.bashrc. You can do bash commands there, but you could also use node there (provided that it's installed).
# comment


Add and show stuff to paths and other variables (put this in a login script):
export NODE_PATH="~/node_modules/jasmine-node/lib:$NODE_PATH"
echo "$NODE_PATH"

To run a command from terminal without "locking it" as long as the app runs, add & at the end. All output will still flow to this terminal (can become a little confusing).
gedit /etc/some.file
gedit /etc/some.file &

After doing so (or pressing ctrl+z to freeze a running app), to switch back to that task "thread":
You can then (for instance) ctrl+c to kill that thread or ctrl+z to freeze the thread and return to the terminal.

To launch some file with the Ubuntu associated file based on extension
gnome-open /etc/some.file

To run a script or such, prefix it with ./, it must have execution permissions
// set execution permission
chmod +x bar
// run

Show all processes
ps -A

Search for specific process
ps -A | grep sshd

Kill a process
kill 1234
// kill++
kill -9 1234

Process tree (ascii)

Check for specific programs listening to incoming ports
sudo ss -lnp | grep sshd

SSH into somewhere (ok, duh)
ssh localhost

Start sshd
sudo /etc/init.d/ssh restart

SSH and SSHd configs
gedit /etc/ssh/sshd_config
gedit /etc/ssh/ssh_config

Get your ips
ifconfig | egrep "inet|Link"

Simple network speed monitor tool:

Advanced monitoring tool (see which app is using network, requires root):

Search files for string
// straightforward
grep -r -i needle ./path
// only list files+line
grep -r -i -n -l needle ./path
// recursive, case insensitive, line numbers, ignore css files
grep -r -i -n --exclude=*.css* logg .
// ignore files, ignore dirs, find dot
grep -r -n --exclude=*css* --exclude=*json* --exclude-dir="*framework*" navigator\\. .
// content of file
grep -R '^FWS' *
// just the matching part
grep -h -o -P -r 'abc.*?def"' *

Mounting windows share:
Or on desktop, go to file, connect to server or open location (the last one being ctrl+L) to do it once but simpler.

To ping hostnames, append .local behind their "Windows" hostnames (or fix it properly ;)

Using scp (source, target must run ssh-server)
//This is the Linux scp command syntax to send file or directory to a remote computer:
scp -r /path/filename login@ip:.
// This is the Linux scp command syntax to retrieve file or directory from a remote computer:
scp -r login@ip:/path/filename .
// copy all dirs from current pwd to target (targetdir must exist)
scp -r ./* targethost:~/targetdir

Using rsync to copy two dirs while ignoring symlinks (impossible with scp). Note that this is not secure, but you can read the source to find out how to do that).
rsync -lav [user@host:]path/* [user@host:]path

Apache2 stuff (this'll need admin rights). Setup a new config file and load it. These days you'll need to install it first (sudo apt-get install apache2).
// vhost config path
// remove default config (or edit it and skip new file step below)
rm 000-default.conf
// create new file, dont forget .conf or it wont be picked up!
touch my-sites.conf
// edit config.
// then enable it...
cd ../sites-enabled
// remove old link (if you did it above)
rm 000-default.conf
// link to the file (maybe we can skip this step and just put the file here, i dunno)
ln -s ../sites-available/my-sites.conf
// restart
/etc/init.d/apache2 restart
// or
service apache2 restart
// or
restart apache2

Example alias config (not very safe, use with caution), also set permissions correctly where needed:
Alias /~ /some/dir/name
<Directory "/some/dir/name">
Options Indexes FollowSymLinks MultiViews SymLinksIfOwnerMatch
AllowOverride All
Require all granted

Open terminal: ctrl+alt+t
Lock workstation: ctrl+alt+l
Toggle hidden files in file manager: ctrl+h
Toggle location bar input/crumb path in file manager: ctrl+l

Gnome tweaker

Ubuntu Tweaker

Check if 3d is working (Not a benchmark..), part of mesa-utils package.

GTK perf test suite

If you have installers with a .run extension, you need to set their permissions to be executable. In properties of the file, in permissions, tick the box. Then double click. Or from console:
chmod +x foo.sh

OpenAL package is libopenal-dev

Fix aptana 2 installation: sudo apt-get install xulrunner-1.9.2 (very specific, I know :)
Clean up aptana 2 folder trash: check here

Lag after suspend fix: http://www.nvnews.net/vbulletin/showthread.php?t=158091

The equivalent to netstat in windows
sudo netstat -ntupl

Installing nodejs (do not use debian package, clone git instead!)
Make sure you replace the path in the last line to the proper path, where you checked out node to (try pwd at that point).
Also note that the node repo is no longer Ryan's sub repo, but that of Joyent. Old tutorials list /ry.
sudo apt-get install g++ curl libssl-dev apache2-utils
sudo apt-get install git-core

git clone https://github.com/joyent/node
cd node
sudo make install
export PATH="path-to-node-bin:$PATH"

Installing npm on top of that (note, you need to be root, that's why sudo is added to the sh):
curl http://npmjs.org/install.sh | sudo sh

Jshint after npm:
sudo npm install jshint

Next jasmine
npm install jasmine-node

After which you can require it in node
var jasmine = require("jasmine-node");

If you ever get the error configure: error: "no (requires X development libraries)", then this is the library you need to include (Which is also included in the gtk+-3.0 package btw.):
sudo apt-get install xorg-dev

Reset sound after it becomes digitally wonky (happens a lot on my system, root problem seems to be some kind of system load..):
alsa force-reload

Reset microphone volume. Works around bug where one channel of microphone is reset after suspend. (source):
pactl set-source-volume 1 0dB 0dB
// can verify in `alsamixer` or whatever, level should update immediately

Create microphone loopback (source):
pactl load-module module-loopback latency_msec=1
// you can now find the loopback under "recording" tab in sound (`pavucontrol`)

// to remove:
pactl unload-module module-loopback

Mount ssh connection as local drive ("sftp") (source). You need some kind of root for this. I'll use sudo for this.
sudo cd /media
sudo mkdir sftpdir
sudo chown your-user-name /media/sftpdir
sudo adduser your-user-name fuse
sshfs user@example.com:/stuff /media/sftpdir

Read/write clipboard (hexdump for analyzing bytes)
xclip -o | hexdump -C
echo -n $'a\r\nb' | xclip -i -selection clip-board

Find correct drive and format SD card (or whatever)

Find the device that holds your SD card. It'll look something like /dev/sdd1. The descriptions are of course quite important here ;) Once you have it, unmount and format the drive (since it's for a camera, I'm formatting it in fat32). You need root for this (or sudo).
sudo umount /dev/sdd1
sudo mkdosfs -F 32 -v /dev/sdd1

June 2014: Had problems creating bootable usb drives. Only way I could seem to do it was by gnome-disks, which is installed with gnome-disk-utility. When gnome-disks is open, select the usb drive, click the "options" icon (top-right corner or whatever) and select "restore disk image". This allows you to select an image and restore it to the usb drive, make it bootable (if the image is). Alternative ways seemed to fail for me right now, randomly freezing in the process.

Old: To "burn" an iso to usb (or to create a bootabla usb drive from an image). This one's pretty damn hard to find unless you know where to look...

(There's actually a bug where you might end up with the message: "An uncaught exception was raised:Invalid version string 'GNU/Linux'" when trying to build a linux disk this way, in that case you need to add a flag. It will still throw the error, but the usb will actually work after it. Note that this option actually allows you to pick ANY drive, so be very careful and make sure you pick the usb drive! source)
sudo usb-creator-gtk --allow-system-internal

Rename a drive:
sudo mlabel -i /dev/sdc1 ::<new_label>

Combine multiple pdfs into one... (yeah, needed that once). Source
sudo apt-get install gs pdftk
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=combinedpdf.pdf -dBATCH 1.pdf 2.pdf 3.pdf

To get rid of certain default entries in the notifier (the envelope), here's what you do (reboot afterwards). For other entries, you'll have to go to /usr/share/indicators/messages/applications and remove (sudo rm ...) entries manually.
// broadcast accounts (facebook, twitter, etc)
sudo apt-get remove evolution
// default instant messenger (yahoo, msn, etc)
sudo apt-get remove empathy
// remove the envelope icon from systray
sudo apt-get remove indicator-messages

Codec stuff. Actually just from here.
sudo apt-get install libxine1-ffmpeg gxine mencoder mpeg2dec vorbis-tools id3v2 mpg321 mpg123 libflac++6 ffmpeg libmp4v2-0 totem-mozilla icedax tagtool easytag id3tool lame nautilus-script-audio-convert libmad0 libjpeg-progs libquicktime1 flac faac faad sox ffmpeg2theora libmpeg2-4 uudeview flac libmpeg3-1 mpeg3-utils mpegdemux liba52-dev
sudo apt-get install gstreamer0.10-ffmpeg gstreamer0.10-fluendo-mp3 gstreamer0.10-gnonlin gstreamer0.10-sdl gstreamer0.10-plugins-bad-multiverse gstreamer0.10-schroedinger gstreamer0.10-plugins-ugly-multiverse totem-gstreamer

Other stuff (for me) to install: dropbox, pidgin, deluge (dont forget to disable DHT!), keepassx, chrome, (firefox), opera, htop, gkrellm, mint-menu, flash, xchat, skype, webstorm.

Ubuntu tweak, if that's still relevant:
sudo add-apt-repository ppa:ubuntu-tweak-testing/ppa
sudo apt-get update
sudo apt-get install ubuntu-tweak´╗┐
sudo apt-get install compizconfig-settings-manager

Enable case-insensitive file/directory tabbing. I needed to open a new terminal for this setting to take effect. Afterwards you can do cd de and tab to jump to Desktop.
sudo nano /etc/inputrc // can also be ~/.inputrc ...
// now add the next line at the end of the file
set completion-ignore-case on

Enabling .htaccess in Apache2 (for Ubuntu). Simply edit /etc/apache2/sites.enabled/default and change Override None to Override All. (Source)

Change and flush DNS settings:
sudo vi /etc/resolv.conf
sudo /etc/init.d/dns-clean start

Set up vpn... First install sudo apt-get install network-manager-openvpn, then go to network icon in systray. Go to VPN connections. In VPN tab, import magic settings (sorry, I received these from other people). Close the popup. Reconnect your current connection (otherwise the vpn option wont show). In the systray, open the network icon, go to vpn and there should be your new VPN connection. To use the VPN only for specific domains, go to the network panel, vpn tab, ip4 settings, click routes button, and select "Use this connection only for resources on its network". Now you can keep skyping and googling while still able to reach resources on your vpn :)

Screen sharing: http://www.mikogo.de/download/linux-download/
Browser page (only) sharing: surfly

Install a BNC (ZNC): https://gist.github.com/1409670

Compare two binary files with vbindiff.

Make OSD (system) notifications use active screen:
gconftool-2 -s -t string /apps/notify-osd/multihead_mode focus-follow

Check audio channels (sends sound to each speaker consecutively) (source):
speaker-test -Dplug:surround51 -c6 -l1 -twav

Fix flash player inverted colors on youtube et.al. (source, worked for me)
sudo mkdir /etc/adobe
echo -e "EnableLinuxHWVideoDecode=1\nOverrideGPUValidation=true" | sudo tee /etc/adobe/mms.cfg > /dev/null

Fix Deluge (bittorrent client) from crashing your router, especially if it only affects your cable but not your wireless: Disable dht in settings. This probably causes your router to crash your NAT table.

Update DNS to be faster. In /etc/nsswitch.conf move the dns entry forward:
hosts: files dns wins mdns4_minimal [NOTFOUND=return] mdns4

Fix a very broken package that blocks apt-get. In my case, I tried reverting to an older version of gnome-screenshot, but that kind of backfired because it broke apt-get due to a cyclic dependency. So I could not update the system and could not use the update system to fix that. In fact, it turned out apt-get could not be used to fix the problem (even though it caused it) and I had to fall back to dpkg. This was the only way for me to remove the bad package (immediately added after that). Ps. the source lists an even more extreme method if this doesn't work for ya...
dpkg --remove --force-all gnome-screenshot

Python webserver onliner:
python -c "import SimpleHTTPServer;SimpleHTTPServer.test()"

Enable line-in as sound output (source):
pactl load-module module-loopback

Convert movie to animated gif, using ffmpeg and gifsicle:
Note: the -r 30 is the converted frame rate of the movie, gif is max 30 fps and --delay=1 should be set to an integer such that delay*fps=30.
sudo apt-get install ffmpeg
sudo apt-get install gifsicle
ffmpeg -i input.ogv -pix_fmt rgb24 -r 30 -f gif - | gifsicle --optimize=3 --delay=1 > output.gif

Remove ^M from files (to get ^M as a char in terminal press ctrl+v and then ctrl+M). (Note that -i means "inline" editing, so no output file part required...) :
sed -i 's/^M$//' inputfile

Stop a service from starting after boot (I needed the port php5-fm was listening to):
sudo update-rc.d -f php5-fm remove
// the actual scripts can be found in /etc/init.d/*

Add custom app to launcher favorites (ubuntu 12.12, gnome3). Big clusterfuck, but here we go (source):
sudo gnome-desktop-item-edit /usr/share/applications/ --create-new
// create the launcher (dont forget to set the icon). After that
// search for the launcher name in applications. Right-mouse on
// the search result and select "add to favorites". Done

Colored syslog:
gtc tail -f /var/log/syslog

Remove dupes from Banshee playlist, using sqlite3 (source). Groups by track and artist title, lowercased.

// Show what you're about to remove (count, title, artist):
select c,t,n from (select count(trackid) as c, coretracks.titlelowered as t, coreartists.namelowered as n from coretracks left join coreartists where coretracks.artistid = coreartists.artistid group by t,n) where c > 1;

// Delete dupes (one dupe per group at a time, so repeat a few times):
delete from coretracks where trackid in (select i from (select trackid as i, count(trackid) as c, coretracks.titlelowered as t, coreartists.namelowered as n from coretracks left join coreartists where coretracks.artistid = coreartists.artistid group by t,n) where c > 1);

// Exit from sqlite3

Fixed network hoo-ha at some point:
sudo ifdown eth0
sudo ifup eth0 -v

Set fancy terminal prompt. I also use this to clarify ssh prompts on remote servers, to make them stand out from a local prompt (has to be done on the remote server in that case). Edit ~/.bashrc, find the $color_prompt part and use the following. It will print the current dir from ~ prefixed by the current branch if in a git repo. Modify to your own likings/colors:
# find this line and enable it

# then find this line later and replace the whole if-else
if [ "$color_prompt" = yes ]; then
#export PS1='\u:\W$(__git_ps1 " (\[\033[0;35m\]git:\[\033[0;34m\]%s\[\033[0m\])")\$ '
# PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
PS1='$(__git_ps1 "(\[\033[01;32m\]%s\[\033[0m\]) ")\[\033[01;34m\]\w\[\033[00m\]\$ '
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

Quick monitor setup scripts: arandr then bind them in config.

Suspend from cli:
sudo pm-suspend

Burn image to usb, easy GUI way. Two different programs doing the same thing. For me the GTK version isn't working properly, but your results may vary. For the KDE version the UI doesn't update when it's done (so mouse keeps stuck in waiting mode) but after a while you can just select one of the few (same) usb entries it creates and burn the image. That does work and creates a bootable usb.
sudo usb-creator-kde
// or
sudo usb-creator-gtk

Burn image to usb, CLI way (source). Confirmed to work as well and create bootable usb.
// find usb using
sudo fdisk -l
// (probably something like /dev/sde or /dev/sdf but it depends on your disks/system/config,
// ALWAYS MAKE SURE TO FIND THE USB! Consider yourself warned.)

// Umount _that_ device
sudo umount /dev/sdf1

// format if new drive, but you probably dont need to do this
sudo mkdosfs -n 'USB-Drive-Name' -I /dev/sdf -F 32

// copy the image, note: make sure you use the usb disk here!
sudo dd if=filename.iso of=/dev/sfc bs=4k

// make OS flush all pending writes to disk (/usb)
// unmount and safely remove disk
sudo eject /dev/sdf

resolve windows hostnames (source):
# maybe not, it only worked for me after installing the next one so not sure if you need this
sudo apt-get install winbind
sudo apt-get install libnss-winbind

# if ping doesnt work yet, update conf:
sudo nano /etc/nsswitch.cfg

# update hosts line to something like this (must contain winbind somewhere)
hosts files dns winbind

ping foo

Determine bandwidth usage per process:
sudo apt-get install nethogs
sudo nethogs

Remount something and change read/write state:
sudo mount -o remount,rw /media/endpoint

Network seems capped at 1mbs but wifi on same router is faster? Check and set eth0 speed (source):
sudo ethtool -s eth0 speed 100 duplex full
sudo ethtool eth0

// maybe also
sudo ethtool -s eth0 speed 10 autoneg off

Reduce mouse sensitivity (source):
// find your mouse
xinput list
// below "10" is the id of the mouse, "5" is the value
xinput --set-prop 10 "Device Accel Constant Deceleration" 5

Auto clicker. For... reasons. May need to apt-get it. Probably.
xdotool click --repeat 100000 1

Record screencast in gif using byzanz (source, also has nice gui script):
sudo apt-get install byzanz
byzanz-record -v -d 10 --delay=5 -x 0 -y 0 -w 1440 -h 900 desktop-animation.gif

Crop an animated gif with gifsicle:
sudo apt-get install gifsicle
gifsicle --crop 50,50-100,100 input.gif > output.gif

Remap audio ports (source):
sudo apt-get install alsa-tools-gui

Run script on thaw (source and example):
sudo nano /etc/pm/sleep.d/99_some_name
sudo chmod +x /etc/pm/sleep.d/99_some_name



case "${1}" in
# this is where you put something you want to run before hibernating
# this is where you put something you want to run on resume/thaw

ffmpeg stuff:
// Use ffmpeg to merge two audio tracks of a video together and mix them to mono...
// (source and source)
ffmpeg -i in.mkv -c:v copy -ac 1 -filter_complex "[0:2] [0:3] amerge" out.mkv

// same as above and also set volumes per input stream (source)
// (note that `-ac 1` makes the tracks mix into stereo)
// (note that the audio must be re-encoded or otherwise the volume filters will be ignored)
// (source)
ffmpeg -i in.mkv -ac 1 -filter_complex "[0:2]volume=0.3:precision=float[A]; [0:3]volume=0.8:precision=float[B]; [A][B]amerge" -y out.mkv

// mute part of one audio track while keeping other audio track as is, mix them together. result has 1 video and 1 audio
ffmpeg -i in.mkv -c:v:0 copy -filter_complex "[0:a:1]volume=enable='between(t,300,390)':volume=0[A]; [A][0:a:2]amix[out]" -map 0:v -map [out] -y out.mkv

// partially slipstream the audio of one file into the audio/video of another. like adding an outro message after recording.
// in this case the main video ran for about 17 minutes and after that i replaced the remaining audio of only one track with that of outro.mkv
// - extract target audio track, partially (-ss is start, -t is duration, -to is stop)
// - concat replacement audio track to it
// - remux new track into video, resulting in 1 video and 1 audio track
// - ... profit
ffmpeg -i in.mkv -ss 0 -t 1126 -map 0:a:1 -y track.mp3
ffmpeg -i track.mp3 -i outro.mkv -filter_complex "[0:a:0] [1:a:2] concat=n=2:v=0:a=1 [A]" -map [A] -y mixed.mkv
// "original movie, mixed mic track, copy video, merge second audio track of original with mixed mic track into out"
// Note that it's using `amix`! The `amerge` method was causing the mic track to get a lower volume. Whatever.
ffmpeg -i in.mkv -i mixed.mkv -to 00:20:15 -c:v:0 copy -filter_complex "[0:a:1][1:a:0]amix" -y out.mkv
// BUT, we can combine the first two into in a single line
ffmpeg -i in.mkv -i outro.mkv -filter_complex "[0:a:2]atrim=duration=1126[A]; [A] [1:a:2] concat=n=2:v=0:a=1 [B]" -map [B] -y mixed.mkv
// and by the same logic we can combine the whole thing into a one liner:
// this:
// - has two input files (the original video and the a video with the outro), both have 1v and 3a
// - cuts the video at 20:15, the original video ran longer than that but i don't need the trailing part
// - copies the video as is. This speeds things up considerably.
// - copies the first 18 minutes of mic audio track into a stream A
// - concats the A stream to the outro, forming stream B
// - mixes the new mic stream B into the background audio of the original video into stream C
// - outputs a new file with video as first stream and our mixed audio stream C in second place
ffmpeg -i in.mkv -i outro.mkv -to 00:20:15 -c:v:0 copy -filter_complex "[0:a:2]atrim=duration=1126[A]; [A] [1:a:2] concat=n=2:v=0:a=1 [B]; [0:a:1][B]amix[C]" -map 0:v -map [C] -y out.mkv

// quickly split up an mkv of, say two hours into four half hours
mkvmerge -o 1.mkv --split parts:-00:30:00 in.mkv
mkvmerge -o 2.mkv --split parts:00:30:00-01:00:00 in.mkv
mkvmerge -o 3.mkv --split parts:01:00:00-01:30:00 in.mkv
mkvmerge -o 4.mkv --split parts:01:30:00- in.mkv

// to merge two files with specific offsets use mkvmerge as ffmpeg gives me crap for it.
// note that the timestamps in --split are for the entire duration of all inputs concat.
// this way you can cut away some trailing/leading time to glue it properly together
mkvmerge -o out.mkv --split parts:-00:06:34,+00:07:28- a.mkv +b.mkv

// add a black area to your video
//ffmpeg -i in.mkv -vf "color=black:WIDTHxHEIGHT [over]; [in][over] overlay=X:Y [out]" out.mkv
ffmpeg -i in.mkv -vf "color=black:1920x100 [over]; [in][over] overlay=0:1000 [out]" out.mkv
// then use mkvmerge to recombine them