Common commands

26 minute read

Published:

General shell commands

CommandsDescription
echo $SHELLDisplay 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 --helpInformation about the command usage
pwdget current path
pwd | pbcopycopy current path to clipboard (Use xcopy or xsel for linux)
cd -go back to previous location
take new_dircreate new_dir and cd into it i.e. mkdir new_dir; cd new_dir
ls -alList. a - all
l - long listing format
d means directory - means file
ls -lslist files with detailed info (permission, date, symoblic links)
ls -1 | wc -lcount number of files in a directory
cat filenameshow 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 -hShow RAM - space used and free
df -hShow 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 -1Show 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-nameGet the process id of a running process.
kill process-idKill the process
uname [-[s][a]]Display name of OS Distribution. a - Detailed info.
stat filenameDisplay file status
alias alias-nameShows the alias actual command
date +format E.g. date +%d/%m/%YDate 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.txtShow file contents (similar to cat but allows to move up and down)
more file.txtShow file contents (similar to cat but allows to move up and down)
rm -irRemove. i - prompt to ask permission for each file. r - recursively delete
grep [-i] text_to_search /path/to/fileSearch for contents in a file , i - case insensitive
grep -v text_to_search /path/to/fileSearch for contents not matching the pattern in a file
command > file.txtadds output of command to file.txt. Creates a new file if does not exist. If exists, overwrites the contents of the file.
command >> file.txtadds 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
awkvery 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 gotopProcess 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

  
tmuxCreate a tmux session with default window name 0
tmux new -As nameCreate a tmux session with a name or attach to an existing session (if it exists)
tmux lsList the active tmux sessions
tmux a -t nameAttach to an existing tmux session
tmux kill-session- t nameKill 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> cCreate a new window (appears in status bar)
<prefix> 0Switch to window 0
<prefix> 1Switch to window 1
<prefix> xKill current window
<prefix> dDetach tmux (exit back to normal terminal)
<prefix> zthe active pane is toggled between zoomed and unzoomed
<prefix> spaceswitch 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 2rename current window to 2 if 2 does not exist
<prefix> :resize-pane -D nResizes the current pane down by n cells
<prefix> :resize-pane -U nResizes the current pane upward by n cells
<prefix> :resize-pane -L nResizes the current pane left by n cells
<prefix> :resize-pane -R nResizes 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)

  
cchange
ddelete
Cchange everything from where your cursor is to the end of the line
Ddelete everything from where your cursor is to the end of the line
dddelete a line
xdelete a sigle character
yyank text into the copy buffer.
yy or Yyank line into the copy buffer.
vhighlight one character at a time.
Vhighlight one line at a time.
<c-v>highlight by columns.
ppaste text after the current line.
Ppaste text on the current line.
>Shift Right
<Shift Left
=Indent
gUmake uppercase
gumake lowercase
~swap case

2. VIM Nouns (text)

i. Text Objects

Must be combined with verbs

  
iwinner word (non whitespace) (works from anywhere in a word)
awword 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.
ibinner bracket (the contents of an HTML tag)
aba bracket
itinner tag (the contents of an HTML tag)
ata tag block
i”inner quotes
a”a quote
ipinner paragraph
apa paragraph
isinner sentence
asa sentence

Combination examples:

  
gUiwcapitalize a word
ci(change inner bracket
6dWdelete 6 words
yiscopy inner sentence
di”delete inner quotes

ii. Motions

Can be combined with verbs or used independently

  
[count] w/Wgo a (word / word with whitespace) to right
[count] b/Bgo a (word / word with whitespace) to left
[count] e/Ego to the end of (word / word with whitespace)
[count] ]mgo to the beginning of next method
[count] h / j / k / lleft / 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.
0moves the cursor to the beginning of the line.
Gmove to the end of the file.
ggmove to the beginning of the file.
]m or [mMode between methods.

Combination examples:

  
3ceChange 3 words to end
d]mdelete start of next method
ctLchange upto before the next occurence of L
d]mdelete start of next method

3. Other important vim commands

  
iInsert to left of cursor
aInsert to right of cursor
Ainsert at end of line
Iinsert at beginning of line
oinsert at beginning of next line
Oinsert at beginning of previous line
uundo
<c-r>will redo the last undo.
/textsearch for text
:%s/text/replacement text/gsearch through the entire document for text and replace it with replacement text.
:%s/text/replacement text/gcsearch through the entire document and confirm before replacing text.
*search forward for word under cursor
#search backward for word under cursor
:vsplitvertical 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
:marksshow all current marks that are being used
:wwrite
:w file_namewrite the changes to a new file
:qquit
:q! or ZQforce quit
:wq or ZZwrite and quit
:w !sudo tee %Write with sudo permissions if permission not available
:bdremove buffer
[:vert] :sf filenamefind file and open in split mode
<c-v> select multiple lines then I or A and type the required textinsert 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 OlympiadSet 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.

  
:argsdisplay args files
:args */.yamladd files to args
:sallopen all args files in split mode
:vert sallopen all args files in vertical split mode
:windo difftshow differences in all args files
c-x, c-lautocomplete
:vim TODO/ ##search in all args files
:cdo s/TODO/DONE/greplace 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 / LNavigations to top / middle / bottom
ztPut current cursor position to top
zzPut current cursor position to middle
zbPut 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

  
cwRename file/dir : change word
ARename file: add at the end of extension
aRename file: add just before the extension
IRename file/dir: add at the front of the filename/directory
:bulkrenameRename a list of files/directories
:mkdir newdirCreate new directory
SpaceHighlight/Select files / directories
VHighlight/Select files / directories similar to visual mode
uvUndo highlight/select
yyCopy/yank file/dir
ddCut file/dir
ppPaste file/dir. If file exists, new file created with _ at the end of the name
poPaste but overwrite file/dir
uyUndo Copy/yank
dDDelete
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 diffShow git differences
:GdiffsplitShow differences in split mode
:GBrowseOpen the repository in github
:GVShow git commit history

3. Coc commands

Install COC plugin first

" Intellisense
Plug 'neoclide/coc.nvim', {'branch': 'release'}
  
gdGoto Definitions of variable under cursor
grGoto References of variable under cursor
:CocInstall tool_name E.g. :CocInstall coc-pythonInstalling coc tools
:CocUninstall tool_nameUninstalling coc tools
:CocList extensions (Tab for autocompletion)Show extensions
:CocCommandexecute a COC command
oexpand/collapse in Coc explorer (First run :CocInstall coc-explorer)

4. coc-python

Install coc-python first

:CocInstall coc-python
  
Shift Kdoc hint
:Formatautopep8 formatting
<C-w>wSwitch 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'
  
:RgFind word inside file
:BLinesFind all occurrences of word in a giant file
:LinesSame as above but search in all buffers
:History:History of commands ran in vim
:Agsimilar to Rg but
:BuffersSearch through buffers
>Tab
gfGoto file: open file directly from path written in vim

6. Startify

Install Startify Plugin for Project management

" Start Screen
Plug 'mhinz/vim-startify'
  
:SSaveSave session
:SLoadLoad session

7. vim-commentary

" Better Comments
Plug 'tpope/vim-commentary'
  
gc[<count>] <Text object> (Examples below)Comment out the target of a motion
gcapComment out a paragraph
gcwComment out the current line
gc2jComment out the current line and 2 lines below it

Easy remapping |nnoremap <leader>/| :Commentary<CR> |vnoremap <leader>/| :Commentary<CR>

Git commands

  
git commit –amendadd 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 branchnameforced push
git rebase mastermerge changes of master onto the current branch (first pull from master before rebase)
git log 
git diff 
git remote -vshow repo information
git reset –hard eg git reset --hard HEAD@1 
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 filenameUndo local changes to latest commit
git stashStash local changes temporarily
git stash listShow stashed branches
git stash showShow the latest stashed file changes
git stash show -p NShow the Nth (see number in git stash list) stashed file changes
git stash drop stash@{index}Remove the given stash
git stash clearRemove 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

References

Leave a Comment