Learning Notes - Linux Commands
This post documents my learning notes of useful Linux commands
Examples used is my VIM key mapping configuration file: ~/.config/nvim/lua/user/keymappings.lua
local opts = { noremap = true, silent = true }
local term_opts = { silent = true }
local keymap = vim.api.nvim_set_keymap
--Remap space as leader key
keymap("", "<Space>", "<Nop>", opts)
vim.g.mapleader = " "
vim.g.maplocalleader = " "
-- Modes
-- normal_mode = "n",
-- insert_mode = "i",
-- visual_mode = "v",
-- visual_block_mode = "x",
-- term_mode = "t",
-- command_mode = "c",
-- Normal --
keymap("n", "<S-e>", "$", opts)
keymap("n", "<S-b>", "^", opts)
keymap("n", "<S-y>", "yg_", opts)
-- remap j/k to gj/gk
keymap("n", "j", "gj", opts)
keymap("n", "k", "gk", opts)
keymap("n", "gj", "j", opts)
keymap("n", "gk", "k", opts)
# grep
: Pattern Search
grep
searches particular regular expression patterns in file(s). This is extremely useful when you need to search for a particular piece of information in one or multiple long text files.
For example, I can search all my keybindings that involves leader key.
grep -i '<leader>' keymappings.lua
This would give me the following:
keymap("n", "<leader>c", ":bd<CR>", opts)
keymap("n", "<leader>e", ":NERDTreeToggle<CR>", opts)
keymap("n", "<leader>tf", ":NERDTreeFind<CR>", opts)
--keymap("n", "<leader>t", ":VimwikiToggleListItem<CR>", opts)
keymap('n', "<leader>ff", ":lua require('telescope.builtin').find_files()<cr>",opts)
keymap('n', "<leader>fg", ":lua require('telescope.builtin').live_grep()<cr>",opts)
keymap('n', "<leader>fb", ":lua require('telescope.builtin').buffers()<cr>",opts)
keymap('n', "<leader>fh", ":lua require('telescope.builtin').help_tags()<cr>",opts)
keymap("n", "<leader>h", ":noh<CR>", opts)
keymap("n", "<leader>s", "1z=<CR>", opts)
keymap("n", "<leader>v", ":vsplit<CR>", opts)
Many times you wound want to search file using the regular expression. For example, I can search all the modes in my configuration file.
grep -P '\-\-.{0,}\-\-' keymappings.lua
This will list out the section lines in the file which are written in a pattern like "-- Mode Name --". Linxu usually support pearl regular expressions.
-- Normal --
-- Insert --
-- Visual --
-- Visual Block --
-- Command Mode --
There are also output options you can use with grep, meaning you can not only just output the line with matched expressions. You can also output n lines before (-B) or n lines after (-A) or both (-C). This is useful when you want to find the context of the line when you search. For example I can search anything associated with vimwiki
and return 3 lines before and after the matching line.
grep -i -C 3 'vimwiki' keymappings.lua
Below is the output.
keymap("n", "<leader>c", ":bd<CR>", opts)
keymap("n", "<leader>e", ":NERDTreeToggle<CR>", opts)
keymap("n", "<leader>tf", ":NERDTreeFind<CR>", opts)
--keymap("n", "<leader>t", ":VimwikiToggleListItem<CR>", opts)
-- Telescope
keymap('n', "<leader>ff", ":lua require('telescope.builtin').find_files()<cr>",opts)
# sed
sed is used to find & replace patterns in an input stream (text file)
Useful flags:
-i
: inplace, useful when you need to modify the file-n
: don't output the input stream-E
: extended regular expressions such as grouping
Useful commands:
s
: replaced
: deletec
: changep
: print
Useful tricks:
- Use line numbers to specify a particular line
- Use comma to specify a block
- Use grouping
()
in the search part and then reuse it in the replace part
# Examples
- sed to replace grep
sed -n /find_pattern/p input file
- To find and replace a string (-n to show only affected lines):
# To output the replaced stream
sed -n s/old_string/new_string/p input_file
# To change inplace in the file
sed -i s/old_string/new_string/ input_file
- To find and replace within a block of text
sed -n /block_start/,/block_end/s/old_string/new_string/p input_file
- To delete an entire block
sed /block_start,/block_end/d input_file
- To change and entire blcok with new contents
sed /block_start/,/block_end/cNew\ Block\ Content input_file
# awk
awk is a more powerful tool (more like a programming language) to handle text streams. awk
is specifically powerful to deal with csv files.
The basics of awk are:
awk 'the_pattern_to_search_for {the actions to perform}'
Special identifiers:
$1, $4
: column 1 to column 4$0
: all columns$NF
: the last columnFNR == n
: the n-th row (both row and column are 1-based)NR
: line numbers,
: similar to sed use , to specify blocksBEGIN/END
: first/last match
Useful flags:
-F
: to specify column separator (by default is space).-i
: source a library (such as-i inplace
)
Functions can be used inside the pattern part
- search:
/regex_pattern/
length
Functions used inside the action part
if(...)
for(i=1;i<=10;i++) ...
Useful tricks
# Examples
- Replacement for grep
# grep lines with 'bin' from /etc/passwd
awk '/bin/' /etc/passwd
# grep the first column
awk -F ":" '{print $1}' /etc/passwd
# grep the first row
awk 'FNR==1' /etc/passwd
- Replacement for sed
awk -F ":" '/root/ {print $1, "FOUND: ", $2}' /etc/passwd