From 39b1cf391f4f065488184a54dc485d9d4e9ba665 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Sun, 11 Aug 2013 15:37:11 -0400 Subject: [PATCH] Refactored the application per some feedback on reddit. --- .cheat | 255 --------------------------------------------- .cheat/apt-cache | 2 + .cheat/bash | 5 + .cheat/convert | 19 ++++ .cheat/dhclient | 7 ++ .cheat/find | 14 +++ .cheat/git | 6 ++ .cheat/ln | 2 + .cheat/mysqldump | 8 ++ .cheat/netstat | 2 + .cheat/notify-send | 4 + .cheat/sed | 8 ++ .cheat/shred | 13 +++ .cheat/sockstat | 2 + .cheat/split | 8 ++ .cheat/ssh | 11 ++ .cheat/ssh-copy-id | 5 + .cheat/ssh-keygen | 11 ++ .cheat/stdout | 5 + .cheat/tar | 14 +++ cheat | 27 +++-- install | 4 +- 22 files changed, 167 insertions(+), 265 deletions(-) delete mode 100644 .cheat create mode 100644 .cheat/apt-cache create mode 100644 .cheat/bash create mode 100644 .cheat/convert create mode 100644 .cheat/dhclient create mode 100644 .cheat/find create mode 100644 .cheat/git create mode 100644 .cheat/ln create mode 100644 .cheat/mysqldump create mode 100644 .cheat/netstat create mode 100644 .cheat/notify-send create mode 100644 .cheat/sed create mode 100644 .cheat/shred create mode 100644 .cheat/sockstat create mode 100644 .cheat/split create mode 100644 .cheat/ssh create mode 100644 .cheat/ssh-copy-id create mode 100644 .cheat/ssh-keygen create mode 100644 .cheat/stdout create mode 100644 .cheat/tar diff --git a/.cheat b/.cheat deleted file mode 100644 index 8624382..0000000 --- a/.cheat +++ /dev/null @@ -1,255 +0,0 @@ -#!/usr/bin/env python - -cheatsheets = { - -# @todo: -# - adb -# - at -# - dd -# - df -# - du -# - fdisk -# - fstab -# - mount -# - mongodump -# - mongoimport -# - nc -# - rsync -# - useradd / adduser -# - xargs - -########## 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 -''', - -########## mysqldump ######################################################### -'mysqldump' : ''' -To dump a database to a file: -mysqldump -uusername -ppassword the-database > db.sql - -To dump a database to a .tgz file: -mysqldump -uusername -ppassword the-database | gzip -9 > db.sql - -To dump all databases to a file: -mysqldump -uusername -ppassword --all-databases > all-databases.sql -''', - -########## 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/ -''', - -########## shred ############################################################## -'shred' : ''' -To shred a file (5 passes) and verbose output: -shred -n 5 -v file.txt - -To shred a file (5 passes) and a final overwrite of zeroes: -shred -n 5 -vz file.txt - -To do the above, and then truncate and rm the file: -shred -n 5 -vzu file.txt - -To shred a partition: -shred -n 5 -vz /dev/sda - -Remember that shred may not behave as expected on journaled file systems if file data is being journaled. -''', - -########## 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'] diff --git a/.cheat/apt-cache b/.cheat/apt-cache new file mode 100644 index 0000000..9a0e02b --- /dev/null +++ b/.cheat/apt-cache @@ -0,0 +1,2 @@ +To search for apt packages: +apt-cache search "whatever" diff --git a/.cheat/bash b/.cheat/bash new file mode 100644 index 0000000..be3b8fc --- /dev/null +++ b/.cheat/bash @@ -0,0 +1,5 @@ +To implement a for loop: +for file in `ls .`; + do echo 'file'; + echo 'found'; +done diff --git a/.cheat/convert b/.cheat/convert new file mode 100644 index 0000000..25bc8ed --- /dev/null +++ b/.cheat/convert @@ -0,0 +1,19 @@ +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 diff --git a/.cheat/dhclient b/.cheat/dhclient new file mode 100644 index 0000000..6ac0750 --- /dev/null +++ b/.cheat/dhclient @@ -0,0 +1,7 @@ +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. diff --git a/.cheat/find b/.cheat/find new file mode 100644 index 0000000..0e99e3c --- /dev/null +++ b/.cheat/find @@ -0,0 +1,14 @@ +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 diff --git a/.cheat/git b/.cheat/git new file mode 100644 index 0000000..efba67e --- /dev/null +++ b/.cheat/git @@ -0,0 +1,6 @@ +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 diff --git a/.cheat/ln b/.cheat/ln new file mode 100644 index 0000000..ab95c66 --- /dev/null +++ b/.cheat/ln @@ -0,0 +1,2 @@ +To create a symlink: +ln -s path/to/the/target/directory name-of-symlink diff --git a/.cheat/mysqldump b/.cheat/mysqldump new file mode 100644 index 0000000..0b22002 --- /dev/null +++ b/.cheat/mysqldump @@ -0,0 +1,8 @@ +To dump a database to a file: +mysqldump -uusername -ppassword the-database > db.sql + +To dump a database to a .tgz file: +mysqldump -uusername -ppassword the-database | gzip -9 > db.sql + +To dump all databases to a file: +mysqldump -uusername -ppassword --all-databases > all-databases.sql diff --git a/.cheat/netstat b/.cheat/netstat new file mode 100644 index 0000000..863788b --- /dev/null +++ b/.cheat/netstat @@ -0,0 +1,2 @@ +To view which users/processes are listening to which ports: +sudo netstat -lnptu diff --git a/.cheat/notify-send b/.cheat/notify-send new file mode 100644 index 0000000..2722f67 --- /dev/null +++ b/.cheat/notify-send @@ -0,0 +1,4 @@ +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. diff --git a/.cheat/sed b/.cheat/sed new file mode 100644 index 0000000..2707d7c --- /dev/null +++ b/.cheat/sed @@ -0,0 +1,8 @@ +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/ diff --git a/.cheat/shred b/.cheat/shred new file mode 100644 index 0000000..3aa3300 --- /dev/null +++ b/.cheat/shred @@ -0,0 +1,13 @@ +To shred a file (5 passes) and verbose output: +shred -n 5 -v file.txt + +To shred a file (5 passes) and a final overwrite of zeroes: +shred -n 5 -vz file.txt + +To do the above, and then truncate and rm the file: +shred -n 5 -vzu file.txt + +To shred a partition: +shred -n 5 -vz /dev/sda + +Remember that shred may not behave as expected on journaled file systems if file data is being journaled. diff --git a/.cheat/sockstat b/.cheat/sockstat new file mode 100644 index 0000000..cff34e2 --- /dev/null +++ b/.cheat/sockstat @@ -0,0 +1,2 @@ +To view which users/processes are listening to which ports: +sudo sockstat -l diff --git a/.cheat/split b/.cheat/split new file mode 100644 index 0000000..62a9427 --- /dev/null +++ b/.cheat/split @@ -0,0 +1,8 @@ +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 diff --git a/.cheat/ssh b/.cheat/ssh new file mode 100644 index 0000000..6099196 --- /dev/null +++ b/.cheat/ssh @@ -0,0 +1,11 @@ +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 diff --git a/.cheat/ssh-copy-id b/.cheat/ssh-copy-id new file mode 100644 index 0000000..1a241b7 --- /dev/null +++ b/.cheat/ssh-copy-id @@ -0,0 +1,5 @@ +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 diff --git a/.cheat/ssh-keygen b/.cheat/ssh-keygen new file mode 100644 index 0000000..32986ba --- /dev/null +++ b/.cheat/ssh-keygen @@ -0,0 +1,11 @@ +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 diff --git a/.cheat/stdout b/.cheat/stdout new file mode 100644 index 0000000..e1a8f57 --- /dev/null +++ b/.cheat/stdout @@ -0,0 +1,5 @@ +To redirect stderr to stdout: +some-command 2>&1 + +To redirect stderr to a file +some-command 2> errors.txt diff --git a/.cheat/tar b/.cheat/tar new file mode 100644 index 0000000..54882b4 --- /dev/null +++ b/.cheat/tar @@ -0,0 +1,14 @@ +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/ diff --git a/cheat b/cheat index 133d82f..ad69b44 100755 --- a/cheat +++ b/cheat @@ -1,23 +1,32 @@ #!/usr/bin/env python -from os.path import expanduser -import imp +import os import sys # assemble a keyphrase out of all params passed to the script keyphrase = ' '.join(sys.argv[1:]) +cheat_dir = os.path.expanduser('~') + '/.cheat/' -# load the dictionary of cheatsheets -config = imp.load_source('config', expanduser('~') + '/.cheat'); +# verify that the ~/.cheat directory exists +if not os.path.isdir(cheat_dir): + print >> sys.stderr, 'The ~/.cheat directory does not exist.' + exit() + +# list the files in the ~/.cheat directory +cheatsheets = os.listdir(cheat_dir) +cheatsheets.sort() # print help if requested if keyphrase in ['', 'help', '--help', '-h']: - cheatsheets_sorted = config.cheatsheets.keys() - cheatsheets_sorted.sort() - print "Usage: cheat [keyphrase]\n" print "Available keyphrases:" - print "\n".join(cheatsheets_sorted) + print "\n".join(cheatsheets) exit() # print the cheatsheet if it exists -print config.cheatsheets[keyphrase] if keyphrase in config.cheatsheets.keys() else 'No cheatsheet found.' +if keyphrase in cheatsheets: + with open (cheat_dir + keyphrase, 'r') as cheatsheet: + print cheatsheet.read() + +# if it does not, say so +else: + print 'No cheatsheet found.' diff --git a/install b/install index 31c2b13..7fbdaa0 100755 --- a/install +++ b/install @@ -5,7 +5,9 @@ import sys try: shutil.copy('./cheat', '/usr/local/bin/') - shutil.copy('./.cheat', expanduser('~')) + shutil.copy('./.cheat/', expanduser('~')) except IOError as e: print >> sys.stderr, "This installer must be run as root." sys.exit(1) + +# don't forget to chown back to the normal user