🌱 Bash-Oneliner
A collection of handy Bash One-Liners and terminal tricks for data processing and Linux system maintenance.
Things I use
Ctrl + r begins a backward search through command history.(keep pressing Ctrl + r to move backward)
Ctrl + a move to the beginning of line.
Ctrl + e move to the end of line.
Ctrl + k delete all text from the cursor to the end of line.
!! run previous command
sudo !! run previous command as sudo
chmod +x filename make file executable
cal show a calendar
ctrl+c delete current bash command
clear clean screen
tree
Things I want to use
!53 Run history number (e.g. 53)
# foo=bar
echo $foo
# bar
echo "$foo"
# bar
# single quotes cause variables to not be expanded
echo '$foo'
# $foo
# single quotes within double quotes will not cancel expansion and will be part of the output
echo "'$foo'"
# 'bar'
# doubled single quotes act as double quotes making variables expand
echo ''$foo''
# bar
gfactor 50 will give the prime factors
grep -c "^$" count empty lines
grep -c bbo filename Grep and return number of matching line(e.g. ‘bbo’)
grep --color bbo filename add color to the match
grep -R bbo /path/to/directory search all files in a directory
grep -rl bbo /path/to/directory Search all files in directory, output ONLY the filenames with matches
grep -f fileA fileB Grep all content of a fileA from fileB
grep $'\t' grep a tab
sed 1d filename remove the 1st line
sed 1,100d filename Remove the first 100 lines
sed '$d' Delete/remove last line
sed '/^$/d' delete/remove empty lines
sed -s '$a,' *.json > all.json Concatenate/combine/join files with a separator and next line
ls|xargs wc -l Count lines in all file, also count total lines
find mso*/ -name M* -printf "%f\n" Find and output only filename (e.g. “mso”)
find / -type f -size +4G Find large files in the system (e.g. >4G)
find . -type f | wc -l Recursively count all the files in a directory
stat filename.txt Display file status
alias print all alias
look phy Print some words that start with a particular string (e.g. words start with ‘phy’)
printf 'hello world\n%.0s' {1..5} Repeat printing string n times
seq 10 Generate sequence 1-10
rev reverse string
echo {1,2}{1,2} generate combinations
Generate all combination (e.g. A,T,C,G)
#!/bin/bash
set={A,T,G,C}
group=5
for((i=0;i<$group;i++));do
repetition=$set$repetition;done
bash -c "echo "$repetition""
split -d -l 1000 largefile.txt Split by line (e.g. 1000 lines/smallfile)
rename s/$/.txt/ * Add file extension to all file(e.g add .txt)
cat -v filename Show non-printing (Ctrl) characters with cat
identify myimage.png describe the format of an image
send mail
echo 'heres the content'| mail -a /path/to/attach_file.txt -s 'mail.subject' me@gmail.com
# use -a flag to set send from (-a "From: some@mail.tld")
speaker-test -t sine -f 1000 -l1 make a beep
cd tmp/ && tar xvf ~/a.tar Run command only if another command returns zero exit status (well done)
cd tmp/a/b/c ||mkdir -p tmp/a/b/c Run command only if another command returns non-zero exit status (not finish)
echo $? check the last exit code
ls -d */ list only directories