Skip to content

Instantly share code, notes, and snippets.

@etdev
Last active August 29, 2015 14:13
Show Gist options
  • Save etdev/128f61cb94a8f0c23f36 to your computer and use it in GitHub Desktop.
Save etdev/128f61cb94a8f0c23f36 to your computer and use it in GitHub Desktop.
Useful commands (便利なシェルなどのコマンド)

###Shell (tested in zsh)

####Replace string_a with string_b in any files containing string_a (recursive):

I'm going to assume this is inside a git repository but even if it weren't, you could just use grep, ag etc. instead.

git grep -l "<string_a>" | xargs sed -i 's@<string_a>@<string_b>@'

For example, if you just started using the assets pipeline on Rails, you might have a need to do this (to replace all instances of src="/javascripts... with src="/assets...):

git grep -l "src=\"\/javascripts" | xargs sed -i 's@src="/javascripts@src="/assets@'

####(Using The silver searcher) Replace string_a with string_b in any files containing string_a (recursive) and ending with .erb:

ag -l -G \.erb$ "string_a" | xargs sed -i 's@string_a@string_b@'

####Concatenate the outputs of two commands:

 ( command 1; command 2; ) | cat 

For example, if you want to make a list of files that contain references to javascript files saved in "/javascripts" and CSS files stored in "/stylesheets", you could do this:

 (git grep -l "src=\"\/javascripts" ; git grep -l "href=\"\/stylesheets" ;) | cat

####List processes that are using a specific port

 lsof -i :<port>

For example, if you want a list of all the processes running on port 3000:

 lsof -i :3000

####Create symlink to a file

This is a basic one but I always screw it up, so:

 ln -s <existing-location> <new-symlinked-location>

For example, to symlink ~/dotfiles/local/.gitconfig.local to ~/.gitconfig.local,

 ln -s $HOME/dotfiles/local/.gitconfig.local $HOME/.gitconfig.local

Be sure to only use absolute paths

####SCP a file from a remote host to the local server
scp <user>@<remote_ip>:/path/to/file /local/path

####Invoke su with correct env variables su - or su -l If you just do su

####Set a file stored in /etc/init.d/my_script to run on system boot (Tested on CentOS 6.5)
sudo /sbin/chkconfig --add my_script
sudo /sbin/chkconfig --list my_script
sudo /sbin/chkconfig my_script on
Note that the script must have comments of the following form to be recognized by chkdisk:

# chkconfig: <levels> <start> <stop>
# description: <some description>

For example:

# chkconfig: 345 99 01
# description: some startup script

345 - levels to configure 99 - startup order 01 - stop order

Then to stop it from running on startup:
sudo chkconfig my_script off

###Tmux

####Re-load tmux config file

 tmux source-file ~/.tmux.conf

####Shutdown tmux server

 tmux kill-server

Vim

####Generate ctags for current directory from within vim (requires rails.vim)

 :Rtags

Postgresql (Mac OS X)

Start/stop (pg_ctl)

Start:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

Stop:
pg_ctl -D /usr/local/var/postgres stop -s -m fast

Log in as postgres user

psql postgres

Add a role

create role <username> with createdb login password '<your password>'

Start/stop (homebrew, deprecated)

brew services <start/stop> postgresql

Start/stop (lunchy)

lunchy <start/stop> postgres

Get out of the CLI

\q or Ctrl+D

Postgresql (Mac OS X)

Start:
sudo service postgresql<version> start

###Dotfiles Install (requires rcup and git repo in the ~/dotfiles folder): env RCRC=$HOME/dotfiles/rcrc rcup

###SSH Install server on CentOS (6.5): sudo yum -y install openssh-server openssh-clients
Restart server on CentOS (6.5): /etc/init.d/sshd restart

###Git Reset specific files to how they were at a given commit:
git checkout <commit hash> <filename>
Remove file from remote repository:
git rm --cached <path/to/file>

###The Silver Searcher Ignore specific files (/filetypes):
ag <search_term> --ignore=<ignored_files>
ag apples --ignore=*.js

Only search specific files (/filetypes):
ag <search_term> -G <included_files>
ag apples -G js$

Note: -G takes a regex (so the above example will search any file that ends in "js").

Print filenames matching (regex) :
ag -g <pattern>
ag -g rb$ shows all files ending in rb

Print only filenames that contain :
ag -l <pattern>
ag -l apples prints the names of files containing the word apples, not the usages themselves.

More options here

Javascript

Get all objects with a given class and attribute value
var fruit = document.querySelectorAll(".apples [type=bananas]");
This will store all elements with class "apples" and attribute type="bananas".

Add cookie for current site (devtools)
document.cookie="myCookieName=myCookieValue"

Zsh

Set sane completion colors on Linux (put in .zshrc)

LS_COLORS='di=34:ln=35:so=32:pi=33:ex=31:bd=46;34:cd=43;34:su=41;30:sg=46;30:tw=42;30:ow=43;30'
export LS_COLORS
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}   

Rbenv

Check currently installed local version
rbenv local
View installable versions
rbenv install -l
Install a Ruby version
rbenv install 2.0.0-p247

Mysql

List tables in current DB
show tables;

Cron

Basic format:
<minute> <hour> <dom> <month> <dow> <user> <cmd> where dom = "day of month" and dow = "day of week"
Examples:
17 8 * * * root echo "This command is run at 8:17 AM"
42 4 1 * * root echo "This command is run at 4:42 on the first of every month"
List crontab for current user
crontab -l
Edit crontab for current user
crontab -e

MongoDB (mongo shell)

Basic commands

show databases
use <database_name>
show collections
coll = db.<collection_name>
coll.find({ date: "2015-01-01", site: "google"}).pretty()

find (linux)

Find and list all mp3 files recursively starting at current path:
find . -name "*.mp3"
Find Mp3, MP3, mP3 too:
find . -iname "*.mp3"
Find and list all files that AREN'T of type mp3:
find . ! -iname "*.mp3"
Find all .bak files and print them in a way that will work properly with xargs and grep etc:
find . -name "*.bak" -print0
Move all .bak files to ~/old.files:
find . -name "*.bak" -print0 | xargs -0 -I {} mv {} ~/old.files
Recursively replace the word apples with the word bananas in all files that aren't of type .bak (or .Bak, .BAK etc.) in the current directory:
find . ! -iname "*.bak" -print0 | xargs -0 sed -i '' -e "s|apples|bananas|g" (OSX will complain unless you use the -e argument; it shouldn't be necessary on linux)
More Examples

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