dotfiles/useful_commands

257 lines
7.5 KiB
Plaintext
Raw Permalink Normal View History

2017-06-12 16:22:05 +02:00
# A list of terminal commands/hacks for Mac OS X/BSD
# For a more comprehensive list:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# For another list
# http://www.commandlinefu.com/commands/browse/sort-by-votes/
# To search commandlinefu using the API:
cmdfu(){ curl "http://www.commandlinefu.com/commands/matching/$@/$(echo -n $@ | openssl base64)/plaintext"; }
#
# For a list of Mac OS X keyboard shortcuts:
# http://support.apple.com/kb/HT1343?viewlocale=en_US&locale=en_US
#
# Lots of interesting stuff:
# http://cb.vu/unixtoolbox.xhtml#other
#####################
### Bash
#####################
# List all bash shortcuts
bind -P
## Bash expansions:
!$ # expands to the last argument to the previous command
!* # expands to all the arguments to the previous command
!! # expands to the entirety of the last command
# you can mix all kinds of stuff, which makes history expansion super powerful:
!-2$: # the last argument to the next-to-last command
!ls:*: # all the arguments to the last command that starts with ls
!!:gs/foo/bar: # the last command with all instances of foo replaced with bar
# Re-run last command substituing the string 'before' with 'after' in the command
^before^after^
# Report the size of 'name'
du -sh name
# Quickly rename a file
mv filename.{old,new}
# Print only certain columns of a file:
cut -f column[,column1,...] filename
# Suppress command output (should be equivalent to 'noisycommand >> /dev/null' )
noisy_command >&-
# Run 'command' and convert output to png
command | convert label:@- ip.png
# Remove every file which has not extension foo, bar or baz
rm !(*.foo|*.bar|*.baz)
# Get info about filesystem hierarchy
man hier
# Define a calculator, exmaple of call '? 1+1' yields 2
? () { echo "$*" | bc -l; }
# Copy your public key to user@machine for ssh-key-based login
cat ~/.ssh/id_rsa.pub | ssh user@machine "mkdir -p ~/.ssh; chmod 0700 .ssh; cat >> ~/.ssh/authorized_keys"
# Intercept stdout,err of $PID. This might require root or special configurations
strace -ff -e trace=write -e write=1,2 -p PID
# Pause process, send it to the background and disown it, so that it keeps running after closing the shell.
^Z $bg $disown
# Repeat previous command until it exists successfully
until !!; do :; done
# Print every Nth line
function every() { N=$1; S=1; [ "${N:0:1}" = '-' ] && N="${N:1}" || S=0; sed -n "$S~${N}p"; }
# Print all available terminal colors (and codes)
for code in {0..255}; do printf "\[\e[38;05;${code}m $code: Test\n" && echo -e "Code: \[\e[38;05;${code}m $code: "; done
# Search for a <pattern> string inside all files in the current directory
grep -RnisI <pattern> *
#####################
### Generic commands
#####################
# Dump hex/ascii table of a binary file
hexdump -C filename
# List open TCP/UDP connections and commands that are using them (+c 15 flag shows 15 characters, what OS X uses)
lsof +c 50 -r -i TCP -i UDP
# Shut down computer in 45 minutes
sudo shutdown -h +45
# Show apps that use internet connection at the moment
ss -p
# Get PID of processes which have 'filename' open
fuser filename
# KILL PID of processes which have 'filename' open
fuser -k filename
# Run 'long and slow terminal command' when the load is below 0.8 (schedule for a quiet time)
echo "long and slow terminal command" | batch
# Start command and kill it if it doesn't finish before 5 seconds
timeout 5s command
# Show output of COMMAND in the top right corner of the shell, updating every $SECONDS seconds
# This can be used with date (shows a clock!) uptime, or others DOESN'T WORK ON OSX
while true; do echo -ne "\e[s\e[0;$((COLUMNS-27))H$(COMMAND)\e[u"; sleep $SECONDS; done &
# This does the same thing, but uses tput instead of escape codes
while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done &
#####################
### Mac OS X
#####################
# Configuration:
scutil
configd
# Disk:
diskutil
# Create a RAM disk, the size has to be specified in units of 512-byte sectors,
# to have a 4 GB RAM disk, one has to use ram://8388608, which is the result of (4*(1024^3))/512
diskutil erasevolume HFS+ RAM Disk $(hdiutil attach -nomount ram://2097152)
# CPU Clock/Power info (must be called as superuser)
sudo powermetrics
# Speak:
say "This is just a test."
# Send say output to Airplay (to list available services say -a ?)
say -a "AirPlay" "hello world"
# Copy command output to clipboard:
command_name | pbcopy
# Paste command output:
pbpaste
# Execute applescript
osascript -e osascript
# Play audio files from the command line
afplay fil_ename
# Convert audio files from the command line
afconvert
# Conversion example (to core audio file, caff, from mp3)
afconvert -f caff -d LEI16@22050 Bottle\ Opener.mp3 Bottle\ Opener.caf
# Screensaver as background:
/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background &
# Change speed of Mission Control Animation (default is 0.24)
defaults write com.apple.dock expose-animation-duration -float 0.24
# Prevent a system from sleeping for one hour
caffeinate -u -t 3600
# Prevent sleep until the process has completed:
caffeinate -s $PROCESS
# Convert any document type to any document type
textutil
# Show filesystem usage by processes:
sudo fs_usage
# Find files created after a timestamp:
touch timestamp
find -x / -newer timestamp
#####################
# File Conversion
#####################
# Convert image size to 100x100
sips -Z 100x100 image.jpg
# Convert image to type png
# (WARNING: this modifies the original file and DOES NOT CHANGE EXTENSION, use --out to specify an output)
sips -s format png test.jpg
# Convert image to jpg and resize (better performance than sips)
mogrify -filter lanczos2 -resize 50% -format jpg -quality 92 -path $output_path
# Convert Audio file (to formats known by CoreAudio)
afconvert track.aiff -o track.m4a -q 127 -b 160000 -f 'm4af' -d 'aac '
#(look up afplay)
## sox, the swiss army knife of audio manipulation (custom installed)
man sox
# Convert any type of (text) file to PDF
cupsfilter file_name > output.pdf
# Create a video from a list of images
# -r gives the framerate, the images are numbered
# the formatting is given by the classic c string formatting
# Output is out.mp4
ffmpeg -r 10 -i image-06d.png -c:v libx264 -vf "fps=25,format=yuv420p" out.mp4
##### SED ####
## The -n option below prints line numbers for the original file
## Use -i [ext] to apply substitution to $filename, ext is the
## extension of the backup file used.
# Print lines between 10 and 20 of $filename
sed [-n|-i [ext]] '10,20p' $filename
# Print line 5 of $filename
sed [-n|-i [ext]] 5p $filename
# Delete line 5 of $filename
sed [-n|-i [ext]] 5d $filename
# Delete lines between 10 and 20 of $filename
sed [-n|-i ''[ext]] '10,20d' $filename
# Delete lines matching foo
sed '/foo/d'
# Delete 5 lines after a pattern (including the line with the pattern):
sed -e '/pattern/,+5d' file.txt
# Delete 5 lines after a pattern (excluding the line with the pattern):
sed -e '/pattern/{n;N;N;N;N;d}' file.txt
# Replace first occurrence of foo with bar
sed 's|foo|bar|'
sed 's/foo/bar/'
sed 's-foo-bar-'
# To replace all occurrences add g (global) before the end of the command:
sed 's/foo/bar/g'
#####################
# Random Stuff
#####################
# ASCII Star Wars
telnet towel.blinkenlights.nl
# Zork-like game
emacs -batch -l dunnet
# BINARY CLOCK!
watch -n 1 'echo "obase=2;`date +%s`" | bc'
# dbus introspection
busctl -j --user introspect org.freedesktop.Notifications "/fr/emersion/Mako"