cheat-fork-echo/cheat
2013-08-09 23:46:34 -04:00

162 lines
4 KiB
Python
Executable file

#!/usr/bin/env python
import sys
# assemble a keyphrase out of all params passed to the script
keyphrase = ' '.join(sys.argv[1:])
# create a dictionary of cheatsheets
cheatsheets = {
# @todo:
# - adb
# - apt-cache
# - dd
# - fdisk
# - mount
# - nc
# - rsync
# - shred
# - useradd / adduser
# @see: http://www.thegeekstuff.com/2010/11/50-linux-commands/
########## bash ###############################################################
'bash' : '''
To implement a for loop:
for file in `ls .`;
do echo 'file';
echo 'found';
done
@todo: if (esp. file/directory)
''',
########## convert ###########################################################
'convert' : '''
To resize an image to a fixed width and proportional height:
convert original-image.jpg -resize 100x converted-image.jpg
To resize an image to a fixed height and proportional width:
convert original-image.jpg -resize x100 converted-image.jpg
To resize an image to a fixed width and height:
convert original-image.jpg -resize 100x100 converted-image.jpg
To resize an image and simultaneously change its file type:
convert original-image.jpg -resize 100x converted-image.png
To resize all of the images within a directory:
To implement a for loop:
for file in `ls original/image/path/`;
do new_path=${file%.*};
new_file=`basename $new_path`;
convert $file -resize 150 conerted/image/path/$new_file.png;
done
''',
########## dhclient ##########################################################
'dhclient' : '''
To release the current IP address:
sudo dhclient -r
To obtain a new IP address:
sudo dhclient
Running the above in sequence is a common way of refreshing an IP.
''',
########## find ##############################################################
'find' : '''
To find files by extension (ex: .jpg):
find . -iname "*.jpg"
To find directories:
find . -type d
To find files:
find . -type f
To find files by octal permission:
find . -type f -perm 777
To find files with setuid bit set:
find . -xdev \( -perm -4000 \) -type f -print0 | xargs -0 ls -l
''',
########## git ###############################################################
'git' : '''
To set your identify:
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
To enable color:
git config --global color.ui true
''',
########## ln ################################################################
'ln' : '''
To create a symlink:
ln -s path/to/the/target/directory name-of-symlink
''',
########## netstat ###########################################################
'netstat' : '''
To view which users/processes are listening to which ports:
sudo netstat -lnptu
''',
########## sockstat ##########################################################
'sockstat' : '''
To view which users/processes are listening to which ports:
sudo sockstat -l
''',
########## ssh ###############################################################
'ssh' : '''
To execute a command on a remote server:
ssh -t user@example.com 'the-remote-command'
To tunnel an x session over SSH:
ssh -X user@example.com
To launch a specific x application over SSH:
ssh -X -t user@example.com 'chromium-browser'
For more information, see:
http://unix.stackexchange.com/q/12755/44856
''',
########## tar ###############################################################
'tar' : '''
To extract an uncompressed archive:
tar -xvf /path/to/foo.tar
To extract a .gz archive:
tar -xzvf /path/to/foo.tgz
To create a .gz archive:
tar -czvf /path/to/foo.tgz /path/to/foo/
To extract a .bz2 archive:
tar -xjvf /path/to/foo.tgz
To create a .bz2 archive:
tar -cjvf /path/to/foo.tgz /path/to/foo/
''',
}
# @todo: demonstrate some keyphrase aliases here
# print help if requested
if keyphrase in ['', 'help', '--help', '-h']:
cheatsheets_sorted = cheatsheets.keys()
cheatsheets_sorted.sort()
print "Usage: cheat [keyphrase]\n"
print "Available keyphrases:"
print "\n".join(cheatsheets_sorted)
exit()
# print the cheatsheet if it exists
print cheatsheets[keyphrase] if keyphrase in cheatsheets.keys() else 'No cheatsheet found.'