Common commands
Published:
General shell commands
Commands | Description |
---|---|
echo $SHELL | Display name of active shell (bash or zsh or others) |
man command-name Eg: man bash | grep -C2 ‘$@’ | Get information about the command Here, return 2 leading and trailing lines around the matching text ‘$@’ in the information |
command-name --help | Information about the command usage |
pwd | get current path |
pwd | pbcopy | copy current path to clipboard (Use xcopy or xsel for linux) |
cd - | go back to previous location |
take new_dir | create new_dir and cd into it i.e. mkdir new_dir; cd new_dir |
ls -al | List. a - all l - long listing format d means directory - means file |
ls -ls | list files with detailed info (permission, date, symoblic links) |
ls -1 | wc -l | count number of files in a directory |
cat filename | show the contents of the file filename |
tee Eg: df -h | tee usage.txt | display stdout of a command and write it in a file |
free -h | Show RAM - space used and free |
df -h | Show disk information - space used and free |
du -sh . | Show total size occupied by current directory |
du -sh * | Show size of each file or folder in current directory |
du -sh * | tail -1 | Show total size occupied by the last file in the current directory |
ps ax[c] [| less] | List currently running programs. c - easier to read less - easier to navigate |
pidof process-name | Get the process id of a running process. |
kill process-id | Kill the process |
uname [-[s][a]] | Display name of OS Distribution. a - Detailed info. |
stat filename | Display file status |
alias alias-name | Shows the alias actual command |
date +format E.g. date +%d/%m/%Y | Date command |
cal [-3] [[month] year] E.g. cal -3 june 1996 or cal 1997 or cal | Calendar command. -3 means show previous and the next month as well. |
less file.txt | Show file contents (similar to cat but allows to move up and down) |
more file.txt | Show file contents (similar to cat but allows to move up and down) |
rm -ir | Remove. i - prompt to ask permission for each file. r - recursively delete |
grep [-i] text_to_search /path/to/file | Search for contents in a file , i - case insensitive |
grep -v text_to_search /path/to/file | Search for contents not matching the pattern in a file |
command > file.txt | adds output of command to file.txt. Creates a new file if does not exist. If exists, overwrites the contents of the file. |
command >> file.txt | adds output of command to file.txt. Creates a new file if does not exist. If exists, appends the outputs to the contents of the file. |
find / -name "file_name" [2>/dev/null] Eg: find \ -name "*backup*" 2/dev/null | Find file from the root directory 2/dev/null: 2 takes error output and redirects to dev/null where it is deleted |
find . -not -name "file_name" | Find files not matching the filename |
find . -name “file_name” | xargs -I % rm % | Find and delete files matching the filename |
find . -name "file_name" -exec rm -i {} \; | Find and delete files matching the filename |
find . -name "file_name" -exec grep "Hello" -i {} \; | Find and search “Hello” in files matching the filename |
find -E . -regex ".*/file_name[0-9].sh" | Find files matching the regular expression (this syntax works only in osx) |
find -E . -not -regex ".*/file_name[0-9].sh" | Find files not matching the regular expression (this syntax works only in osx) |
command | grep text_to_search Eg: find / -name “backup” 2>/dev/null | grep $USER | Using pipe to combine grep with other commands |
awk | very powerful command for pattern scanning and processing |
<C-T> | fzf: fuzzy finding files or directories You need to install fzf |
<C-R> | fzf: fuzzy finding commands in history |
<Esc-C> | fzf: fuzzy finding files or directories from current path |
top or htop or ytop or gotop | Process info and CPU Usage (You need to install htop or ytop) |
tree [-aldf][-L level][-P pattern][-I pattern][-o filename] | display directory’s contents in a tree a - all files l - symbolic links d - directories only L - limit number of levels of directory I - files not matching pattern P - files matching pattern o - output to filename You need to install tree |
lsof /dev/nvidia* | awk '{print $2}' | Display ids of processes utilizing CUDA/GPU |
SSH Commands
SSH Tunnelling
To access servers hosted on the remote machine from the local machine
$ ssh -NL port1_server:localhost:port1_local [-NL port2_server:localhost:port2_local]{multiple ports possible} username@remote-ip-address
Example:
$ ssh -NL 8888:localhost:8888 [email protected]
Copy multiple files from remote to local:
$ scp username@remote-ip:/some/remote/directory/\{file1,file2,file3\} /localpath
$ scp username@remote-ip:'/path1/file1 /path2/file2 /path3/file3' /localPath
Other ssh commands
Generate ssh key:
Using ed25519 (more secure: Recommended)
$ ssh-keygen -t ed25519
Using RSA
$ ssh-keygen -t rsa -b 3072
Save ssh host info Modify this file: ~/.ssh/config
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa (path/to/key)
Host targaryen
HostName 192.168.1.10
User daenerys
Port 7654
Host tyrell
HostName 192.168.10.20
Host martell
HostName 192.168.10.50
Host *ell
User oberyn
Host * !martell
LogLevel INFO
Host *
User root
Compression yes
Save ssh password so that no need to re-enter every time
Run this in client (not server)
ssh-copy-id -i path/to/key.pub username@server-ip-address
Example:
ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
Open server in nautilus / file explorer in linux
File explorer: Other locations > Connect to server > sftp://username@ip/
tmux commands
tmux | Create a tmux session with default window name 0 |
tmux new -As name | Create a tmux session with a name or attach to an existing session (if it exists) |
tmux ls | List the active tmux sessions |
tmux a -t name | Attach to an existing tmux session |
tmux kill-session- t name | Kill an existing tmux session |
<prefix> = <c-B> (default), can be changed to <c-A> | |
<prefix> [%”] | (Splitting panes) |
[c-D] | (exit) |
<prefix> D | (get out ) |
<prefix> c | Create a new window (appears in status bar) |
<prefix> 0 | Switch to window 0 |
<prefix> 1 | Switch to window 1 |
<prefix> x | Kill current window |
<prefix> d | Detach tmux (exit back to normal terminal) |
<prefix> z | the active pane is toggled between zoomed and unzoomed |
<prefix> space | switch between split orientations |
<prefix> ! | Break current pane to a new window |
<prefix> | Swap pane within a window | |
<prefix> () | Switch between tmux sessions |
<prefix> <C-o> | Swap pane within a window |
<prefix> :move-window -t 2 | rename current window to 2 if 2 does not exist |
<prefix> :resize-pane -D n | Resizes the current pane down by n cells |
<prefix> :resize-pane -U n | Resizes the current pane upward by n cells |
<prefix> :resize-pane -L n | Resizes the current pane left by n cells |
<prefix> :resize-pane -R n | Resizes the current pane right by n cells |
<prefix> :join-pane [-dhv] [-l size | -p percentage] [-s src-pane] [-t dst-pane] Eg: <prefix> :join-pane -v -s 4 -t :1 | Join one pane to another |
<prefix> <c-S> | save current state You need to install tmux-resurrect |
<prefix> <c-R> | reload saved state |
Vim commands
I. Pure Vim
Syntax:
Verbs (operations) + Noun (text on which operation is performed)
[count] [operation] [text object / motion]
Run bash commands in vim
:[.]!command
. (dot)
- outputs the command into the current buffer
1. VIM Verbs (operations)
c | change |
d | delete |
C | change everything from where your cursor is to the end of the line |
D | delete everything from where your cursor is to the end of the line |
dd | delete a line |
x | delete a sigle character |
y | yank text into the copy buffer. |
yy or Y | yank line into the copy buffer. |
v | highlight one character at a time. |
V | highlight one line at a time. |
<c-v> | highlight by columns. |
p | paste text after the current line. |
P | paste text on the current line. |
> | Shift Right |
< | Shift Left |
= | Indent |
gU | make uppercase |
gu | make lowercase |
~ | swap case |
2. VIM Nouns (text)
i. Text Objects
Must be combined with verbs
iw | inner word (non whitespace) (works from anywhere in a word) |
aw | word with surrounding white space (works from anywhere in a word) aw ~ W. Difference in position. E.g. For dw, cursor must be at beginning, whereas daw works from any position. |
ib | inner bracket (the contents of an HTML tag) |
ab | a bracket |
it | inner tag (the contents of an HTML tag) |
at | a tag block |
i” | inner quotes |
a” | a quote |
ip | inner paragraph |
ap | a paragraph |
is | inner sentence |
as | a sentence |
Combination examples:
gUiw | capitalize a word |
ci( | change inner bracket |
6dW | delete 6 words |
yis | copy inner sentence |
di” | delete inner quotes |
ii. Motions
Can be combined with verbs or used independently
[count] w/W | go a (word / word with whitespace) to right |
[count] b/B | go a (word / word with whitespace) to left |
[count] e/E | go to the end of (word / word with whitespace) |
[count] ]m | go to the beginning of next method |
[count] h / j / k / l | left / down / up / right |
[count] f/F [char] [;,]+ | go to the next occurence of character |
[count] t/T [char] [;,]+ | go to just before the next occurence of character |
% | move to matching parenthesis pair |
[count] + | down to first non blank char of the line. |
[count]$ | moves the cursor to the end of the line. |
0 | moves the cursor to the beginning of the line. |
G | move to the end of the file. |
gg | move to the beginning of the file. |
]m or [m | Mode between methods. |
Combination examples:
3ce | Change 3 words to end |
d]m | delete start of next method |
ctL | change upto before the next occurence of L |
d]m | delete start of next method |
3. Other important vim commands
i | Insert to left of cursor |
a | Insert to right of cursor |
A | insert at end of line |
I | insert at beginning of line |
o | insert at beginning of next line |
O | insert at beginning of previous line |
u | undo |
<c-r> | will redo the last undo. |
/text | search for text |
:%s/text/replacement text/g | search through the entire document for text and replace it with replacement text. |
:%s/text/replacement text/gc | search through the entire document and confirm before replacing text. |
* | search forward for word under cursor |
# | search backward for word under cursor |
:vsplit | vertical split windows |
m[a-zA-Z] | sets a custom mark whose location can be accessed using `[mark] and line accessed using ‘[mark] |
g; | goto last cursor position |
’. | move to the last edit |
:marks | show all current marks that are being used |
:w | write |
:w file_name | write the changes to a new file |
:q | quit |
:q! or ZQ | force quit |
:wq or ZZ | write and quit |
:w !sudo tee % | Write with sudo permissions if permission not available |
:bd | remove buffer |
[:vert] :sf filename | find file and open in split mode |
<c-v> select multiple lines then I or A and type the required text | insert text at multiple lines at the beginning or the end |
q <char > commands q [count] @ <char > | record command macros apply recorded commands |
:ab ipho International Physics Olympiad | Set abbreviation for long terms for easy typing Use <C-v> to prevent expansion |
norm command. Eg vip then :norm Ithis comes to the left | Applies sequence of button presses / commands to each line selected Select a paragraph and add the text to the left of each line in the paragraph |
Global commands. Eg :g/^@/m$ | Apply commands to lines matching particular pattern Move all lines starting with @ to the end of the document |
Timetravel Eg :earlier 10m :earlier 5h :later 2h | Move to the file state in the past or future as specified |
4. Useful key remappings
jk (Custom- inoremap jk <Esc> ) | <Esc> |
kj (Custom)inoremap kj <Esc> | <Esc> |
nnoremap <C-c>| <Esc> | |
nnoremap <C-s> | :w<CR> |
nnoremap <C-Q> | :wq!<CR> |
Better window navigation | |
nnoremap <C-h> | <C-w>h |
nnoremap <C-j> | <C-w>j |
nnoremap <C-k> | <C-w>k |
nnoremap <C-l> | <C-w>l |
5. Using Args
Args are list of files initially opened. So, it’s a subset of buffers.
:args | display args files |
:args */.yaml | add files to args |
:sall | open all args files in split mode |
:vert sall | open all args files in vertical split mode |
:windo difft | show differences in all args files |
c-x, c-l | autocomplete |
:vim TODO/ ## | search in all args files |
:cdo s/TODO/DONE/g | replace in all args files |
6. Scrolling and motions
<c-u > , <c-d > | Up, down scroll |
{ } | Up, down scroll between spaces |
<c-b > , <c-f > | Up, down full screen scroll |
<c-y > , <c-e > | Up, down scroll by lines |
H / M / L | Navigations to top / middle / bottom |
zt | Put current cursor position to top |
zz | Put current cursor position to middle |
zb | Put current cursor position to bottom |
II. Vim Plugins commands
Install any vim plugin manager like vim-plug.
To apply latest settings:
:source $MYVIMRC
Ranger
First, instll ranger
Mac
brew install ranger
Linux
sudo apt install ranger
Install ranger plugin for vim
" Ranger in vim
Plug 'francoiscabrol/ranger.vim'
" Dependency for ranger in neovim
Plug 'rbgrouleff/bclose.vim'
When ranger is open in vim or externally
cw | Rename file/dir : change word |
A | Rename file: add at the end of extension |
a | Rename file: add just before the extension |
I | Rename file/dir: add at the front of the filename/directory |
:bulkrename | Rename a list of files/directories |
:mkdir newdir | Create new directory |
Space | Highlight/Select files / directories |
V | Highlight/Select files / directories similar to visual mode |
uv | Undo highlight/select |
yy | Copy/yank file/dir |
dd | Cut file/dir |
pp | Paste file/dir. If file exists, new file created with _ at the end of the name |
po | Paste but overwrite file/dir |
uy | Undo Copy/yank |
dD | Delete |
Z (Custom mapping) | Compress using an external script mapped to ranger |
x (Custom mapping) | Extract using an external script mapped to ranger |
1. vim-surround commands
Install Plug tpope/vim-surround
ds[‘“bB{}t] | delete surrounding quotes |
cs[‘“bB{}t] [‘“bB{}t] | change surrounding quotes |
ysiw[‘“bB{}t] | add surrounding quotes “ |
v-select, S[‘“bB{}t] | add surrounding |
Examples:
<p> Hello </p> | cst<h2> | <h2> Hello </h2> |
if *x>3{ | ysW( | if ( x>3 ) { |
*“hello” | ysWf print<cr(Enter)> | print(“hello”) |
2. Git plugins commands
Install these plugins first
" Show differences with style
Plug 'mhinz/vim-signify'
" Main GIT PLugin :Git
Plug 'tpope/vim-fugitive'
" Git Hub plugin, enables :Gbrowse
Plug 'tpope/vim-rhubarb'
" Git commit browser
Plug 'junegunn/gv.vim'
" Git commit history in each line
<c-o> <c-i> | Toggle between buffers |
:Git diff | Show git differences |
:Gdiffsplit | Show differences in split mode |
:GBrowse | Open the repository in github |
:GV | Show git commit history |
3. Coc commands
Install COC plugin first
" Intellisense
Plug 'neoclide/coc.nvim', {'branch': 'release'}
gd | Goto Definitions of variable under cursor |
gr | Goto References of variable under cursor |
:CocInstall tool_name E.g. :CocInstall coc-python | Installing coc tools |
:CocUninstall tool_name | Uninstalling coc tools |
:CocList extensions (Tab for autocompletion) | Show extensions |
:CocCommand | execute a COC command |
o | expand/collapse in Coc explorer (First run :CocInstall coc-explorer) |
4. coc-python
Install coc-python first
:CocInstall coc-python
Shift K | doc hint |
:Format | autopep8 formatting |
<C-w> w | Switch cursors between sidebar and code |
<C-n> <C-n> <C-n> c I A | multiple cursors: change Insert at first Insert at end |
5. FZF
Install fzf in system and fzf plugin
OSx
brew install fzf
# To install useful key bindings and fuzzy completion:
$(brew --prefix)/opt/fzf/install
brew install ripgrep
Linux
sudo apt install fzf
sudo apt install ripgrep
FZF Plugin
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
Plug 'airblade/vim-rooter'
:Rg | Find word inside file |
:BLines | Find all occurrences of word in a giant file |
:Lines | Same as above but search in all buffers |
:History: | History of commands ran in vim |
:Ag | similar to Rg but |
:Buffers | Search through buffers |
> | Tab |
gf | Goto file: open file directly from path written in vim |
6. Startify
Install Startify Plugin for Project management
" Start Screen
Plug 'mhinz/vim-startify'
:SSave | Save session |
:SLoad | Load session |
7. vim-commentary
" Better Comments
Plug 'tpope/vim-commentary'
gc[<count>] <Text object> (Examples below) | Comment out the target of a motion |
gcap | Comment out a paragraph |
gcw | Comment out the current line |
gc2j | Comment out the current line and 2 lines below it |
Easy remapping |nnoremap <leader>
/| :Commentary<CR>
|vnoremap <leader>
/| :Commentary<CR>
Git commands
git commit –amend | add to previous commits |
git commit -m $’Heading commit\n\nCommit description\nLine 2 of description’ | Commit and commit description in 1 line |
git push origin -f branchname | forced push |
git rebase master | merge changes of master onto the current branch (first pull from master before rebase) |
git log | |
git diff | |
git remote -v | show repo information |
git reset –hard | |
git show | |
git config –global user.name | |
git config –global user.email | |
git reset | remove file from the current index (the “about to be committed” list) without changing anything else. |
git checkout filename | Undo local changes to latest commit |
git stash | Stash local changes temporarily |
git stash list | Show stashed branches |
git stash show | Show the latest stashed file changes |
git stash show -p N | Show the Nth (see number in git stash list) stashed file changes |
git stash drop stash@{index} | Remove the given stash |
git stash clear | Remove all stashes |
git stash list | awk -F: '{ print "\n\n\n\n"; print $0; print "\n\n"; system("git stash show -p " $1); }' | Show the changes in the stash in detail |
Ignore files that have already been committed to the repo
$ git rm -r --cached .
$ git add .
$ git commit -m "Clean up ignored files"
Hard delete unpublished commits
git reset --hard commit_id (reset to the particular commit. It will destroy any local modifications.)
Alternatively, if there’s work to keep
git stash
git reset --hard commit_id
git stash pop
This saves the modifications, then reapplies that patch after resetting. You could get merge conflicts, if you’ve modified things which were changed since the commit you reset to.
Undo published commits with new commits
This will create three separate revert commits:
git revert a867b4af 25eee4ca 0766c053
It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD
Similarly, you can revert a range of commits using commit hashes:
git revert a867b4af..0766c053
Reverting a merge commit
git revert -m 1 <merge_commit_sha>
To get just one, you could use
rebase -i
to squash them afterwards Or, you could do it manually (be sure to do this at top level of the repo) get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .
Then commit. Be sure and write a good message describing what you just did
git commit
Git reset
git reset does know five “modes”: soft, mixed, hard, merge and keep. I will start with the first three, since these are the modes you’ll usually encounter. After that you’ll find a nice little a bonus, so stay tuned.
soft
When using
git reset --soft HEAD~1
you will remove the last commit from the current branch, but the file changes will stay in your working tree. Also the changes will stay on your index, so following with a git commit will create a commit with the exact same changes as the commit you “removed” before.
mixed
This is the default mode and quite similar to soft. When “removing” a commit with
git reset HEAD~1
you will still keep the changes in your working tree but not on the index; so if you want to “redo” the commit, you will have to add the changes (git add) before commiting.
hard
When using
git reset --hard HEAD~1
you will lose all uncommited changes in addition to the changes introduced in the last commit. The changes won’t stay in your working tree so doing a git status command will tell you that you don’t have any changes in your repository.
Tread carefully with this one. If you accidentally remove uncommited changes which were never tracked by git (speak: committed or at least added to the index), you have no way of getting them back using git.
Bonus (keep)
git reset --keep HEAD~1
is an interesting and useful one. It only resets the files which are different between the current HEAD and the given commit. It aborts the reset if anyone of these files has uncommited changes. It’s basically acts as a safer version of hard.
This mode is particularly useful when you have a bunch of changes and want to switch to a different branch without losing these changes - for example when you started to work on the wrong branch.
Remove sensitive file from github repo history
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
git push -f origin master
Other git commands
git rm | rm plus git add combined
git rm --cached | file removed from the index (staging it for deletion on the next commit), but keep your copy in the local file system.
i3wm commands
- pavucontrol
- alsamixer
mod + r - resize mode , then arrow keys or vim keys | |
mod + Shift + e - exit | |
mod + d - dmenu | |
mod+Shift+c reload | |
mod+Shift+r restart |
Alt+Shift- Change keyboard language
Brew bundle for OSx
Usage
Create a Brewfile
in the root of your project with:
touch Brewfile
Add your dependencies in your Brewfile
:
tap "homebrew/cask"
tap "user/tap-repo", "https://[email protected]/user/homebrew-tap-repo.git"
cask_args appdir: "/Applications"
brew "imagemagick"
brew "denji/nginx/nginx-full", args: ["with-rmtp-module"]
brew "[email protected]", restart_service: true, link: true, conflicts_with: ["mysql"]
cask "firefox", args: { appdir: "~/my-apps/Applications" }
cask "google-chrome"
cask "java" unless system "/usr/libexec/java_home --failfast"
mas "1Password", id: 443987910
whalebrew "whalebrew/wget"
cask
and mas
entries are automatically skipped on Linux. Other entries can be run only on (or not on) Linux with if OS.mac?
or if OS.linux?
.
Install
You can then easily install all dependencies with:
brew bundle
Any previously-installed dependencies which have upgrades available will be upgraded.
brew bundle
will look for a Brewfile
in the current directory. Use --file
to specify a path to a different Brewfile
, or set the HOMEBREW_BUNDLE_FILE
environment variable; --file
takes precedence if both are provided.
My .Brewfile
is stored in the home directory and the HOMEBREW_BUNDLE_FILE
environment variable is set to ~/.Brewfile
You can skip the installation of dependencies by adding space-separated values to one or more of the following environment variables:
HOMEBREW_BUNDLE_BREW_SKIP
HOMEBREW_BUNDLE_CASK_SKIP
HOMEBREW_BUNDLE_MAS_SKIP
HOMEBREW_BUNDLE_WHALEBREW_SKIP
HOMEBREW_BUNDLE_TAP_SKIP
brew bundle
will output a Brewfile.lock.json
in the same directory as the Brewfile
if all dependencies are installed successfully. This contains dependency and system status information which can be useful in debugging brew bundle
failures and replicating a “last known good build” state.
You can opt-out of this behaviour by setting the HOMEBREW_BUNDLE_NO_LOCK
environment variable or passing the --no-lock
option.
You may wish to check this file into the same version control system as your Brewfile
(or ensure your version control system ignores it if you’d prefer to rely on debugging information from a local machine).
Dump
You can create a Brewfile
from all the existing Homebrew packages you have installed with:
brew bundle dump
The --force
option will allow an existing Brewfile
to be overwritten as well. The --describe
option will output a description comment above each line. The --no-restart
option will prevent restart_service
from being added to brew
lines with running services.
Cleanup
You can also use a Brewfile
to list the only packages that should be installed, removing any package not present or dependent. This workflow is useful for maintainers or testers who regularly install lots of formulae. To uninstall all Homebrew formulae not listed in the Brewfile
:
brew bundle cleanup
Unless the --force
option is passed, formulae that would be uninstalled will be listed rather than actually be uninstalled.
Check
You can check there’s anything to install/upgrade in the Brewfile
by running:
brew bundle check
This provides a successful exit code if everything is up-to-date, making it useful for scripting.
For a list of dependencies that are missing, pass --verbose
. This will also check all dependencies by not exiting on the first missing dependency category.
List
Outputs a list of all of the entries in the Brewfile.
brew bundle list
Pass one of --casks
, --taps
, --mas
, --whalebrew
or --brews
to limit output to that type. Defaults to --brews
. Pass --all
to see everything.
Note that the type of the package is not included in this output.
Exec
Runs an external command within Homebrew’s superenv build environment.
brew bundle exec -- bundle install
This sanitized build environment ignores unrequested dependencies, which makes sure that things you didn’t specify in your Brewfile
won’t get picked up by commands like bundle install
, npm install
, etc. It will also add compiler flags which will help find keg-only dependencies like openssl
, icu4c
, etc.
Restarting services
You can choose whether brew bundle
restarts a service every time it’s run, or only when the formula is installed or upgraded in your Brewfile
:
# Always restart myservice
brew 'myservice', restart_service: true
# Only restart when installing or upgrading myservice
brew 'myservice', restart_service: :changed
Leave a Comment