Skip to content

Instantly share code, notes, and snippets.

@vtjeng
Last active May 26, 2020 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vtjeng/95f4611afec6b5f4e03be1c36b48f00a to your computer and use it in GitHub Desktop.
Save vtjeng/95f4611afec6b5f4e03be1c36b48f00a to your computer and use it in GitHub Desktop.
Command Line Magic

Utilities

Shell

Source Control

  • hub or ghi for working with GitHub over the command line.
  • tig for a text-mode interface to git.

Miscellaneous

Useful Commands

Files

Copying and Moving

Copying

cp SOURCE_FILE DEST_FILE
# specifies new name for destination file
cp SOURCE_FILE DEST_DIRECTORY
# creates copy with same name in destination directory
cp -r SOURCE_DIRECTORY DEST_DIRECTORY
# copies directory and all contents recursively
# https://askubuntu.com/a/844879

Moving

mv SOURCE_DIRECTORY/* DEST_DIRECTORY
# moves directory and all contents recursively
# https://askubuntu.com/a/998602
# note: this ignores dotfiles, see link for workaround

SCP

scp [OPTION] [user@]SRC_HOST:]SOURCE_FILE [user@]DEST_HOST:]DEST_FILE
scp -r scp [OPTION] [user@]SRC_HOST:]SOURCE_DIRECTORY [user@]DEST_HOST:]DEST_DIRECTORY
# https://linuxize.com/post/how-to-use-scp-command-to-securely-transfer-files/

Searching and Counting

Count number of files in directory

find . -name "*.filetype" | wc -l
# https://stackoverflow.com/questions/12656427/count-number-of-specific-file-type-of-a-directory-and-its-sub-dir-in-mac

Search for files containing matches, group counts by top-level directory

rg -t py "foo" -l | cut -f1,2 -d'/' | sort | uniq -c | sort -n

# search for all files in current directory with py extension containing foo, returning just filenames
# remove everything after the second occurency of the delimiter '/': https://stackoverflow.com/questions/23690698/remove-everything-after-2nd-occurrence-in-a-string-in-unix
# sort and count unique entries, then sort numerically

Modifying Text Programmatically

sed

sed -i 'Ns/.*/replacement-line/' file.txt
# Replace by line number
# https://stackoverflow.com/questions/11145270/how-to-replace-an-entire-line-in-a-text-file-by-line-number/11145362
sed -n '1,1p;1002,2001p' full.csv > subset.csv
# Extract by line number
# https://stackoverflow.com/questions/83329/how-can-i-extract-a-predetermined-range-of-lines-from-a-text-file-on-unix
sed -i -e '5,10d;12d' file.txt
# Delete by line number
# https://stackoverflow.com/questions/2112469/delete-specific-line-numbers-from-a-text-file-using-sed

Git

Get file from another branch

git checkout <commit-ish> -- file_name
# https://stackoverflow.com/questions/2364147/how-to-get-just-one-file-from-another-branch/2364223
# why the double dashes? https://stackoverflow.com/questions/13321458/meaning-of-git-checkout-double-dashes

Check out branch from remote

git checkout -b <your branch name> <name of remote>/<branch name on remote>
# https://stackoverflow.com/questions/1783405/how-do-i-check-out-a-remote-git-branch

See statistics on lines changed per file

git diff --stat <commit-ish> <commit-ish>
# https://stackoverflow.com/questions/2528111/how-can-i-calculate-the-number-of-lines-changed-between-two-commits-in-git/2528129
# --numstat for machine-readable

Diff feature branch against trunk

git diff master...topic
# https://matthew-brett.github.io/pydagogue/git_diff_dots.html
# Shows the differences between master and topic starting at the last common commit.
# Also see: https://stackoverflow.com/questions/1549146/git-find-the-most-recent-common-ancestor-of-two-branches

Follow history of a function across file renames and moves

git log -L ':<funcname>:<file>' -- follow -- <file>
# --follow option: https://kgrz.io/use-git-log-follow-for-file-history.html
# -L option: traces the evolution of <funcname> https://git-scm.com/docs/git-log

Look for differences that change the number of occurrences of the specified string (i.e. addition/deletion)

git log -S <string> -- <path>

Disk Space

Disk space used and available

df -h

Space a specific directory is taking up

du /tmp -h --max-depth=1 | sort -h
# shows size of 1 `tmp` directory and sub-directories up to depth 1

Miscellaneous

Copying to X Windows primary clipboard

cat my_file.txt | xclip -sel c
# https://stackoverflow.com/questions/749544/pipe-to-from-the-clipboard-in-bash-script

Copying to clipboard over ssh on OSX

# from outside of SSH session
ssh remotehost "cat your_file.txt" | pbcopy
# https://unix.stackexchange.com/questions/16694/copy-input-to-clipboard-over-ssh

Working with time

date -d @946684800
# Fri Dec 31, 16:00:00 PST 1999
date --date="14 day ago" +%Y-%m-%d
2020-04-04

See most common commands in your history

history | cut -c 8-1000 | cut -f1,2 -d' ' | sort | uniq -c | sort -n
# list commands in history
# remove first 7 characters; this is a bit of a hack and may vary depending on the size of your history. source: https://unix.stackexchange.com/a/192446
# remove everything after the second occurence of the delimiter ' '
# sort and count unique entries, then sort numerically

Miscellaneous Information

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