Study

Install Windows Subsystem for Linux (WSL) and Learn these first basic Linux commands


Table of Contents:

Resources used:

Recommended resources after these notes:

WSL Tutorial

(Friday, April 29, 2022)

WSL Installation

In order to install WSL (Windows Subsystem for Linux), we need to follow these steps:

Note: If nothing happens when running wsl --install, then it is possible that you have an older verion of WSL installed (that could cause some problems when downloading and trying to install Ubuntu-22.04 in the next steps). Try to run wsl --update instead, then restart PC.

Congratulations, you officially have GNU/Linux Ubuntu 22.04 (based on Debian Distribution) installed on your system!

You can run lsb_release -a command on your Ubuntu Shell… and you can also install any package like neofetch (sudo apt install neofetch) - note that neofetch will download A LOT of additional packages automatically (like bzip2 fonts ghostscript jp2a etc… don’t know any of these either).

You can run htop as well:

Or, we can run other “system information” related commands, like:

Display related and other devices/drivers:


Now, if you just close the Ubuntu Terminal, the container for Ubuntu subsystem will still run in background (you can check again by running in wsl -l -v in a PowerShell window).

From wsl --help, we can see the following commands:

So, we can run our Ubuntu 22.04 subsystem from PowerShell by running wsl command (instead of opening it from Start menu).


Note that the current path in Ubuntu is /mnt/c/Users/YOUR_USERNAME/, so our main C: (system) partition is mounted as a drive in Linux… so you can theoretically access/remove/modify all Windows System Files ⚠⚠⚠.


Note, that even if Ubuntu (or any other Linux) is in “Stopped” state (eg. after wsl --shutdown), you can still run any linux command inside that Linux subsystem by writing wsl <Linux command> in PowerShell, and the output will be printed in PowerShell, and Ubuntu/Linux subsystem will still remain offline after 🟠 (so any wsl <command> from PowerShell will start/boot the Linux Subsystem Container, run the command, then shuts it down if it was in “Stopped” state). You will often see commands like wsl docker when working with Docker or any other Linux applications on Windows that runs with the help of WSL.


Run Python from Linux Subsystem

It’s kind of amazing how fast we can transition from Powershell to Linux Subsystem terminal. For example, even if our Ubuntu subsystem is in “stopped” state, we can run wsl python3 directly from PowerShell, and, we are actually running Python inside Linux in “PowerShell” (that’s actually Ubuntu’s Terminal). And, if we open another Ubuntu Terminal and run htop, we can see that our Ubuntu subsystem contains our previous Python “Task”.


Compile C/C++ from Linux Subsystem

This could be also really useful if we want to compile .c or .cpp (C/C++) programs directly from PowerShell, by using our Linux Subsystem! Let’s try it, by running the following from Ubuntu terminal:

apt show gcc
sudo apt install gcc
gcc --version

Since we are on Ubuntu (Debian based), we can install the GNU C Compiler (GCC) using Advanced Packaging Tool (APT).

Create a new file using Nano editor:

nano hello.c
#include <stdio.h>

int main() {
    printf("Hello there\n");
    return 0;
}

Press CTRL+O (Nano editor for “Write Out”, meaning “Save”), CTRL+X to exit Nano editor. Run:

gcc -o hello hello.c
./hello

We should see our “Hello there” output. We can also find the created files in Windows File Explorer:


Nyancat

And, we can also run fun things like…

sudo apt install nyancat -y
nyancat


Or… sudo apt install cowsay


Other fun commands:


Command Line Crash Course

Notes taken from:

Intro to Bash Command Line

Bash Shortcuts

Most used shortcuts in bash (More here):

Bash Control/Processes


Bash Manual

Each argument given to man command is normaly the name of the program, utility or function, then the information/documentation about that is displayed.

man bash
man <command>
man git
man gcc
man python3

(Monday, May 02, 2022)


Very basic Bash commands

Creating files and directories

Directories/Folders:

mkdir myfolder
rmdir myfolder
rm -r myfolder
mkdir -p mydir/mysubdir
cd mydir/mysubdir



Files:

Create files with touch - Note that touch is mainly used to alter the “modified” timestamp of a file:

touch myfile.txt

# You can create multiple files
nano file1.txt markdownFile.md script.py

You can create and edit files with nano editor (after entering nano editor, you will have multiple options like “save” CTRL+O, or “exit” CTRL+X):

nano hello.py

You can also create a file with content already in it by using echo "your_string" and direct the content to the file using > operator:

echo "This is my text file" > myfile.txt
echo "print("hi there")" > hi.py
python3 hi.py

You can also add content to the bottom of a file that already has content in it (append more content to end of file) by using >> operator. Note: you can also view the content of a file using cat file.txt

echo "My first text line" > myfile.txt
echo "My second text line on the same file" >> myfile.txt
cat myfile.txt

# will output
# My first text line
# My second text line on the same file

Both > and >> operators create a file if the mentioned file is not existent in current folder (https://linuxhint.com/difference-arrow-double-arrow-bash/), and:


Note: You can see the content of a file by using cat. Or, you can see the first 10 lines of a files by running head command (head myfile.txt), and if you want to see the last 10 lines from a file, run tail (tail myfile.txt). There are also more and less commands in order to see the content of a file “page by page” (more myfile.txt) or line by line (less myfile.txt).



You can also use other text editor like gedit, or vim (that is almost used as an IDE) - you can run vimtutor command for a complete vim editor walkthrough.

vim myfile.txt

Note: We can also use vim as a “file explorer” by running vim . (it is also exited with :q!)


Others: If you want to see more information about a file (that is not necessarily a text file), you can run the file command. For example, viewing more details about a picture/image/photo:

file foggyPhoto.jpg

# will return
# foggyPhoto.jpg: JPEG image data, JFIF standard 1.01, resolution (DPI), density 72x72, segment length 16, progressive, precision 8, 5472x3648, components 3

Or, just for image files, you can also use identify (man identiyf) command (more on this here):

identify foggyPhoto.jpg
foggyPhoto.jpg JPEG 5472x3648 5472x3648+0+0 8-bit sRGB 1.07214MiB 0.000u 0:00.006


To list/view all the files within a folder, eg use flags to see detailed view (man ls):

ls
ls -lah

ls flags:

For example, to sort files by size within a folder, run ls --sort=size -lah.



To navigate to a folder within current path (ls), use “change directory” with cd. To go to the previous folder use cd .., to go to previous previous directory cd ../.. (go 2 levels back) and so on.

cd mydir/myotherdir
cd ..
cd ../..



To find files in the entire system (/ - the root directory) or in current path and folders inside (.), we can use find (man find) - it will output the path(s) to the searched file:

find .
find / -name host.conf
find . -name docker-compose.yml


To search for strings inside files (and output their path), use ack (man ack), is just as powerful, but easier as grep. Note, it is possible that ack needs to be installed (sudo apt install ack for Debian based distros).

ack 'stringpattern'

# or grep equivalent
grep -rni '/path/to/somewhere/' -e 'stringpattern'
# -r or -R is recursive
# -n is to show line number in file
# -w stands for match the whole word
# -l (lower-case L) can be added to just give the file name of matching files (show the file name, not the result itself)
# -e is the pattern used during the search
# -i for ignore case

https://stackoverflow.com/questions/16956810/how-do-i-find-all-files-containing-specific-text-on-linux


Manipulating files

As we saw, with touch (man touch for manual) we can manipulate the “modified” timestamp of files. Here we can mention the timestamp in any way we want, even as “2 hours ago” (with -d flag).

touch -d "09:00" myfile.txt
touch -d "2 hours ago" myfile.txt
touch -d "2022-02-24 13:23:40" myfile.txt
touch -d "next Wednesday" myfile.txt
touch -d "last Monday" myfile.txt
touch -d "last Thursday 16:21:32" myfile.txt
touch -d "Sun, 29 Feb 2020 16:21:42" myfile.txt
touch -d '1 June 2018 11:02' file1

# you can also touch multiple files
touch myfile1.txt myfile2.txt myfile3.txt



Remove/Delete files with rm (man rm).

rm myfile.txt
rm -r myDirContainingFiles

You can also check these articles:



Move a file to another location (man mv, mv /path/to/sourcefile /path/to/targetlocationfile)

mv hi.py ../hi.py  # move one folder up
mv ../hi.py ./hi.py  # move file from one folder up to current folder/path



Changing filenames (renaming files) are also done with mv

mv currentFileName.txt newFileName.txt
mv hi.py hello.py



Copy files (either in same location, or other locations, with/withourt different names, etc)

cp file_2.txt file_3.txt
cp hello.py ../hello2.py

# If folder does not exist
cp hello.py /v2/hello.py  # cp: cannot create regular file '/v2/hello2.py': No such file or directory
mkdir -p ./v2 && cp hello.py $_

https://stackoverflow.com/questions/1529946/linux-copy-and-create-destination-dir-if-it-does-not-exist


Also, cp is often used for backups

cp file_2.txt{,.bkp}

cp hello.py{,.bkp}
cp hello.py hello.py.bkp #  the equivalent command


Networking. Connecting to a server from a Linux PC

(min22:20 - Command Line Crash Course)

Tunnel/Connect to a server terminal with ssh

ssh username@ipv4_ip_addres

# example
ssh root@127.54.43.98

If we connect to a session, then exit that session, we can use tmux to reconnect back to that session without needing to run again ssh username@ipv4_ip_addres command.

tmux  # same as tmux attach, attach to last session
exit  # exits current session opened with tmux (detach session), same as CTRL+B, D

You can select from multiple sessions with tmux ls, then attach to one from list by its index

tmux ls
tmux attach -t 0

To close/kill all tmux session use either:

tmux kill-server
# or
pkill -f tmux


Other related networking commands:

Note: ifconfig needs to be installed with sudo apt install net-tools. In Windows OS, we have ipconfig equivalent command.


Process Management in brief

Get the pid (process id) of a process/application/task

pidof program

# example
pidof chrome
pidof firefox

Example kill program with pid of 9378 (assuming this pid was found with top or pidof command)

kill 9378

Kill a program (all programs) by its name(s) (man pkill)

pkill program

# example
pkill chrome

Get a list of all running programs/tasks

top

Get a list of all running programs/tasks (with a nice interface)

htop