unzip aaa.zip -d dest # unzip aaa.zip to folder 'dest'
<some command> | sort -u # print only unique lines (note uniq is not the tool for this)
ls * | grep -E '\..*$' -o | sort -u # print list of unique file extensions
- Delete next word: Alt-D
- Delete prev word: Ctrl-W | Ctrl-X + Backspace
- Remove character: Ctrl+D or Delete – remove or deletes the character under the cursor.
- Ctrl+K – removes all text from the cursor to the end of the line.
- !! – execute last command.
- !top – execute the most recent command that starts with ‘top’ (e.g. !).
- !top:p – displays the command that !top would run (also adds it as the latest command in the command history).
- !$ – execute the last word of the previous command (same as Alt +., e.g. if last command is ‘cat tecmint.txt’, then !$ would try to run ‘tecmint.txt’).
- !$:p – displays the word that !$ would execute.
- !* – displays the last word of the previous command.
- !:p – displays the last word that ! would substitute.
- find file:
find . -type f -name "test1.txt"
- find files:
find . -type f -name "test*"
- find directory:
find . -type d -name "some-dir"
- case insensitive:
find . -type f -iname "test*"
- files modified in last 10 mins:
find . -type f -mmin -10
- files modified more than 1 min ago, less than 5 mins ago:
find . -type f -mmin +1 -mmin -5
- find empty file:
find . -empty
- find files over 5 MB:
find . -size +5M
- find files over 5 KB:
find . -size +5k
- find files over 5 GB:
find . -size +5G
- list all file paths recursively:
find . -type f
-OPTIONS: - size # takes M k or G - mtime # days - mmin - cmin, ctime # change minutes or days - amin, atime # access minutes or days
history # shows all history (including current)
cat ~./bash_history # shows all history from past sessions (not current)
history n # shows n most recent lines of history with numbers
history -w /dev/stdout # prints history without numbers
history -w /dev/stdout | tail -n n # prints n most recent lines of history without numbers
- see installed packages:
dpkg -l
- uninstall package:
sudo apt-get remove packagename
- uninstall package and config files:
sudo apt-get purge packagename
- highlight with color: https://linuxcommando.blogspot.com/2007/10/grep-with-color-output.html
- search all files of specific type in all subdirectories:
grep -r --include "*.py" <searchstring>
- omit dash separators:
grep --no-group-separator
- search the NOT condition (i.e. invert match)
grep -v
- show line numbers:
less -N
- Substitutions:
$(patsubst %,raw/%.csv,file smile tile)
-->raw/file.csv raw/smile.csv raw/tile.csv
- Implicit recipe (where
$*
is whatever the%
is):
output/%.csv : src/script_to_do raw/%.zip
python $^ $@ $*
- Print the recipe instead of running it (dry run):
make -n target-file.csv
- Use .RECIPEPREFIX += to allow multiple spaces instead of tabs in the makefile Issues with Spaces
- 'rename' syntax: `$ rename 's///'
- print out changes:
rename -v
- dry run:
rename -n
- reference groups in replacement:
$ rename -v -n 's/file_(\d+)/$1_file/' file_*
-->rename(file_2020.txt, 2020_file.txt)
- print between lines:
sed -n '10,40p' filename.txt # prints lines 10 to 40
- find and replace:
sed 's/old-text/new-text/g' input.txt
- regex find and replace:
sed -E 's/^\s+/ - /g' file.txt
- delete all empty/blank lines:
sed '/^$/d' /tmp/data.txt
check Ubuntu version: lsb_release -a
- Set up noclobber to avoid overwriting files:
set -o noclobber
(add to .bashrc) - Prompt when copying or moving (
-i
= 'interactive'):cp -i
,mv -i
(add to .bashrc:alias cp='cp -i'
)
- Open first file in an
ls
:vim $(ls -t some-file-pattern*txt | head -n 1)
- Find filename and pipe to Vim:
grep google= ~/.bashrc | sed -E 's/.*(\/mnt.*\.sh).*/\1/' | xargs -o vim
- Count lines in file:
wc -l filename.csv
- Count lines in files:
wc -l data/*.csv
- Count columns in csv file:
head -1 file.csv | sed 's/[^,]//g' | wc -c
grep
with color:grep --color=auto
cat
with color:highlight -O ansi
less
with color:cless () {ccat $1 | less -R; }
- Other Sources/Ideas:
Get the stem of a filename with parameter substitution/pattern matching src
$ x=abc.txt
$ echo ${x%.txt}
abc
for f in *.opml; do pandoc $f -o ${f%.opml}.md; done;
References:
- Convert Bash input to vim mode: add
set editing-mode vi
to the~/.inputrc
file. src - or ad hoc: type
set -o vi
in the terminal - Turn of dinging: put line
set bell-style none
in your/etc/inputrc
or~/.inputrc
file ($ echo 'set bell-style none' >> ~/.inputrc
) (src)
Char | Word | Line | |
---|---|---|---|
RIGHT | Ctrl-F | Alt-F* | Ctrl-E |
LEFT | Ctrl-B | Alt-B* | Ctrl-A |
UP | Ctrl-P | ||
DOWN | Ctrl-N |
* Alt- can be done instead by doing Esc then
- Move one char forward:
Ctrl-F
orRight
- Move one char backward:
Ctrl-B
orLeft
- Move one word forward:
Alt-F
,Ctrl-Right
, orEsc + F
- Move one word backward:
Alt-B
,Ctrl-Left
, orEsc + B
- Move one line up:
Ctrl-P
- Move one line down:
Ctrl-N
- Move home:
Ctrl-A
- Move end:
Ctrl-E
- Delete char:
Ctrl-D
orDel
- Delete previous word:
Ctrl-W
- Delete next word:
Alt-D
- Delete to end of line:
Ctrl-K
- Delete to start of line:
Ctrl-U
orCtrl-X + Backspace
- Swap char with previous:
Ctrl-T
- Transposes the two words immediately before (or under) the cursor:
Esc + T
- Transforms the text from the cursor to the end of the word to uppercase:
Esc + U
- Transforms the text from the cursor to the end of the word to lowercase:
Esc + L
- Changes the letter under the cursor (or the first letter of the next word) to upper:
Esc + C
- Keep using shell after opening other window: append
&
to the command src - Ctrl+Z – suspend the current foreground process. This sends the SIGTSTP signal to the process. You can get the process back to the foreground later using the fg process_name (or %bgprocess_number like %1, %2 and so on) command.
- Ctrl+C – interrupt the current foreground process, by sending the SIGINT signal to it. The default behavior is to terminate a process gracefully, but the process can either honor or ignore it.
- Ctrl+D – exit the bash shell (same as running the exit command).
- Ctrl+L – clears the screen (same effect as the “clear” command).
- Ctrl+S – pause all command output to the screen. If you have executed a command that produces verbose, long output, use this to pause the output scrolling down the screen.
- Ctrl+Q – resume output to the screen after pausing it with Ctrl+S. src
- Up arrow key – retrieves the previous command. If you press it constantly, it takes you through multiple commands in history, so you can find the one you want. Use the Down arrow to move in the reverse direction through the history.
- Ctrl+P and Ctrl+N – alternatives for the Up and Down arrow keys, respectively.
- Ctrl+R – starts a reverse search, through the bash history, simply type characters that should be unique to the command you want to find in the history.
- Ctrl+S – launches a forward search, through the bash history.
- Ctrl+G – quits reverse or forward search, through the bash history.
All are valid (first is most common) src:
function_name () {
commands
}
function_name () { commands; }
function function_name {
commands
}
function function_name { commands; }
#!/bin/bash
T1="foo"
T2="bar"
if [ "$T1" = "$T2" ]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
- In bash, you should do your check in arithmetic context:
if (( a > b )); then ... fi
- For POSIX shells that don't support (()), you can use -lt and -gt:
if [ "$a" -gt "$b" ]; then ... fi
src
- Put this at the beginning of the script:
#!/bin/bash
- Example:
#!/bin/bash
# global variables
var1='A'
var2='B'
my_function () {
local var1='C'
var2='D'
echo "Inside function: var1: $var1, var2: $var2"
}
- referenced in order as
$1
,$2
, etc. $0
= function name$#
= number of passed arguments$*
and$@
both hold all arguments but behave different when double quoted:- When double-quoted,
"$*"
expands to a single string separated by space -->"$1 $2 $n"
. - When double-quoted,
"$@"
expands to separate strings -->"$1" "$2" "$n"
- Slicing arguments:
echo $@; #"p1" "p2" "p3" "p4" "p5"
echo ${@: 0}; #"bash" "p1" "p2" "p3" "p4" "p5"
echo ${@: 1}; #"p1" "p2" "p3" "p4" "p5"
echo ${@: 2}; #"p2" "p3" "p4" "p5"
echo ${@: 2:1}; #"p2"
echo ${@: 2:2}; #"p2" "p3"
echo ${@: -2}; #"p4" "p5"
echo ${@: -2:1}; #"p4"
Example:
#!/bin/bash
echo -n "Enter the name of a country: "
read COUNTRY
echo -n "The official language of $COUNTRY is "
case $COUNTRY in
Lithuania)
echo -n "Lithuanian"
;;
Romania | Moldova)
echo -n "Romanian"
;;
Italy | "San Marino" | Switzerland | "Vatican City")
echo -n "Italian"
;;
*)
echo -n "unknown"
;;
esac
Example:
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
or: for i in 1 2 3 4 5; do echo $i ; done
- Mounting Drives
sudo mkdir /mnt/x
sudo mount -t drvfs '\\network\path' /mnt/x
-
Un-mount drive:
sudo umount /mnt/x
src -
Mount a Drive Until Logoff:
- Create a new folder for that drive letter under /mnt if it does not already exist. (ex: mkdir /mnt/m)
- Mount the drive with
sudo mount -t drvfs M: /mnt/m
-
Mount Drives in a Persistent Manner (src)
- Ensure the folder exists for the mount target (e.g.
/mnt/m
) - Open /etc/fstab and add a line such as the following:
- M: /mnt/m drvfs defaults 0 0
- Reload the fstab file with sudo mount -a
- Ensure the folder exists for the mount target (e.g.
-
Using
start
in WSL:alias start='cmd.exe /C start'
-
Check WSL version (in powershell):
wsl -l -v
(src) -
Change WSL version:
wsl --set-version Ubuntu 2
(src)
- Find
etc
directory for Windows Git Bash:c/Users/<username>/AppData/Local/Programs/Git/etc/
- Pipe output to clipboard from WSL:
<command> | clip.exe
- install AutoHotKey
- create an
.ahk
script - add
CapsLock::Esc
to the script - run the script to enable it
- to run it at startup, add it to
C:\Users\<UserName>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
- you can open that folder by doing
win + r
and runningshell:startup
source
-
Hard to implement Linux
xcape
solutions on WSL because it requiresX
, which is a window thing that probably won't run on there (I think) that's what this guy talks about anyway; I haven't verified that WSL doesn't actually work -
This guy made a tool for Linux using C (hasn't been maintained for a long time).
-
This guy made "Interception" for Windows but it's old (5 yrs)
-
Add AutoHotKey to capslock: https://gist.github.com/sedm0784/4443120, https://github.com/ililim/dual-key-remap