Skip to content

Instantly share code, notes, and snippets.

@negamorgan
Last active August 29, 2015 14:07
Show Gist options
  • Save negamorgan/bc5c258fc9e11d9427be to your computer and use it in GitHub Desktop.
Save negamorgan/bc5c258fc9e11d9427be to your computer and use it in GitHub Desktop.
Advanced Command Line Techniques

Advanced Command Line Techniques

pushd to save an array of working directories

❤  sites  mkdir sandbox
❤  sites  cd sandbox
❤  sandbox  pushd ~/Downloads/php-master
~/Downloads/php-master ~/sites/sandbox ~/sites ~

dirs to return the array

❤  php-master  dirs
~/Downloads/php-master ~/sites/sandbox ~/sites ~

popd to backtrack/pop-off from array

❤  php-master  popd
~/sites/sandbox ~/sites ~
❤  sandbox  dirs
~/sites/sandbox ~/sites ~

file gives info about files

❤  sandbox  touch notes.md
❤  sandbox  file notes.md
notes.md: UTF-8 Unicode English text
❤  sandbox  file `which vim`
/usr/local/bin/vim: Mach-O 64-bit executable x86_64

basename gives the current directory, one level only

❤  sandbox  basename `pwd`
sandbox

cmd + k to hard clear the Terminal history

gzip

❤  sandbox  echo "here is some stuff" > stuff.txt
❤  sandbox  gzip stuff.txt
❤  sandbox  ls -l
total 16
-rw-r--r--@ 1 kiddo  501  834 Sep 28 17:18 notes.md
-rw-r--r--  1 kiddo  501   49 Sep 28 17:20 stuff.txt.gz
❤  sandbox  gzip -d stuff.txt.gz
❤  sandbox  ls -l
total 16
-rw-r--r--@ 1 kiddo  501  834 Sep 28 17:18 notes.md
-rw-r--r--  1 kiddo  501   19 Sep 28 17:20 stuff.txt

tar to compress/uncompress archives

❤  sandbox  tar -cvzf archive.tar.gz one.txt two.txt three.txt
a one.txt
a two.txt
a three.txt
❤  sandbox  ls
archive.tar.gz one.txt        three.txt
notes.md       stuff.txt      two.txt
❤  sandbox  tar -xvzf archive.tar.gz
x one.txt
x two.txt
x three.txt

> and >> to funnel output to a file instead of STDOUT

❤  sandbox  echo "here is some content" > file.txt
❤  sandbox  cat file.txt
here is some content
❤  sandbox  echo "here is more content" >> file.txt
❤  sandbox  cat file.txt
here is some content
here is more content

&> to write STDOUT & STDERR to file

❤  sandbox  mkdir -p exists/stuff
❤  sandbox  ls -l exists doesnt-exist &> log.txt
❤  sandbox  cat log.txt
ls: doesnt-exist: No such file or directory
exists:
total 0
drwxr-xr-x  2 kiddo  501  68 Sep 28 17:45 stuff

cat can also be used to quickly create a file from STDIN

❤  sandbox  cat > file.md
# stuff that happened

## first thing
notes notes

## second thing
- notes
- notes
^C
❤  sandbox  cat file.md
# stuff that happened

## first thing
notes notes

## second thing
- notes
- notes

sort you guessed it

❤  sandbox  cat > file.txt
currants
bananas
dates
apples
^C
❤  sandbox  sort file.txt
apples
bananas
currants
dates
❤  sandbox  cat file.txt | sort > file_sorted.txt
❤  sandbox  cat file_sorted.txt
apples
bananas
currants
dates

uniq (must be sorted first)

❤  sandbox  cat file.txt
currants
bananas
dates
apples
currants
bananas
❤  sandbox  uniq file.txt
currants
bananas
dates
apples
currants
bananas
❤  sandbox  sort file.txt | uniq
apples
bananas
currants
dates

miscellaneous

❤  sandbox  expr 1 + 1
2
❤  sandbox  expr 45 \* 3
135
❤  sandbox  uptime
17:29  up 8 days, 15:44, 6 users, load averages: 2.77 2.02 1.89
❤  sandbox  date
Mon Sep 29 17:29:46 EDT 2014
❤  sandbox  cal
   September 2014
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

❤  sandbox  wc email.txt
      20     144     953 email.txt
❤  sandbox  wc *.txt
       2       6      46 bash.txt
      20     144     953 email.txt
       6       6      47 file.txt
       4       4      30 file_sorted.txt
       4      19     108 log.txt
       1       1       8 new.txt
       0       0       0 one.txt
       5       6      43 stuff.txt
       0       0       0 three.txt
       0       0       0 two.txt
      42     186    1235 total
❤  sandbox  open file.md

history lists history, can limit history 10 or pipe output

❤  sandbox  history | grep mocha | tail
 9489  mocha node_modules/profile-image/test.js
 9492  mocha node_modules/profile-image/test.js
 9494  mocha node_modules/profile-image/test.js
 9505  mocha node_modules/profile-image/test.js
 9507  mocha node_modules/profile-image/test.js
 9510  mocha node_modules/profile-image/test.js
 9531  mocha node_modules/profile-image/test.js
 9532  mocha test.js

sed highlights

❤  sandbox  sed 1p file.txt
currants
currants
bananas
dates
apples
currants
bananas
❤  sandbox  sed -n 1p file.txt
currants
❤  sandbox  cat file.txt | sed 's/dates/oranges/'
currants
bananas
oranges
apples
currants
bananas
❤  sandbox  cat file.txt
currants
bananas
dates
apples
currants
bananas
❤  sandbox  cat file.txt | sed '1a\
pipe quote> second line
pipe quote> '
currants
second line
bananas
dates
apples
currants
bananas

ack

❤  sandbox  ack exist # search
❤  sandbox  ack -w exist # word boundary
❤  sandbox  ack -ch exist # count the total number of matches
7
❤  sandbox  ack -cl exist
log.txt:2
notes.md:5

❤  sandbox  ack -C 1 happened # one line of context padding before and after
file.md
1:# stuff that happened
2-

notes.md
87-	❤  sandbox  cat > file.md
88:	# stuff that happened
89-
--
97-	❤  sandbox  cat file.md
98:	# stuff that happened
99-

❤  sandbox  ack --help-types

❤  sites  ack --ruby each_with_object
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment