Attempting to cheatsheets out of application logic.

This commit is contained in:
Chris Lane 2013-08-10 17:08:17 -04:00
parent 8e9cfc0eb1
commit cbbd18e621
3 changed files with 238 additions and 233 deletions

230
.cheat Normal file
View File

@ -0,0 +1,230 @@
cheatsheets = {
# @todo:
# - adb
# - at
# - dd
# - df
# - du
# - fdisk
# - fstab
# - mount
# - mysqldump
# - mysqlimport
# - mongodump
# - mongoimport
# - nc
# - rsync
# - shred
# - useradd / adduser
# - xargs
# @see: http://www.thegeekstuff.com/2010/11/50-linux-commands/
########## apt-cache ##########################################################
'apt-cache' : '''
To search for apt packages:
apt-cache search "whatever"
''',
########## 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
''',
########## dd ################################################################
'dd' : '''
@todo
''',
########## 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
''',
########## notify-send #######################################################
'notify-send' : '''
To send a desktop notification via dbus:
notify-send -i 'icon-file/name' -a 'application_name' 'summary' 'body of message'
The -i and -a flags can be omitted if unneeded.
''',
########## sed ################################################################
'sed' : '''
To replace all occurrences of "day" with "night" and write to stdout:
sed s/day/night file.txt
To replace all occurrences of "day" with "night" within file.txt:
sed s/day/night file.txt > file.txt
To replace all occurrences of "day" with "night" on stdin:
echo 'It is daytime' | sed s/day/night/
''',
########## split #############################################################
'split' : '''
To split a large text file into smaller files of 1000 lines each:
split file.txt -l 1000
To split a large binary file into smaller files of 10M each:
split file.txt -b 10M
To consolidate split files into a single file:
cat x* > file.txt
''',
########## 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
''',
########## ssh-copy-id ########################################################
'ssh-copy-id' : '''
To copy a key to a remote host:
ssh-copy-id username@host
To copy a key to a remote host on a non-standard port:
ssh-copy-id username@host -p 2222
''',
########## ssh-keygen ########################################################
'ssh-keygen' : '''
To generate an SSH key:
ssh-keygen -t rsa
To generate a 4096-bit SSH key:
ssh-keygen -t rsa -b 4096
To copy a key to a remote host:
ssh-copy-id username@host
To copy a key to a remote host on a non-standard port:
ssh-copy-id username@host -p 2222
''',
########## stdout #############################################################
'stdout' : '''
To redirect stderr to stdout:
some-command 2>&1
To redirect stderr to a file
some-command 2> errors.txt
''',
########## 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/
''',
}
# Cheatsheets can be aliased under a different keyphrase like this:
#cheatsheets['dbus'] = cheatsheets['notify-send']
#cheatsheets['imagick'] = cheatsheets['convert']
#cheatsheets['redirection'] = cheatsheets['stdout']

238
cheat
View File

@ -1,243 +1,17 @@
#!/usr/bin/env python #!/usr/bin/env python
from os.path import expanduser
import imp
import sys import sys
# assemble a keyphrase out of all params passed to the script # assemble a keyphrase out of all params passed to the script
keyphrase = ' '.join(sys.argv[1:]) keyphrase = ' '.join(sys.argv[1:])
# create a dictionary of cheatsheets # load the dictionary of cheatsheets
cheatsheets = { config = imp.load_source('config', expanduser('~') + '/.cheat');
# @todo:
# - adb
# - at
# - dd
# - df
# - du
# - fdisk
# - fstab
# - mount
# - mysqldump
# - mysqlimport
# - mongodump
# - mongoimport
# - nc
# - rsync
# - shred
# - useradd / adduser
# - xargs
# @see: http://www.thegeekstuff.com/2010/11/50-linux-commands/
########## apt-cache ##########################################################
'apt-cache' : '''
To search for apt packages:
apt-cache search "whatever"
''',
########## 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
''',
########## dd ################################################################
'dd' : '''
@todo
''',
########## 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
''',
########## notify-send #######################################################
'notify-send' : '''
To send a desktop notification via dbus:
notify-send -i 'icon-file/name' -a 'application_name' 'summary' 'body of message'
The -i and -a flags can be omitted if unneeded.
''',
########## sed ################################################################
'sed' : '''
To replace all occurrences of "day" with "night" and write to stdout:
sed s/day/night file.txt
To replace all occurrences of "day" with "night" within file.txt:
sed s/day/night file.txt > file.txt
To replace all occurrences of "day" with "night" on stdin:
echo 'It is daytime' | sed s/day/night/
''',
########## split #############################################################
'split' : '''
To split a large text file into smaller files of 1000 lines each:
split file.txt -l 1000
To split a large binary file into smaller files of 10M each:
split file.txt -b 10M
To consolidate split files into a single file:
cat x* > file.txt
''',
########## 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
''',
########## ssh-copy-id ########################################################
'ssh-copy-id' : '''
To copy a key to a remote host:
ssh-copy-id username@host
To copy a key to a remote host on a non-standard port:
ssh-copy-id username@host -p 2222
''',
########## ssh-keygen ########################################################
'ssh-keygen' : '''
To generate an SSH key:
ssh-keygen -t rsa
To generate a 4096-bit SSH key:
ssh-keygen -t rsa -b 4096
To copy a key to a remote host:
ssh-copy-id username@host
To copy a key to a remote host on a non-standard port:
ssh-copy-id username@host -p 2222
''',
########## stdout #############################################################
'stdout' : '''
To redirect stderr to stdout:
some-command 2>&1
To redirect stderr to a file
some-command 2> errors.txt
''',
########## 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/
''',
}
# Cheatsheets can be aliased under a different keyphrase like this:
cheatsheets['dbus'] = cheatsheets['notify-send']
cheatsheets['imagick'] = cheatsheets['convert']
cheatsheets['redirection'] = cheatsheets['stdout']
# print help if requested # print help if requested
if keyphrase in ['', 'help', '--help', '-h']: if keyphrase in ['', 'help', '--help', '-h']:
cheatsheets_sorted = cheatsheets.keys() cheatsheets_sorted = config.cheatsheets.keys()
cheatsheets_sorted.sort() cheatsheets_sorted.sort()
print "Usage: cheat [keyphrase]\n" print "Usage: cheat [keyphrase]\n"
@ -246,4 +20,4 @@ if keyphrase in ['', 'help', '--help', '-h']:
exit() exit()
# print the cheatsheet if it exists # print the cheatsheet if it exists
print cheatsheets[keyphrase] if keyphrase in cheatsheets.keys() else 'No cheatsheet found.' print config.cheatsheets[keyphrase] if keyphrase in config.cheatsheets.keys() else 'No cheatsheet found.'

View File

@ -1,10 +1,11 @@
#!/usr/bin/env python #!/usr/bin/env python
from os.path import expanduser
import shutil import shutil
import sys import sys
try: try:
shutil.copy('./cheat', '/usr/local/bin/') shutil.copy('./cheat', '/usr/local/bin/')
# shutil.move('./.cheat', '~') shutil.copy('./.cheat', expanduser('~'))
except IOError as e: except IOError as e:
print >> sys.stderr, "This installer must be run as root." print >> sys.stderr, "This installer must be run as root."
sys.exit(1) sys.exit(1)