mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Change the zsh heading to unix.
This commit is contained in:
@@ -1,6 +0,0 @@
|
||||
# All The Environment Variables
|
||||
|
||||
If you want to see all the environment variables defined on your machine,
|
||||
you can list them all out with `printenv`. If you are like me, you probably
|
||||
have a ton of them. Pipe it through `less` to make it easier to navigate and
|
||||
search through (i.e. `printenv | less`).
|
||||
@@ -1,27 +0,0 @@
|
||||
# Cat A File With Line Numbers
|
||||
|
||||
You can quickly view a file using `cat`
|
||||
|
||||
```
|
||||
$ cat Gemfile
|
||||
source 'https://rubygems.org'
|
||||
|
||||
|
||||
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
|
||||
gem 'rails', '4.2.0'
|
||||
# Use postgresql as the database for Active Record
|
||||
gem 'pg'
|
||||
```
|
||||
|
||||
With the `-n` flag you can view that file with line numbers
|
||||
|
||||
```
|
||||
$ cat -n Gemfile
|
||||
1 source 'https://rubygems.org'
|
||||
2
|
||||
3
|
||||
4 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
|
||||
5 gem 'rails', '4.2.0'
|
||||
6 # Use postgresql as the database for Active Record
|
||||
7 gem 'pg'
|
||||
```
|
||||
@@ -1,20 +0,0 @@
|
||||
# Check If A Port Is In Use
|
||||
|
||||
The `lsof` command is used to *list open files*. This includes listing
|
||||
network connections. This means I can check if a particular port is in use
|
||||
and what process is using that port. For instance, I can check if my rails
|
||||
application is currently running on port 3000.
|
||||
|
||||
```
|
||||
$ lsof -i TCP:3000
|
||||
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
|
||||
ruby 13821 jbranchaud 12u IPv6 0xdf2e9fd346cc12b5 0t0 TCP localhost:hbci (LISTEN)
|
||||
ruby 13821 jbranchaud 13u IPv4 0xdf2e9fd33ca74d65 0t0 TCP localhost:hbci (LISTEN)
|
||||
```
|
||||
|
||||
I can see that a ruby process (my rails app) is using port 3000. The PID
|
||||
and a number of other details are included.
|
||||
|
||||
See more details with `man lsof`.
|
||||
|
||||
h/t [Mike Chau](https://twitter.com/money_mikec)
|
||||
@@ -1,7 +0,0 @@
|
||||
# Clear The Screen
|
||||
|
||||
If you type `clear` into your shell, the screen will be cleared. There is a
|
||||
handy keybinding though that will save you a few keystrokes. Just hit
|
||||
`ctrl-l` to achieve the same effect.
|
||||
|
||||
source: [Derek P.](https://twitter.com/DerkTheDaring)
|
||||
@@ -1,23 +0,0 @@
|
||||
# Create A File Descriptor with Process Substitution
|
||||
|
||||
Process substitution can be used to create a file descriptor from the
|
||||
evaluation of a shell command. The syntax for process substitution is
|
||||
`<(LIST)` where `LIST` is one or more bash commands.
|
||||
|
||||
```
|
||||
$ cat <(echo 'hello, world')
|
||||
hello, world
|
||||
```
|
||||
|
||||
This is particularly useful for commands that expect files, such as diff:
|
||||
|
||||
```
|
||||
$ diff <(echo 'hello, world') <(echo 'hello, mars')
|
||||
1c1
|
||||
< hello, world
|
||||
---
|
||||
> hello, mars
|
||||
```
|
||||
|
||||
Sources: [Brian Dunn](https://twitter.com/higgaion) and
|
||||
[Bash Guide for Beginners](http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html#sect_03_04_07)
|
||||
@@ -1,6 +0,0 @@
|
||||
# Do Not Overwrite Existing Files
|
||||
|
||||
When using the `cp` command to copy files, you can use the `-n` flag to make
|
||||
sure that you do not overwrite existing files.
|
||||
|
||||
h/t [Dillon Hafer](https://twitter.com/dillonhafer)
|
||||
@@ -1,16 +0,0 @@
|
||||
# File Type Info With File
|
||||
|
||||
Use the `file` utility to determine the type of a file:
|
||||
|
||||
```bash
|
||||
$ file todo.md
|
||||
todo.md: ASCII English text
|
||||
|
||||
$ file Hello.java
|
||||
Hello.java: ASCII C++ program text
|
||||
|
||||
$ file Hello.class
|
||||
Hello.class: compiled Java class data, version 52.0
|
||||
```
|
||||
|
||||
The `Hello.java` file isn't exactly a C++ program, but close enough.
|
||||
@@ -1,13 +0,0 @@
|
||||
# Find Newer Files
|
||||
|
||||
Use the `-newer` flag with the name of a file to find files that have a
|
||||
newer modification date than the named file.
|
||||
|
||||
For instance,
|
||||
|
||||
```
|
||||
$ find blog -name '*.md' -newer blog/first-post.md
|
||||
```
|
||||
|
||||
will find all markdown files in the `blog` directory that have a
|
||||
modification date more recent than `blog/first-post.md`.
|
||||
@@ -1,28 +0,0 @@
|
||||
# Global Substitution On The Previous Command
|
||||
|
||||
Let's say we just executed the following command:
|
||||
|
||||
```bash
|
||||
$ grep 'foo' foo.md
|
||||
```
|
||||
|
||||
It gave us the information we were looking for and now we want to execute
|
||||
a similar command to find the occurrences of `bar` in `bar.md`. The `^`
|
||||
trick won't quite work here.
|
||||
|
||||
```bash
|
||||
$ ^foo^bar<tab>
|
||||
$ grep 'bar' foo.md
|
||||
```
|
||||
|
||||
What we need is a global replace of `foo` in our previous command. The `!!`
|
||||
command can help when we sprinkle in some `sed`-like syntax.
|
||||
|
||||
```bash
|
||||
$ !!gs/foo/bar<tab>
|
||||
$ grep 'bar' bar.md
|
||||
```
|
||||
|
||||
For a short command like this, we haven't gained much. However, for large
|
||||
commands that span the length of the terminal, this can definitely save us
|
||||
a little trouble.
|
||||
@@ -1,40 +0,0 @@
|
||||
# Hexdump A Compiled File
|
||||
|
||||
The `hexdump` unix utility allows you to dump the contents of a
|
||||
compiled/executable file in a _readable_ hexadecimal format. Adding the `-C`
|
||||
flag includes a sidebar with a formatted version of that row of hexadecimal.
|
||||
|
||||
For example, a compiled _Hello World_ java program, `Hello.java`, will look
|
||||
something like this:
|
||||
|
||||
```
|
||||
> cat Hello.class | hexdump -C
|
||||
00000000 ca fe ba be 00 00 00 34 00 1d 0a 00 06 00 0f 09 |.......4........|
|
||||
00000010 00 10 00 11 08 00 12 0a 00 13 00 14 07 00 15 07 |................|
|
||||
00000020 00 16 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29 |.....<init>...()|
|
||||
00000030 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e |V...Code...LineN|
|
||||
00000040 75 6d 62 65 72 54 61 62 6c 65 01 00 04 6d 61 69 |umberTable...mai|
|
||||
00000050 6e 01 00 16 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67 |n...([Ljava/lang|
|
||||
00000060 2f 53 74 72 69 6e 67 3b 29 56 01 00 0a 53 6f 75 |/String;)V...Sou|
|
||||
00000070 72 63 65 46 69 6c 65 01 00 0a 48 65 6c 6c 6f 2e |rceFile...Hello.|
|
||||
00000080 6a 61 76 61 0c 00 07 00 08 07 00 17 0c 00 18 00 |java............|
|
||||
00000090 19 01 00 0d 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 |....Hello, World|
|
||||
000000a0 21 07 00 1a 0c 00 1b 00 1c 01 00 05 48 65 6c 6c |!...........Hell|
|
||||
000000b0 6f 01 00 10 6a 61 76 61 2f 6c 61 6e 67 2f 4f 62 |o...java/lang/Ob|
|
||||
000000c0 6a 65 63 74 01 00 10 6a 61 76 61 2f 6c 61 6e 67 |ject...java/lang|
|
||||
000000d0 2f 53 79 73 74 65 6d 01 00 03 6f 75 74 01 00 15 |/System...out...|
|
||||
000000e0 4c 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 |Ljava/io/PrintSt|
|
||||
000000f0 72 65 61 6d 3b 01 00 13 6a 61 76 61 2f 69 6f 2f |ream;...java/io/|
|
||||
00000100 50 72 69 6e 74 53 74 72 65 61 6d 01 00 07 70 72 |PrintStream...pr|
|
||||
00000110 69 6e 74 6c 6e 01 00 15 28 4c 6a 61 76 61 2f 6c |intln...(Ljava/l|
|
||||
00000120 61 6e 67 2f 53 74 72 69 6e 67 3b 29 56 00 20 00 |ang/String;)V. .|
|
||||
00000130 05 00 06 00 00 00 00 00 02 00 00 00 07 00 08 00 |................|
|
||||
00000140 01 00 09 00 00 00 1d 00 01 00 01 00 00 00 05 2a |...............*|
|
||||
00000150 b7 00 01 b1 00 00 00 01 00 0a 00 00 00 06 00 01 |................|
|
||||
00000160 00 00 00 01 00 09 00 0b 00 0c 00 01 00 09 00 00 |................|
|
||||
00000170 00 25 00 02 00 01 00 00 00 09 b2 00 02 12 03 b6 |.%..............|
|
||||
00000180 00 04 b1 00 00 00 01 00 0a 00 00 00 0a 00 02 00 |................|
|
||||
00000190 00 00 03 00 08 00 04 00 01 00 0d 00 00 00 02 00 |................|
|
||||
000001a0 0e |.|
|
||||
000001a1
|
||||
```
|
||||
@@ -1,14 +0,0 @@
|
||||
# Killing A Frozen SSH Session
|
||||
|
||||
Whenever an SSH session freezes, I usually mash the keyboard in desperation
|
||||
and then kill the terminal session. This can be avoided though. SSH will
|
||||
listen for the following kill command:
|
||||
|
||||
```
|
||||
~.<cr>
|
||||
```
|
||||
|
||||
This will kill the frozen SSH session and leave you in the terminal where
|
||||
you were before you SSH'd.
|
||||
|
||||
source: [Jack C.](http://hashrocket.com/team/jack-christensen)
|
||||
@@ -1,22 +0,0 @@
|
||||
# List All The Say Voices
|
||||
|
||||
The `say` command can be a fun party trick.
|
||||
|
||||
```bash
|
||||
$ say Get ready for the bass to drop
|
||||
```
|
||||
|
||||
Your friends will be even more impressed when you use some of the alternate
|
||||
voices.
|
||||
|
||||
```bash
|
||||
$ say -v Daniel Would you like a cup of tea?
|
||||
```
|
||||
|
||||
To see all the alternate voices available, type the following
|
||||
|
||||
```bash
|
||||
$ say -v '?'
|
||||
```
|
||||
|
||||
[source](http://stackoverflow.com/questions/1489800/getting-list-of-mac-text-to-speech-voices-programmatically)
|
||||
@@ -1,10 +0,0 @@
|
||||
# Only Show The Matches
|
||||
|
||||
Tools like `grep`, `ack`, and `ag` make it easy to search for lines in a
|
||||
file that contain certain text and patterns. They all come with the `-o`
|
||||
flag which tells them to only show the part that matches.
|
||||
|
||||
This is particularly powerful when used with regex and piped into other
|
||||
programs.
|
||||
|
||||
h/t Dillon Hafer
|
||||
@@ -1,9 +0,0 @@
|
||||
# Repeat Yourself
|
||||
|
||||
Use the `repeat` command to repeat some other command.
|
||||
|
||||
You can repeat a command any number of times like so
|
||||
|
||||
```
|
||||
$ repeat 5 say Hello World
|
||||
```
|
||||
@@ -1,21 +0,0 @@
|
||||
# Saying Yes
|
||||
|
||||
Tired of being prompted for confirmation by command-line utilities? Wish you
|
||||
could blindly respond 'yes' to whatever it is they are bugging you about?
|
||||
The `yes` command is what you've been looking for.
|
||||
|
||||
```
|
||||
$ yes | rm -r ~/some/dir
|
||||
```
|
||||
|
||||
This will respond `y` as `rm` asks for confirmation on removing each and
|
||||
every file in that directory.
|
||||
|
||||
`yes` is just as good at saying *no*. Give it `no` as an argument and it
|
||||
will happily (and endlessly) print `no`.
|
||||
|
||||
```
|
||||
$ yes no
|
||||
```
|
||||
|
||||
h/t [Chris Erin](https://twitter.com/MCNormalMode)
|
||||
@@ -1,12 +0,0 @@
|
||||
# Search History
|
||||
|
||||
Often times there is a very specific command you have entered into your bash
|
||||
prompt that you need to run again. You don't want to have to type it again
|
||||
and stepping manually through your history may be suboptimal if you typed it
|
||||
quite a while ago. Fortunately, there is a simple history search feature
|
||||
that you can use in this kind of situation.
|
||||
|
||||
Hit `Ctrl+r` and then start typing a moderately specific search term. Your
|
||||
search history will be filtered by that term. Subsequent hitting of
|
||||
`Ctrl+r` will step forward through that filtered history. Once you find the
|
||||
command you are looking for, hit enter to execute it.
|
||||
@@ -1,11 +0,0 @@
|
||||
# Securely Remove Files
|
||||
|
||||
If you really want to make sure you have wiped a file from your hard drive,
|
||||
you are going to want to use `srm` instead of `rm`. The man page for `srm`
|
||||
gives the following description:
|
||||
|
||||
> srm removes each specified file by overwriting, renaming, and truncating
|
||||
> it before unlinking. This prevents other people from undeleting or
|
||||
> recovering any information about the file from the command line.
|
||||
|
||||
h/t Dillon Hafer
|
||||
@@ -1,7 +0,0 @@
|
||||
# SSH With Port Forwarding
|
||||
|
||||
Use the `-L` flag with `ssh` to forward a connection to a remote server
|
||||
|
||||
```
|
||||
$ ssh someserver -L3000:localhost:3000
|
||||
```
|
||||
@@ -1,17 +0,0 @@
|
||||
# Switch Versions of a Brew Formula
|
||||
|
||||
If you've installed a couple versions of a program via brew and you'd like
|
||||
to switch from the currently linked version to the other installed version,
|
||||
you can use the `switch` command. For instance, if you are on version
|
||||
`1.8.2` of `phantomjs` and you'd like to switch to `1.9.0`, you can simply
|
||||
invoke:
|
||||
|
||||
```
|
||||
$ brew switch phantomjs 1.9.0
|
||||
```
|
||||
|
||||
More generically:
|
||||
|
||||
```
|
||||
$ brew switch <formula> <version>
|
||||
```
|
||||
@@ -1,15 +0,0 @@
|
||||
# Watch The Difference
|
||||
|
||||
The `watch` command is a simple way to repeatedly run a particular command.
|
||||
I'll sometimes use it to monitor the response from some endpoint. `watch`
|
||||
can make monitoring responses even easier when the `-d` flag is employed.
|
||||
This flag instructs `watch` to highlight the parts of the output that are
|
||||
*different* from the previous run of the command.
|
||||
|
||||
So if I run
|
||||
|
||||
```
|
||||
$ watch -d curl -LIs localhost:3000
|
||||
```
|
||||
|
||||
I can easily see if the http status of the request changes.
|
||||
@@ -1,16 +0,0 @@
|
||||
# Watch This Run Repeatedly
|
||||
|
||||
I usually reach for a quick bash for loop when I want to run a particular
|
||||
process a bunch of times in a row. The `watch` command is another way to
|
||||
run a process repeatedly.
|
||||
|
||||
```
|
||||
watch rspec spec/some/test.rb
|
||||
```
|
||||
|
||||
The default is 2 seconds in between subsequent executions of the command.
|
||||
The period can be changed with the `-n` flag though:
|
||||
|
||||
```
|
||||
watch -n 2 rspec spec/some/test.rb
|
||||
```
|
||||
@@ -1,21 +0,0 @@
|
||||
# Where Are The Binaries?
|
||||
|
||||
When I want to know where an executable is, I use `which` like so:
|
||||
|
||||
```
|
||||
$ which rails
|
||||
/Users/jbranchaud/.gem/ruby/2.1.4/bin/rails
|
||||
```
|
||||
|
||||
That is the rails binary on my path that will be used if I enter a rails command.
|
||||
|
||||
However, with something like rails, there may be multiple versions on your
|
||||
path. If you want to know where all of them are, you can use `where`, like
|
||||
so:
|
||||
|
||||
```
|
||||
$ where rails
|
||||
/Users/jbranchaud/.gem/ruby/2.1.4/bin/rails
|
||||
/Users/jbranchaud/.rubies/2.1.4/bin/rails
|
||||
/usr/bin/rails
|
||||
```
|
||||
Reference in New Issue
Block a user