Linux / Ubuntu Bash Commands

Copying a directory: cp -R source target
Copying a directory and force overwrite: /bin/cp -Rfu 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

Uncompress straight bz2 file without deleting the original: bzip2 -dk file.bz2

—–

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

Search all PHP files in /var/www/ for blabla:

find /var/www/ -type f -name "*.php" | xargs grep 'blabla'

Copy all pdf files in the local folder physics, recursively, to another folder:

find physics -name "*.pdf" -print0 | xargs -0 -I {} cp {} /feting/Downloads/physics/

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.

——
Mass find replace of foo with bar in all .txt files, in the /foo/bar directory:

find /foo/bar -name \*.txt -exec sed -i "s/foo/bar/g" {} \;

——

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&param2=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.

Undo a hg add of a lot of files: hg status -an0 | xargs -0 hg revert

Undo a commit of one file, if tip is has rev number 223, it doesn’t matter if nothing was changed in rev 222:

hg revert -r 222 file.php
hg commit -m "put file back"

To resolve a merge conflict just fix the issue (I usually just replace the file with the contents of what I try to upload) and run hg resolve -m test.h.

——-

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 usage:

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.

Uploading files and changing permissions:

scp zs.png root@domain.com:/dir/to/upload/to
ssh root@domain.com chmod 644 /dir/to/upload/to/zs.png

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.

Search for Linux recursively in the content of all files:

find . -type f | xargs grep "Linux"

Rsync a directory via SSH without recursion (if you want recursion change -dlptgoDvz to -avz) :

sudo rsync -dlptgoDvz -e ssh root@domain.com:/var/www/domain.com/file_uploads/ /var/www/domain/file_uploads/

Checking MegaCLI hardware RAID status: megaclisas-status

Turning on/off SSH password login in /etc/ssh/sshd_config set PasswordAuthentication to yes/no.

To see status of software RAID: cat /proc/mdstat

To step through a very large text file: less big-ass-file.txt

View line 3371 in a very large text file:

sed -n '3371,3371p' big.sql

Get line wrapping in Nano: nano -w -$ filename

Connecting to remote mysql server:

mysql --host=ip --user=myname --password=yourpwd mydb

Rename all .jpeg files to .jpg:

rename 's/\.jpeg$/\.jpg/' *.jpg

Add hello to the beginning of all files:

rename 's/^/hello_/' *.*

Set system time, second line sets the hardware clock with the help of the system time which was set on the first line, the third line does the opposite:

date -s "12 JUL 2013 06:37:00"
hwclock --systohc
hwclock --hctosys

Change timezone:

dpkg-reconfigure tzdata
/etc/init.d/cron stop
/etc/init.d/cron start

Show the most CPU intensive processes at the top of a ps aux:

ps -eo pcpu,pid,user,args | sort -k 1 -r | head -6

OR:

ps -Ao user,uid,comm,pid,pcpu,tty --sort=-pcpu | head -n 1000 > processes.txt

Show all partitions / HDs: lsblk -o NAME,FSTYPE,SIZE,MOUNTPOINT,LABEL

Show available space on all HDs: df -h

Show all pil processes with parent process id: ps -H -o pid,ppid,start,size,pcpu,wchan,cmd -C pil

Give read, write and execute permissions to all file in current folder, deny everyone else: chmod u=rwx,g=r,o= *

Generating a SHA256 Certificate Signing Request with accompanying key:

openssl req -nodes -newkey rsa:2048 -sha256 -keyout myserver.key -out server.csr

Lines for generating MySQL certificate files that work, just put them in /etc/mysql to avoid issues with for instance Apparmor:

openssl genrsa 2048 > ca-key.pem
openssl req -new -x509 -nodes -days 100000 -key ca-key.pem -out ca-cert.pem 
openssl req -newkey rsa:2048 -days 100000 -nodes -keyout server-key.pem -out server-req.pem
openssl rsa -in server-key.pem -out server-key.pem
openssl x509 -req -in server-req.pem -days 100000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem 
openssl req -newkey rsa:2048 -days 1000 -nodes -keyout client-key.pem -out client-req.pem
openssl rsa -in client-key.pem -out client-key.pem
openssl x509 -req -in client-req.pem -days 100000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem

To revert to US layout if you end up suddenly writing things like ßðđŋßðđŋðđŋ: setxkbmap -layout us

Starting yEd: java -jar “yed.jar”

Kill by grep:

ps -ef | grep orion | grep -v grep | awk '{print $2}' | xargs kill -9

If you’re having issues with name resolution (works in Ubuntu 14.04):
1.) nano /etc/resolvconf/resolv.conf.d/tail -> add the nameserver IP lines you want.
2.) resolvconf -u

Removing the top 42 lines from a mysql dump:

sed '1,42d' test.sql > test2.sql

List all files with octal permissions in current directory:

stat -c "%a %n" *

Monitor / measure disk I/O:

iostat -x 1

Encrypting an external USB drive:

cryptsetup -v --verify-passphrase luksFormat /dev/sdf1
cryptsetup luksOpen /dev/sdf1 Maxtor
mkfs.ext4 /dev/mapper/Maxtor
cryptsetup luksClose /dev/mapper/Maxtor

Creating RSA key pair, no passphrase, and get modulus for using with JS libs:

openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
openssl rsa -in private.pem -noout -modulus

Checking open TCP/IP ports:

netstat -atn

Listing files with octal permissions:

stat -c "%a %n" *

Replacing text in a huge text file (SQL dump files come to mind):

sed -i 's/old text/new text/g' file