Apr/10 3 1
BASH cheat sheet
This is my cheat sheet BASH. I'm here to collect useful code snippets and constructs that I had to google from time to time or otherwise man'en. The article serves me personally as a reference book but could be useful to others who often have the Bourne-again shell to do.
Grind
while [1] # Endless loop do echo "Blah" done
for i in $ (ls / etc) do echo $ i done for i in `seq 1 10`; do echo $ i done
Case
case "$ 1" in a) echo "a" ;; b) echo b " ;; *) echo "$ 1 is not a or b" ;; esac
Switches for Test: http://bash-hackers.org/wiki/doku.php/commands/classictest or / Usr / bin / [- help
If-Then-Else
if ["$ 1" = "a"] then echo "a" else echo "not a" fi
dpkg + apt
# List files installed from a package dpkg-L [package name] # List files IN a package dpkg-c [package.deb] # Info about a package dpkg-I [package.deb] # List all installed packages dpkg-l dpkg - get-selections> getselections.txt Installation # import from file selection dpkg - set-selections <getselections.txt
netstat
# Lists all open ports to the outside netstat-lpn | grep LISTEN | grep-v localhost
mdadm
# Test your email # Uses MAILADDR from / etc / mdadm / mdadm.conf mdadm - monitor -1 / dev/md0-t # Device to add to the array mdadm - add / dev/md0 / dev/sda1 # Remove device from the array mdadm - remove / dev/md0 / dev/sda1 # Remove Faulty Devices mdadm - manage / dev/md0 - remove faulty mdadm - manage / dev/md0 - remove failed # Re-install boot loader grub grub> root (hd0, 0) grub> setup (hd0) # Disk information output smartctl-i / dev / sdx
ext
# Disklabel set e2label device label
Screen
# Screens to automatically generate scripts and task start screen screen-S [screen name]-d-m [script.sh] # Create a new screen or to change existing screen-R-d [screen name] # Keystroke to exit a screen CTRL - A - D
mysql
# All databases into a dump file mysqldump-u root-psecret - all-databases> backup.sql # Only the database structure into a dump file mysqldump-u root-psecret - no-data - all-databases> backup.sql # Dump Restore mysql-u root-psecret <backup.sql # root password reset / etc / init.d / mysql stop echo "SET PASSWORD FOR 'root' @ 'localhost' = PASSWORD ('new password')"> ~ / setrootpassword.sql mysqld_safe - init-file = ~ / setrootpassword.sql # [ctrl] + [c] / etc / init.d / mysql start rm-f ~ / setrootpassword.sql imagemagick
# Resize all images in current directory mogrify-resize 800x600. / *. JPG # All images in the current directory to PNG convert mogrify-format png. / *
unionfs-fuse
# Union between a and b in c # Will be posted on a unionfs-fuse-o cow / home / a RW = :/ home / b = RW / home / ww / Desktop / c
ffmpeg
# Convert flv to 3gp ffmpeg-i-f 3gp-vcodec in.flv h263-s 176x144-ab 12.2k-ar 8000-ac 1 out.3gp # Convert from just about any format to any other as loss free as possible ffmpeg-i infile.ext-pass 2-sameq outfile.ext
find
# Find files, and do something with them find. -Name "* mp3."-Exec file {} \; # Soft links can view and find. -Type l-exec ls-l {} \; # Include all files in a directory find / directory-type f | wc-l
sudo
# / Etc / sudoers # User username password command without a permit execute a privileged # (Appended at the very end must be from / etc / sudoers) username ALL = NOPASSWD: / sbin / reboot
iptables
# Allow everything on INPUT iptables-F; iptables-P INPUT ACCEPT # Debug connections # Packet counters in a chain to 0 iptables-Z INPUT # Chain with counter display package iptables-L INPUT-v
FTP mount
mkdir / tmp / mnt modprobe fuse curlftpfs-v-o nonempty ftp.host.com-o user = username: password / tmp / mnt umount / tmp / mnt
umount "device is busy" - find-blocking process
fuser-m [mountpoint] wget mirrorn website
wget-km http://www.website.com SAMBA mount
mount.cifs / / 192.168.0.1/share / media / share-o username = $ USERNAME, password = $ PASSWORD 32bit or 64bit
getconf LONG_BIT / Etc / fstab
NFS: 192.168.0.1 :/ home / home nfs rsize = 8192 wsize = 8192, hard, intr 0 0
Disk Full - Find memory hogs
apt-get install ncdu ncdu # / Mounts without any other FS / mnt # (If data cover mounts on the local disk) mnt / bind / - mount, du-sm / mnt
Full encryption of the system
Very good tutorial by Andreas Haerter: http://blog.andreas-haerter.com/2011/06/18/ubuntu-festplattenvollverschluesselung-lvm-luks
Disk via the LAN cloning
dd if = / dev / hda | ssh-c blowfish-C root@otherbox.ip.addr dd of = / dev / hda Current UNIX TIMESTAMP output (ET)
date +% s Asterisk
# Convert sound file in Asterisk playable format ffmpeg-i-ar 8000-ac ./input.mp3 1 from 64. / output.wav

April 8th, 2010
Grinding:
- Endless while: while true; ...
- Ls parsing is a kind of no-no: cd / etc for i in *; ... or simply if applicable / etc / *. The growth rates of them forever.
Case:
- Not quoting word of the test parameters as needed (but not incorrect)
Test:
- When using / usr / bin / [is used for coding, and / usr / bin / [used instead of [(builtin bash)
- Preferred: the [[]] Keyword (bash)
Imagemagick:
-.. / [Jj] [Pp] [Gg], or help another method for Case-Insensitivity may