cheat-fork-echo/cheat

250 lines
6.3 KiB
Text
Raw Normal View History

2013-07-31 04:48:07 +02:00
#!/usr/bin/env python
import sys
2013-07-31 05:25:36 +02:00
# assemble a keyphrase out of all params passed to the script
keyphrase = ' '.join(sys.argv[1:])
# create a dictionary of cheatsheets
cheatsheets = {
2013-07-31 04:48:07 +02:00
2013-08-10 05:46:34 +02:00
# @todo:
# - adb
2013-08-10 16:16:13 +02:00
# - at
2013-08-10 05:46:34 +02:00
# - dd
2013-08-10 16:16:13 +02:00
# - df
# - du
2013-08-10 05:46:34 +02:00
# - fdisk
2013-08-10 17:26:56 +02:00
# - fstab
2013-08-10 05:46:34 +02:00
# - mount
2013-08-10 16:16:13 +02:00
# - mysqldump
# - mysqlimport
# - mongodump
# - mongoimport
2013-08-10 05:46:34 +02:00
# - nc
# - rsync
# - shred
# - useradd / adduser
2013-08-10 16:16:13 +02:00
# - xargs
2013-08-10 05:46:34 +02:00
# @see: http://www.thegeekstuff.com/2010/11/50-linux-commands/
2013-08-10 16:16:13 +02:00
########## 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 ###########################################################
2013-08-10 03:56:42 +02:00
'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/`;
2013-08-10 03:56:42 +02:00
do new_path=${file%.*};
new_file=`basename $new_path`;
convert $file -resize 150 conerted/image/path/$new_file.png;
done
''',
2013-08-10 17:26:56 +02:00
########## dd ################################################################
'dd' : '''
@todo
''',
2013-07-31 05:57:04 +02:00
########## 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.
2013-07-31 05:57:04 +02:00
''',
########## find ##############################################################
2013-07-31 04:48:07 +02:00
'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
2013-07-31 04:48:07 +02:00
''',
2013-07-31 05:57:04 +02:00
########## git ###############################################################
2013-07-31 04:48:07 +02:00
'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
''',
2013-07-31 05:57:04 +02:00
########## ln ################################################################
2013-07-31 04:48:07 +02:00
'ln' : '''
To create a symlink:
2013-08-10 03:56:42 +02:00
ln -s path/to/the/target/directory name-of-symlink
2013-07-31 04:48:07 +02:00
''',
2013-08-10 03:56:42 +02:00
########## netstat ###########################################################
'netstat' : '''
To view which users/processes are listening to which ports:
sudo netstat -lnptu
''',
2013-08-10 16:16:13 +02:00
########## 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
''',
2013-08-10 03:56:42 +02:00
########## sockstat ##########################################################
'sockstat' : '''
To view which users/processes are listening to which ports:
sudo sockstat -l
''',
2013-07-31 05:57:04 +02:00
########## ssh ###############################################################
'ssh' : '''
To execute a command on a remote server:
2013-08-10 03:56:42 +02:00
ssh -t user@example.com 'the-remote-command'
To tunnel an x session over SSH:
ssh -X user@example.com
2013-07-31 05:57:04 +02:00
2013-08-10 03:56:42 +02:00
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
''',
2013-07-31 05:57:04 +02:00
2013-08-10 16:16:13 +02:00
########## 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
''',
2013-07-31 05:57:04 +02:00
########## tar ###############################################################
2013-07-31 04:48:07 +02:00
'tar' : '''
To extract an uncompressed archive:
tar -xvf /path/to/foo.tar
2013-07-31 05:57:04 +02:00
To extract a .gz archive:
2013-07-31 04:48:07 +02:00
tar -xzvf /path/to/foo.tgz
2013-07-31 05:57:04 +02:00
To create a .gz archive:
2013-07-31 04:48:07 +02:00
tar -czvf /path/to/foo.tgz /path/to/foo/
2013-07-31 05:57:04 +02:00
To extract a .bz2 archive:
2013-07-31 04:48:07 +02:00
tar -xjvf /path/to/foo.tgz
2013-07-31 05:57:04 +02:00
To create a .bz2 archive:
2013-07-31 04:48:07 +02:00
tar -cjvf /path/to/foo.tgz /path/to/foo/
''',
}
2013-08-10 16:16:13 +02:00
# Cheatsheets can be aliased under a different keyphrase like this:
2013-08-10 17:26:56 +02:00
cheatsheets['dbus'] = cheatsheets['notify-send']
cheatsheets['imagick'] = cheatsheets['convert']
cheatsheets['redirection'] = cheatsheets['stdout']
2013-08-10 05:46:34 +02:00
# print help if requested
2013-07-31 05:57:04 +02:00
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)
2013-07-31 05:25:36 +02:00
exit()
# print the cheatsheet if it exists
print cheatsheets[keyphrase] if keyphrase in cheatsheets.keys() else 'No cheatsheet found.'