Linux / Ubuntu Bash Commands
Copying a directory: cp -R source target
Removing a directory: rm -rf dir
—–
Mounting directory:
sudo mkdir /media/www
sudo mount //server/www /media/www
In /etc/fstab:
//server/www /media/www smb defaults 0 0
In /etc/hosts: 192.168.0.1 server
Using cifs with username and password in .smbcredentials:
//server/www /media/www cifs credentials=/root/.smbcredentials,iocharset=utf8 0 0
Ntfs-3g example usage:
/dev/sda2 /media/disk ntfs-3g defaults,nls=utf8,umask=007,gid=46 0 1
Reloading fstab: mount -a
—–
Compress/Decompress:
tar -cjf backup.tar.bz2 directory
tar -xjf backup.tar.bz2
tar -cvf tarname directory/
gzip tarname.tar
tar -xzvf tarname.tar.gz
Uncompress gz: tar -zxvf file.tar.gz
Uncompress zip: unzip file.zip
Uncompress/Untar tar file: tar -xvwf myfile.tar
—–
Restart System / Apache / PostgreSQL / MySQL
shutdown -r now
sudo /etc/init.d/apache2 restart
sudo /etc/init.d/postgresql-8.2 restart
sudo /etc/init.d/mysql restart
—–
IP:
ifconfig / iwconfig
ip addr show
ip route
—–
Restart X-Server: Ctrl-Alt-BackSpace
—–
Linking:
ln -s target/path name
—–
All services: ps aux
Filtered services: ps aux | grep servicename
Search the whole file system: find / -name filename
Copy dir: cp -r dir/* dest/dir
Example /etc/network/interfaces:
auto lo
iface lo inet loopback
iface eth0 inet dhcp
address 192.168.1.22
netmask 255.255.255.0
gateway 192.168.1.254
auto eth0
Network info: sudo ifconfig
VI: ESC to get into command mode. In command mode use i to insert text, ESC to get back to command mode. Get out and save by typing :wq.
——
General hardware info: lspci
——
My current /etc/hosts file:
127.0.0.1 localhost
127.0.1.1 henrik-desktop
192.168.0.1 server
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
——
Location of Grub menu file: /boot/grub/menu.lst
——
find /home -name ‘*.avi’
chmod 777 folder
——
Shutting down / restarting:
sudo shutdown -P now
sudo reboot
——
Email:
Deleting the whole Postfix queue: postsuper -d ALL
Check how many mails in queue: mailq | tail -n 1
——
Checking folder info: du -hs dir
Checking Ubuntu version: lsb_release -a
Checking Kernel version: uname -r
Vacuuming Firefox database:
3.0.*
for f in ~/.mozilla/firefox/*/*.sqlite; do sqlite3 $f 'VACUUM;'; done
3.5.*
for f in ~/.mozilla/firefox-3.5/*/*.sqlite; do sqlite3 $f 'VACUUM;'; done
——
Pushing keys to remote server to be able to ssh without entering password all the time:
ssh-keygen -t dsa (accept all the defaults)
cat ~/.ssh/id_dsa.pub | ssh root@domain.com 'mkdir .ssh; cd .ssh; cat >> authorized_keys; chmod 600 authorized_keys'
Note: root@domain.com, it needs to be replaced with the server info in question and step 1 is only needed once. If you do it again and use default values you will overwrite your existing keys, not good.
SSH with non-default port: ssh -p 2922 admin@domain.com
Killing a process by name, 4 different methods from how to geek, I added the last one which will kill the process no matter what:
kill $(pgrep irssi)
killall -v irssi
pkill irssi
kill `ps -ef | grep irssi | grep -v grep | awk ‘{print $2}’`
killall -9 -v irssi
Listing all big files / folders:
find . -type f -size +10000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
Getting my twitter timeline with CURL and saving it to twitter.xml with a 60 second timeout.
curl -u hsarvell:password -m 60 -o twitter.xml http://twitter.com/statuses/friends_timeline.xml
Posting with CURL:
curl -d "param1=value1¶m2=value2" http://example.com/script.php
Dumping MySQL database:
mysqldump --databases dbname > /some/folder/dbname.sql
or verbose:
mysqldump databasename tablename --user userid --password > /some/folder/dbname.sql
bzip2 dbname.sql
Importing a dumped MySQL database with the file being encoded in utf8:
mysql -u username -p --default_character_set utf8 dbase < file.sql
Printing computer specs (as root): lshw -html > specs.html
Turning off touchpad: synclient TouchpadOff=1
Compiling 32bit binaries on a 64bit system:
1.) sudo apt-get install gcc-multilib libc6-i386
2.) Use the -m32 flag.
Checking which ports are busy and what’s running behind them:
sudo apt-get install nmap
nmap -p 1-65535 -sV 127.0.0.1
Checking if any process is using port 80: netstat -anl | grep :80
Short note on how to setup new mail / change password on existing mail with Postfix and Courier:
cd /etc/postfix
nano access
nano virtual
cd virtual_domains
nano domain.com
cd /etc/courier/userdb
nano domain.com (add a hash for a password you already know)
cd /usr/lib/courier
perl makeuserdb
/etc/init.d/postfix restart
Adding a directory to your path, nano ~/.bashrc and:
PATH=$PATH:/opt/clojure
export PATH
What is my ip from the shell: wget -qO- whatismyip.org
——-
Undoing an arbitrary Mercurial commit:
hg log -l 10 -v
hg backout --merge -m 'get back userinfo' 516
The first line shows a verbose log so you can get at the changeset you want to undo by way of id, the second line will undo the change, in this case the changeset with id 516.
Using the output from the log command you can also do hg strip 170 where 170 is the changeset id to undo everything up to and including the id in question.
To get hg strip to work you add this in the .hg/hgrc file:
[extensions]
mq =
Pretend you had changes you didn’t want to lose before doing the hg update -C command above, then you could do:
hg export 171 >~/Desktop/171.txt
followed by this after performing the undo:
hg import ~/Desktop/171.txt
For more info on when the above could be needed see David Herron’s multiple head problem post.
To stop tracking a file you can do hg forget, if you don’t have forget the original is hg remove -Af.
——-
Removing passphrase from ssl key: openssl rsa -in server.key -out server2.key
——-
Recovering files:
grep -a -B 25 -A 100 ’some string in the file’ /dev/sda1 > results.txt
Where 25 is the amount of lines before the match to output, 100 after and /dev/sda1 is the partition the file resided on, original here.
——-
Nice tshark line:
tshark -i wlan0 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)' -R 'http.request.method == "GET" || http.request.method == "HEAD"'
——-
Starting a tied off process and suppressing output:
sh -c "some command" > /dev/null 2>&1 &
How to install curl in PHP (from Ivan Kristianto):
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl php5-mcrypt
The apt-get sources file, for instance changing all instances of intrepid with lucid can do wonders for your ubuntu install
:
nano /etc/apt/sources.list
Splitting a large text file:
wc -l large.txt
split -l 602566 large.txt
The first wc command will output the amount of lines in the text file and the second split command splits it into 602566 lines per new file.
The best results for converting a PDF to HTML with the PDFtoHTML command: pdftohtml -i -c -noframes 5814.pdf 5814.html. PDFtoHTML can be installed by running apt-get install poppler-utils.
Replace karmic with lucid in a file using sed:
sed -i 's/karmic/lucid/g' /etc/apt/sources.list
scp root@domain.com:/opt/lib/file.php /opt/file.php
scp -r root@domain.com:/opt/lib /opt
The first line copies a file and the second a whole directory in a recursive fashion.
To see port usage: netstat -ntpl
Listing all text files that contains linux in a recursive and case insensitive fashion:
grep --color=auto -iRnH 'linux' *.txt
Use less to stagger output, for instance one window at a time like this: ps aux | less, and then use space to cycle through the output one screen at a time, q to quit.
Get CPU info / see number of cores: cat /proc/cpuinfo
Get info on if the system is 32 or 64 bit: uname -m
Restart Compiz:
ALT + F2 -and then compiz --replace as the command.