Install Windows Subsystem for Linux (WSL) and Learn these first basic Linux commands
Table of Contents:
Resources used:
Recommended resources after these notes:
(Friday, April 29, 2022)
In order to install WSL (Windows Subsystem for Linux), we need to follow these steps:
Virtual Machine Platform
and Windows Subsystem for Linux
. Click OK, wait for features to install, then restart your Windows PC.wsl --install
. After installation, restart PC again.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.
wsl --set-default-version 2
Complete Ubuntu Installation Process in the new window (only using keyboard) - also press ENTER after the “mounting options” screen. If “Provide a new UNIX username” (that only contains lowercase letters, underscore _
, and dash -
) is asked, just press CTRL+C (the user was already created when you provided an username and a pass).
You can now run Ubuntu 22.04 LTS directly from start -> Run “Ubuntu 22.04 LTS”
In a separate PowerShell, check for installed Linux subsystems by running wsl --list --verbose
(or wls -l -v
for short). You should see “Ubuntu-22.04” in RUNNING state with WSL Version 2.
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:
uptime
free
- current memory usage (just like top
/htop
)ps
- show current Linux Processes (ps -A
to show all the processes)df -h
- show file system disk space usagelsblk
- list block devicesDisplay related and other devices/drivers:
lspci
- list all PCI devices (PCI buses) and driversnvidia-smi
- provies information about NVIDIA GPUs and driverslshw
- list all hardware information on machine (use lshw -C display
for display/GPU related hardware information)nvtop
- show GPU related processes, just like htop
but for discrete graphics (eg. NVIDIA) (sudo apt install nvtop
)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:
wsl --shutdown
- terminates/shutdowns all distributions in “running” state -> This will put our Ubuntu 22.04 in “Stopped” state.wsl --set-default Ubuntu-22.04
- sets our Ubuntu-22.04 distribution as default, namely, if you have multiple other distributions, “Ubuntu-22.04” will start if you just type/run wsl
command directly from PoweShell.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.
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”.
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:
And, we can also run fun things like…
sudo apt install nyancat -y
nyancat
Or… sudo apt install cowsay
Other fun commands:
Notes taken from:
Bash Shortcuts
Most used shortcuts in bash (More here):
CTRL+R
- search through commands history - based on your before-used commands, you can re-run a command by searching part of it (history
)CTRL+A
/ CTRL+E
- while you write a command, CTRL+A
will move your cursor to the start of the command line, CTRL+E
will move your cursor to the endCtrl+U
- Deletes before the cursor until the start of the commandCTRL+L
- clears the command line (clear
)Ctrl+D
- Closes the current terminalBash Control/Processes
Ctrl+S
- Stops command output to the screenCtrl+C
- Sends SIGI signal and kills currently executing commandCtrl+Z
- Suspends current command execution and moves it to the backgroundCtrl+Q
- Resumes suspended commandBash 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)
Directories/Folders:
mkdir myfolder
rmdir: failed to remove 'ubuntu/': Directory not empty
):rmdir myfolder
rm
with caution! ⚠⚠⚠) - -r
stands for recursive
, remove directories and their contents recursively
rm -r myfolder
-p, --parents - make parent directories as needed
):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:
>
output operator overwrites anything in a file>>
appends to the existing fileNote: 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:
-a, --all
- do not ignore entries starting with . (hidden files)-l
- use long listing format (display as list with 1 file and its details per row)-h
- human-readable, print sizes like 1K, 234M, 2G, etc-d, --directory
- list only directories-c
- used with -lt
, sort by ctime (time of last modification)-C
- list entries by columns--sort
- sort by WORD instead of name, eg size (-S)
, time (-t)
, version (-v)
, extension (-X)
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
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 -r
– delete files recursively from all directories and sub-directoriesrm -f
– delete files on the path provided without prompting the user. This also includes deleting the “Read Only Files”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
(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:
ifconfig
- view the configuration of network interface (more about here). Also used for network configuration.Note: ifconfig
needs to be installed with sudo apt install net-tools
.
In Windows OS, we have ipconfig
equivalent command.
ip
- newer command for ifconfig
(ip
command requests must have an “object” parameter, eg. ip a
)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