Shell general commands

Create multiple folders

mkdir Folder_{00,01,02,03} 
mkdir Folder_{01..10}

Commands history

!! #re-execute last cmd
! CMD-No # exe cmd
fc -s CMD-NO # exe cmd
cat FILE; vim !$ #vim edit last display

Join multi files

join FILE1 FILE2 | join - FILE3 | join -v1 - FILE4 | join -a2 - FILE5

Join with tab as separator

join -t $'\t' FILE1 FILE2

Command line text handling

CTL + U # erase the whole line
CTL + W # erase one word in the current line
CTL + L # clear screen

Renaming

mv FILE1 FILE2 # rename file1 to file2
for i in *.txt; do mv "$i" "${i/[0-9]*-FILE.md.txt/.txt}"

Cat

cat -n FILE1 FILE2 # number each line
cat -b # number line except blank lines
cat -s # replace 2 continuing blank lines to 2

Product sequential ids

seq 30000 33480 | awk '{print "Smp_" $1 "0"}'

Create a link

ln -s <source file> <symbolic file>

Sort: first on column 1 and then on column2 (numbers)

sort -k1,1 -k2,2n

Sort: on tab separated file

sort -t $'\t' FILE

Sequential numbers

for i in $(seq 1 3) $(seq 5 9) 25; do echo $i; done # 1 2 3 5 6 7 8 9 25

Cat too many files with the error: argument too long

cd file_directory
ls | xargs -n 32 -P 8 cat >>../outfile

To execute commands in a file

sh -e FILE.txt

To replace whitespace with tab (sed not working)

tr ' ' '\t'

Append file name to each line

awk '{print $0, FILENAME}' INPUT_FILE

Piping and Chaining

grep 'TEXT' file.txt | sort 
./cmd.sh && head outfile.txt # '&&' wait for command to finish then next

Show all files

ls -alhG
# ./bash_profile: alias ls="ls -alhG"

Switch directories

cd - # go back to last dir
pushd /newdir # go to new dir and remember current dir
popd # go to the remembered dir

Command history

HISTTIMEFORMAT="%Y-%m-%d %T " # add date and time to command history; add to .bashrc to record 
CTL + R # search command

Empty a file

truncate -s 0 FILE

Suspend a file

CTL + Z # send to background
fg # bring to forground
comments powered by Disqus
CC-BY-NC 4.0
Built with Hugo Theme Stack