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).
Code:
#!/bin/sh
# comment
stuff

#!/usr/local/bin/node
require("jasmine-node");


Add and show stuff to paths and other variables (put this in a login script):
Code:
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).
Code:
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":
Code:
fg
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
Code:
gnome-open /etc/some.file


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


Show all processes
Code:
ps -A


Search for specific process
Code:
ps -A | grep sshd


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


Process tree (ascii)
Code:
pstree


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


SSH into somewhere (ok, duh)
Code:
ssh localhost


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


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


Get your ips
Code:
ifconfig | egrep "inet|Link"


Simple network speed monitor tool:
Code:
bwm-ng

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


Search files for string
Code:
// 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:
http://industriousone.com/blog/mounting-windows-shares-linux
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)
Code:
//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).
Code:
rsync -lav [user@host:]path/* [user@host:]path


Apache2 stuff (this'll need admin rights). Setup a new config file and load it.
Code:
// vhost config path
/etc/apache2/sites-available/
// 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

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


Keys:
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
Code:
gconf-editor


Ubuntu Tweaker
http://ubuntu-tweak.com/

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


GTK perf test suite
Code:
gtkperf


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:
Code:
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
Code:
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.
Code:
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
./configure
make
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):
Code:
curl http://npmjs.org/install.sh | sudo sh


Jshint after npm:
Code:
sudo npm install jshint


Next jasmine
Code:
npm install jasmine-node

After which you can require it in node
Code:
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.):
Code:
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..):
Code:
alsa force-reload


Mount ssh connection as local drive ("sftp") (source). You need some kind of root for this. I'll use sudo for this.
Code:
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)
Code:
xclip -o | hexdump -C
echo -n $'a\r\nb' | xclip -i -selection clip-board


Find correct drive and format SD card (or whatever)
Code:
df

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).
Code:
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...
Code:
usb-creator-gtk
usb-creator-kde

(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)
Code:
sudo usb-creator-gtk --allow-system-internal


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


Combine multiple pdfs into one... (yeah, needed that once). Source
Code:
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.
Code:
// broadcast accounts (facebook, twitter, etc)
sudo apt-get remove evolution
// default instant messenger (yahoo, msn, etc)
sudo apt-get remove empathy


Codec stuff. Actually just from here.
Code:
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:
Code:
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.
Code:
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:
Code:
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:
Code:
gconftool-2 -s -t string /apps/notify-osd/multihead_mode focus-follow


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


Fix flash player inverted colors on youtube et.al. (source, worked for me)
Code:
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:
Code:
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...
Code:
dpkg --remove --force-all gnome-screenshot


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


Enable line-in as sound output (source):
Code:
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.
Code:
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...) :
Code:
sed -i 's/^M$//' inputfile


Stop a service from starting after boot (I needed the port php5-fm was listening to):
Code:
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):
Code:
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:
Code:
gtc tail -f /var/log/syslog


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

Code:
// 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
.exit


Fixed network hoo-ha at some point:
Code:
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:
Code:
# find this line and enable it
force_color_prompt=yes

# 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\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi


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