Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ZGainsforth/407f4ed8deecb9e21edb to your computer and use it in GitHub Desktop.

Select an option

Save ZGainsforth/407f4ed8deecb9e21edb to your computer and use it in GitHub Desktop.
List of common terminal commands to do useful things on OS X and/or linux

View network usage by process.

Make sure to make the terminal window the width of the full screen (~200 chars wide is good). Press 'd' to switch to delta mode and then every second it reports what was sent in the last second. You can use right and left arrow to see all columns if you can't make the terminal window wide enough.

nettop

Find a file under the current directory matching a wildcard.

find . -name "*.txt"

Find a file matching a name using spotlight

mdfind -name "gcc"

Track disk usage by process.

Get 12 samplings each 5 seconds long and list them.

sudo iotop -C 5 12

Kill processes by name (in this case the process name is top.

sudo killall top

Compress a dirctory with 7z.

mx3 is fast compression, but not super fast. mmt enables multithreading.

7z a -mx3 -mmt CoupledSubsOctTetB.7z CoupledSubsOctTetB

Make a parity archive for rebuilding large files (like 7z) in case of corruption.

par2 create -v CoupledSubsOctTetB.7z

rsync

rsync without delete. Don't forget the slashes on the end of the directories!

rsync -avz source/ dest/

rsync with delete.

rsync -avz —delete source/ dest/

Soft link.

ln -s /full/path/to/original/file /full/path/to/link

Reduce resolution of the clipboard image (useful for retina displays)

pngpaste - | convert - -interpolate nearest -filter point -sample 50% - | imgcat

sed

replace, -i does it in place.

sed -i 's/DenseScale=1/DenseScale=2/' CNN.py

If you want to change every occurence use:

sed -i 's/DenseScale=1/DenseScale=2/g' CNN.py

Remember the quoted text are regex so gotchas include / \ ' " . & * etc.

Extract portion of screencast

-ss is the start time. -to is the stop time. -c:v copy -c:a copy copies both video and audio streams.

ffmpeg -i "Input.mp4" -ss 00:21:29.00 -to 00:30:31.00 -c:v copy -c:a copy "Output.mp4"

Ultimate plumber.

find . -name '*.py' | up

Rename using regex

Assume a directory with files t.001.jpg t.002.jpg etc. You want just 001.jpg 002.jpg etc.

rename 's/t.//' *

Hide and show all the icons on OS X desktop

defaults write com.apple.finder CreateDesktop false killall Finder defaults write com.apple.finder CreateDesktop true killall finder

Recursive, and file name specific grep

-r is for recursion -n is to list the file line numbers. --include says to only include the files which match the pattern.

grep -rn --include '*.py' import .

Make a RAM disk.

Allocation is in 512 byte blocks, so there are 2048 blocks in one megabyte. hdiutil is used to allocate GIGS gigabytes of RAM and attach it as an unmounted drive. That's passed to diskutil which formats it as an HFS+

GIGS=4; diskutil erasevolume HFS+ "RAMDisk" $(hdiutil attach -nomount ram://$((2048*1024*GIGS)))

Write a Linux iso to USB

First we need to convert the iso to a dmg which can be block-for-block written to the USB drive. Then we have to disk dump it to the USB drive. In the code below, replace the name values with appropriate disk or file names. In the final command, you must enter the size of the dmg into the pv command in the middle of the pipe. That is how it knows how far through the copy you are.

hdiutil convert -format UDRW -o [name].dmg [name].iso
diskutil list
diskutil eraseDisk FAT32 [NAME] GPT /dev/disk[n]
diskutil unmountDisk /dev/disk[n]
sudo dd if=/Users/Zack/Desktop/[name].dmg of=/dev/disk[n] bs=1m
dd if=[name].dmg ibs=1m | pv -s [size] | sudo dd of=/dev/disk[n] obs=1m

Show all information about displays in OS X

system_profiler SPDisplaysDataType

LAMMPS multithreaded command

lmp_serial.exe -sf omp -pk omp 6 -in file.in

sf omp tells it to use OpenMP for algorithms that support it. pk omp tells it to tell the OpenMP package that it should use 6 threads.

Profile python script

python -m cProfile -s cumtime myscript.py > profile.txt
cat profile.txt

cProfile does the profiling, -s cumtime sorts the output by cumtime, and send the result to a text file.

Run an interactive terminal on the cluster.

srun --partition=etna --account=nano --ntasks=24 --time=01:00:00 --pty bash

Tools for monitoring the system on linux

bashtop = terminal resource monitor s-tui = stress test ui shows cores and temps hwinfo = data dump about all the hardware psensor = ui for graphing temps over time cpu-x = ui like cpu-z on linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment