- Basic Scripting
- This folder is not located in drive D, but it's showing
- How can I tell which version of Ubuntu I'm running
- Edit .bashrc or any other files in linux
- How do I pause/resume download and install from terminal?
- Modify Your Hosts File
- How can I get octal file permissions from command line
- How to clean up unnecessary files
- What is the correct way to completely remove an application?
- Changing the home directory of user on Windows Subsystem for Linux
- How to open windows explorer from current working directory of WSL shell?
- How do I hide the “user@hostname” info
- zsh compinit: insecure directories and files, run compaudit for list.
- Folder permission "Insecure completion-dependent directories detected
- Timing All ZSH Plugins Programmatically
- WSL2 DNS stops working
- WSL2 automatically release disk space back to the host OS
#! /bin/bash
Echo command:
echo Hello World!
Variables:
# Uppercase by convention
# Letters, numbers, underscores
NAME="Bob"
echo "My name is $NAME"
# echo "My name is ${NAME}"
User input:
read -p "Enter your name: " NAME
echo "Hello $NAME, nice to meet you!"
if statement:
if [ "$NAME" == "Genesis" ]
then
echo "Your name is Genesis"
fi
if-else:
if [ "$NAME" == "Genesis" ]
then
echo "Your name is Genesis"
else
echo "Your name is NOT Genesis"
fi
else-if (elif):
if [ "$NAME" == "Genesis" ]
then
echo "Your name is Genesis"
elif [ "$NAME" == "David" ]
then
echo "Your name is David"
else
echo "Your name is NOT Genesis or David"
fi
Comparisons:
NUM1=31
NUM2=5
if [ "$NUM1" -gt "$NUM2" ]
then
echo "$NUM1 is greater than $NUM2"
else
echo "$NUM1 is less than $NUM2"
fi
########
# val1 -eq val2 Returns true if the values are equal
# val1 -ne val2 Returns true if the values are not equal
# val1 -gt val2 Returns true if val1 is greater than val2
# val1 -ge val2 Returns true if val1 is greater than or equal to val2
# val1 -lt val2 Returns true if val1 is less than val2
# val1 -le val2 Returns true if val1 is less than or equal to val2
########
File conditions:
FILE="test.txt"
if [ -e "$FILE" ]
then
echo "$FILE exists"
else
echo "$FILE does NOT exist"
fi
########
# -d file True if the file is a directory
# -e file True if the file exists (note that this is not particularly portable, thus -f is generally used)
# -f file True if the provided string is a file
# -g file True if the group id is set on a file
# -r file True if the file is readable
# -s file True if the file has a non-zero size
# -u True if the user id is set on a file
# -w True if the file is writable
# -x True if the file is an executable
########
Case statement:
read -p "Are you 21 or over? Y/N " ANSWER
case "$ANSWER" in
[yY] | [yY][eE][sS])
echo "You can have a beer :)"
;;
[nN] | [nN][oO])
echo "Sorry, no drinking"
;;
*)
echo "Please enter y/yes or n/no"
;;
esac
For loop:
NAMES="Genesis Moises David Jesh"
for NAME in $NAMES
do
echo "Hello $NAME"
done
For loop to rename files:
FILES=$(ls *.txt)
NEW="new"
for FILE in $FILES
do
echo "Renaming $FILE to new-$FILE"
mv $FILE $NEW-$FILE
done
While loop - read through a file line by line:
LINE=1
while read -r CURRENT_LINE
do
echo "$LINE: $CURRENT_LINE"
((LINE++))
done < "./new-1.txt"
Function:
function sayHello() {
echo "Hello World"
}
sayHello
Function with params:
function greet() {
echo "Hello, I am $1 and I am $2"
}
greet "Genesis" "25"
Create folder and write to a file:
mkdir hello
touch "hello/world.txt"
echo "Hello World" >> "hello/world.txt"
echo "Created hello/world.txt"
$ rd /s "\\?\D:\bad\folder\path "
$ lsb_release -a
$ lsb_release -sd # determine what version of Ubuntu you're running
$ lsb_release -sc # codename of your distribution
sudo do-release-upgrade # upgrade a full Ubuntu environment to a new release
$ vim ~/.bashrc
# alias winhome='cd /mnt/c/Users/<USERNAME>/'
$ source ~/.bashrc # source the file for those changes to take effect
winhome # will go to the set folder directly
Ctrl
+c
cancels it but next time start from where you left Ctrl
+z
stops process but then you can't do another process as it remains locked to the first process
Using one of the above methods is generally better than just closing the terminal, but if you just close the terminal while it's downloading packages, it should start the download right where it stopped next time you run sudo apt-get upgrade
If you want to restart the download after using Ctrl
+z
:
- Check paused tasks by typing
jobs
in the terminal - To resume a process, type
fg
- If you have multiple tasks, then type
fg 1
,fg 2
, etc…
- Open a terminal window.
- Open the hosts file in a text editor (you can use any text editor) by typing the following line:
sudo vim /etc/hosts
- Enter your domain user password.
- Make the necessary changes to the file.
- Press Control-x.
- When asked if you want to save your changes, answer y.
$ stat -c "%a %n" *
Replace *
with the relevant directory or the exact filename that you want to examine.
-c --format=FORMAT
use the specified FORMAT instead of the default; output a newline after
each use of FORMAT
%a Access rights in octal
%n File name
Usage:
# with files
$ stat -c "%a %n" ./Documents/Udev.html
664 ./Documents/Udev.html
# with folders
$ stat -c "%a %n" ./Documents/
755 ./Documents/
To clean some packages laying around, run the following:
$ sudo apt-get autoremove
$ sudo apt-get clean
$ sudo apt-get autoclean
It will purge required packages along with dependencies that are installed with those packages. The --auto-remove
option (being an alias of autoremove
) works similar to sudo apt-get autoremove
. By using this command we can run a single command:
$ sudo apt-get purge --auto-remove packagename
# instead of
$ sudo apt-get purge packagename
$ sudo apt-get autoremove
-
Enter bash
-
Type the command
sudo vim /etc/passwd
-
Find your account's line, which might look like:
<USERNAME>:x:1000:1000:"",,,:/home/<USERNAME>:/bin/bash
-
Change the home directory, which above is
/home/<USERNAME>
, to the new directory, using WSL notation -
Save the file
-
Exit bash and re-launch it
-
To test, use the commands:
$ cd ~ $ pwd
$ explorer.exe .
Set alias with .bashrc
for a custom command:
$ echo 'alias explorer="explorer.exe ."' >> ~/.bashrc
$ source ~/.bashrc
To open the current working directory in Windows Explorer, you can just use:
$ explorer
Open your .zshrc and paste this at the bottom:
prompt_context() {
if [[ "$USER" != "$DEFAULT_USER" || -n "$SSH_CLIENT" ]]; then
prompt_segment black default "%(!.%{%F{yellow}%}.)$USER"
fi
}
This makes only your username to appear. If you don't want that too, just comment out this line prompt_segment black default "%(!.%{%F{yellow}%}.)$USER"
- run
compaudit
and it will give you a list of directories it thinks are unsecure - run
sudo chown -R username:root target_directory
- run
sudo chmod -R 755 target_directory
Set ZSH_DISABLE_COMPFIX=true
in your zshrc file, before oh-my-zsh.sh is sourced, and update OMZ to the latest version. It should ignore these permission issues and load the completion system normally.
Update ~/.oh-my-zsh/oh-my-zsh.sh
# Load all of the plugins that were defined in ~/.zshrc
for plugin ($plugins); do
timer=$(($(python3 -c 'from time import time; print (int(round(time() * 1000)))')))
#timer=$(($(python -c 'from time import time; print int(round(time() * 1000))')))
if [ -f $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh ]; then
source $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh
elif [ -f $ZSH/plugins/$plugin/$plugin.plugin.zsh ]; then
source $ZSH/plugins/$plugin/$plugin.plugin.zsh
fi
now=$(($(python3 -c 'from time import time; print (int(round(time() * 1000)))')))
#now=$(($(python -c 'from time import time; print int(round(time() * 1000))')))
elapsed=$(($now-$timer))
echo $elapsed":" $plugin
done
- Create a file:
/etc/wsl.conf
- Put the following lines in the file:
[network]
generateResolvConf = false
- In a
cmd
window, runwsl --shutdown
- Restart WSL2
- Create a file:
/etc/resolv.conf
. If it exists, replace existing one with this new file. - Put the following lines in the file
nameserver 8.8.8.8
- Repeat step 3 and 4. You will see
git
working fine now.
### Optimize (shrink) WSL 2 .vhdx
## Must be run in PowerShell as Administrator user
# DistroFolder found at: $env:LOCALAPPDATA\Packages\
# Examples:
# CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc
# CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc
cd $env:LOCALAPPDATA\Packages\REPLACE_ME_WITH_TARGET_DISTRO_FOLDERNAME\LocalState\
wsl --shutdown
optimize-vhd -Path .\ext4.vhdx -Mode full
#Run `wsl` or your favorite terminal to resume use
wsl --shutdown
diskpart
# open window Diskpart
select vdisk file="C:\WSL-Distros\…\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit