Skip to content

Latest commit

 

History

History

cli_tools

Examples of common commands

wget

To download this repository

wget https://github.com/eliranwong/wsl2/archive/master.zip

Setup mlocate

To install:

sudo apt install mlocate

To create database:

sudo /etc/cron.daily/mlocate

To manually update the database:

sudo updatedb

To run:

locate

or

mlocate

Common tasks of text processing

To split a single text file into multiple ones, with maximum of 500 lines each, e.g.:

split -d -l 500 [filename] [optional_prefix_name]

To sort TAB-delimited text file by first 3 columns:

sort -b -n -t "[TAB HERE]" -k 1,3 test.txt > test2.txt

To add line numbers to text file, starting from number 1:

nl -nln -v 1 -s "[TAB HERE]" test.txt > test2.txt
[Remarks: -v 1 -s "[TAB HERE]" is implemented by default, could be omitted.]

To change file extensions of multiple files:

for file in *.html; do mv "$file" "${file%.html}.txt"; done

To add an empty line at the end of a file:

echo "" >> test.txt

To add an empty line in multiple files:

for file in *.txt; do echo "" >> $file; done

To combine multiple files in a single file:

cat *.txt > combined.txt

Tutorial on string manipulation:
https://www.tldp.org/LDP/abs/html/string-manipulation.html

More at: http://www.tldp.org/LDP/abs/html/

Search & Replace

  • Simple tasks with sed:

sed -E -i.bak 's/[search]/[replace]/g' [file]

e.g. an example of back references:

echo "test testing" | sed -E 's/(t)(e)s\1/\2/g'

e.g. executing multiple commands

echo "test testing" | sed -E -e 's/(t)(e)s\1/\2/g;s/i/I/g'

sed -E -i.bak -f myScript.sed inputFile.txt

e.g. remove file extension:

echo "test.txt.html" | sed s/\.[^\\.]*$//

e.g. open Thunar from Windows Explorer:

https://github.com/eliranwong/wsl2/blob/master/file_manager/thunar.md#launch-thunar-from-windows-context-menu

  • Simple tasks with awk / gawk:

echo "My_name_is_Wong" | awk -F '_' '{surname=$4; $4="Eliran"; print $4 " " surname; print $0 " " surname}'

echo "My_name_is_Wong" | awk -F '_' '{gsub($4,"Eliran_Wong",$0); print$0}'

e.g. append text before file extension

echo "test.text.txt.html" | awk -F '.' '{gsub($(NF-1), $(NF-1)"_new", $0); print$0}'

e.g. open Windows Explorer from Thunar:

https://github.com/eliranwong/wsl2/blob/master/file_manager/thunar.md#by-right-clicking-a-folder

Convert an image file to html

echo '<img src="data:image/png;base64,'$(base64 test.png)'">' > test.html

A Search & Replace Utility, written with python:

https://github.com/eliranwong/bible-verse-parser/blob/master/RegexSearch.py

Read usage & examples at:

https://github.com/eliranwong/wsl2/blob/master/cli_tools/resr.md

rar & unrar

To list files in a rar file, e.g. file.rar:

unrar l file.rar

To extract a rar file, e.g. file.rar:

unrar e file.rar

To pack a folder, e.g. myFolder, into a rar file, e.g. file.rar:

rar a file.rar myFolder

sqlite3 Import / Export

Import a TAB-delimited text file into a table:
sqlite3 FILE.sqlite

.separator "\t"
.import DATA.csv TABLENAME
VACUUM;
.quit

To export without header:

sqlite3 -separator "[TAB HERE]" FILE.sqlite "SELECT * FROM TABLENAME;" > output_filename.csv

To export with header:

sqlite3 -header -separator "[TAB HERE]" FILE.sqlite "SELECT * FROM TABLENAME;" > output_filename.csv

To delete all records in a table:

sqlite3 FILE.sqlite "DELETE FROM TABLENAME; VACUUM;"

To delete a table:

sqlite3 Bible_Promises.book "DROP TABLE TABLENAME;"

Remarks: To enter a TAB character on terminal:
Ctrl + v + TAB

Conversion between Traditional and Simplified Chinese

Convert traditional Chinese to simplified Chinese:

opencc -i [inputFile] -o [outputFile] -c t2s.json

Convert simplified Chinese to traditional Chinese:

opencc -i [inputFile] -o [outputFile] -c s2t.json

Download Video / Audio from YouTube

https://github.com/eliranwong/wsl2/blob/master/multimedia/youtube-dl.md

Examples of ffmpeg

https://github.com/eliranwong/wsl2/blob/master/multimedia/ffmpeg.md

Working on Xresources

To add / update configurations:
nano ~/.Xresources
[Edit ~/.Xresources]
xrdb -merge ~/.Xresources

To replace / remove configurations:
xrdb -query -all > ~/.Xresources
[Edit ~/.Xresources]
xrdb ~/.Xresources

Information

  • check cpu

lscpu

  • check storage

df -h

  • check size of current directory

du -sh

  • check memory

free -h

  • Display File(s) in Tree

For example:
tree -L 1 /

Force quit an app

Make use of "aux ps" to locate a process and "kill" to quit.

Alternatively, use "pkill"

Install & Uninstall .deb package

To Install a downloaded .deb file:
sudo dpkg -i packagename.deb
OR
sudo apt install ./packagename.deb
OR
Use chrome os "Files" app, right-click a .deb file and select "Install with Linux"

To Remove an installed package:
sudo dpkg -r packagename

Alternatively, open chrome os "Files" app (Alt+Shift+m), right click a *.deb file and select "Install with Linux"

Compile from source, an example:

tar xvfz [package].tar.gz
cd [unpacked folder]
./configure
make
make install

Trouble-shoot error on loading file(s):

  1. Use "mlocate" to find full path of the missing file:
    mlocate [missing file]

  2. Use "dpkg -S" to find the package(s) owning file(s):
    dpkg -S [full file path]

  3. re-install the package, indicated in step in the result of step (2)

For example, https://community.rstudio.com/t/installation-error-cannot-find-libsmime3-so/30646

Upgrade

Use "sudo apt dist-upgrade" instead of "sudo apt upgrade"

Clean up storage

sudo apt clean
sudo apt autoremove --purge

Launch a GUI app through Terminal WITHOUT Blocking the Terminal

e.g. To launch leafpad through terminal:

geany &
[Closing the terminal closes leafpad too.]

OR

geany & disown
[Leafpad keeps running even the terminal is closed.]

vi editor

ZZ - Save and exit
:q! - discard all changes, since the last save, and exit
:w - save file but don't exit
:wq - again, save and exit

[ryanstutorials cheat sheet]

Navigation with "man" or "less"

SPACE = next page
b = previous page
q = quit