diff --git a/assignment1/q1/1.md b/assignment1/q1/1.md new file mode 100644 index 0000000..96a20ef --- /dev/null +++ b/assignment1/q1/1.md @@ -0,0 +1,100 @@ +# Bandit wargame +Personal refernce- Read about the new commands again +## Level 0: +`ls` +`cat readme` + +Password: ZjLjTmM6FvvyRnrb2rfNWOZOTa6ip5If + +## Level 1: +`ls` +`cat ./-` + +Password: 263JGJPfgU6LtdEvgfWU1XP5yac29mFx + +## Level 2: +`ls -l` +`cat "spaces in this filename"` + +Password: MNk8KNH3Usiio41PRUEoDFPqfxLPlSmx + +## Level 3: +`ls` +`cd inhere` +`ls -la` +`cat ...Hding-From-You` + +Password: 2WmrDFRmJIq3IPxneAaMGhap0pFhF3NJ + +## Level 4: +`cd inhere` +`file ./*` +For refernce: `file ` (Used to find the type of a file)
+`cat ./-file07` + +Password: 4oQYVPkxZOOEOO5pTW81FB8j8lxXGUQw + +## Level 5: +`find . -type f -size 1033c`
+`cat ./inhere/maybehere07/.file2`
+For reference: +`ls -s` : Lists files with their disk usage in block of 1024 bytes. ie- Shows the disk space the file is taking
+Find command works recursively by default (recursively inside the directories) +. :Current directory + +Password: HWasnPhtq9AVKe0dmk45nxy20cvUa6EG + +## Level 6: +`find / -type f -user bandit7 -group bandit6 -size 33c 2>/dev/null` +`cat /var/lib/dpkg/info/bandit7.password`
+For personal reference:
+`.` :Refers to the current directory itself
+`..` :Refers to the parent directory
+`2>/dev/null` :Hide error messages (stderr) +/ :Root directory (Every file and folder in Linux is inside or under this directory) + +Password: morbNTDkSW6jIlUc0ymOdMaLnOlFVAaj + +## Level 7: +`grep -i millionth data.txt` + +Password: dfwvzFQi4mU0wfNbFOe9RoWskMLg7eEc + +## Level 8: +`sort data.txt | uniq -u`
+For personal reference:
+`uniq -u`: removes all duplicates entirely + +Password: 4CKMh1JI91bUIZZPXDqGanal4xvAg0JM + +## Level 9 +`strings data.txt | grep '==='` + +Password: FGUW5ilLVJrxX9kMYMmlN4MgbpfMiqey + +## Level 10 +`cat data.txt` +`base64 -d data.txt`
+Personal reference- Decode by converting Base64 to binary, then to ASCII text
+Each Base64 character = 6 bits
+4 Base64 chars = 3 bytes of data + +Password: dtR173fZKb0RRsDFSGsg2RWnpNVj3qRr + +## Level 11 +`cat data.txt` +`cat data.txt | tr 'A-Za-z' 'N-ZA-Mn-za-m'` + +Password: 7x16WNeHIi5YkIhWsfFIqoognUTyj9Q4 + +## Level 12 +`` + +For personal refernce-
+`mktemp` = make temporary file or directory
+Created inside /tmp by default (a place for temp files)
+Has a random name, so it's hard to guess (important for security)
+Prevents name collisions + + +Password: \ No newline at end of file diff --git a/assignment1/q2/2a.md b/assignment1/q2/2a.md new file mode 100644 index 0000000..76aba2a --- /dev/null +++ b/assignment1/q2/2a.md @@ -0,0 +1,40 @@ +# Shell Assignment 2.a (Missing Semester -The Shell) + +1. `echo $SHELL` + +2. `mkdir /tmp/missing` + +3. `man touch` + +4. `touch /tmp/missing/semester` + +5. `nano /tmp/mmissing/semester` + +Paste into the file: +```bash +#!/bin/sh +curl --head --silent https://missing.csail.mit.edu +``` +Save and Exit + +6. `/tmp/missing/semester` +Check why it failed: +`ls -l /tmp/missing/semester` + +7. `sh /tmp/missing/semester` + +8. `man chmod` + +9. ` chmod +x /tmp/missing/semester` + +Run the file : +`/tmp/missing/semester` + +10. `/tmp/missing/semester | grep -i "last-modified" > ~/last-modified.txt ` + +11. Battery: `cat /sys/class/power_supply/BAT0/capacity +` + +CPU temperature (in millidegrees Celsius): `cat /sys/class/thermal/thermal_zone0/temp +` + diff --git a/assignment1/q2/2b.md b/assignment1/q2/2b.md new file mode 100644 index 0000000..ba7fd40 --- /dev/null +++ b/assignment1/q2/2b.md @@ -0,0 +1,86 @@ +# Shell Tools and Scripting + +1. `ls -alh --sort=time --color=auto` + +2. +``` bash +#!/bin/bash + +# Function to save the current directory +marco() { + export MARCO_DIR="$PWD" +} + +# Function to return to the saved directory +polo() { + cd "$MARCO_DIR" || echo "No directory saved. Run marco first." +} +``` +Personal reference-
+`source filename.sh` -
+Executes the commands in the given file in the current terminal
+Variables and functions defined in the script remain available after it runs
+`bash marco.sh` -
+It runs in a new temporary shell, and any functions or variables won’t persist after that script ends.
+ +`export` makes the variable available globally to the current shell and any child processes.
+`$VAR_NAME` means: "Get the value stored in the variable `VAR_NAME` + +![alt text](image.png) + + +3. +`failing_Script.sh` +```bash +#!/usr/bin/env bash + +n=$(( RANDOM % 100 )) + +if [[ n -eq 42 ]]; then + echo "Something went wrong" + >&2 echo "The error was using magic numbers" + exit 1 +fi + +echo "Everything went according to plan" + +``` + +`watcher.sh` +```bash +#!/bin/bash + +count=0 + +while true; do + ((count++)) + ./failing_script.sh > out.txt 2> err.txt + if [[ $? -ne 0 ]]; then + echo "Script failed after $count runs" + echo "---- STDOUT ----" + cat out.txt + echo "---- STDERR ----" + cat err.txt + break + fi +done +``` +Personal reference- +`(( ))` is used for arithmetic operations in Bash
+`n=$(( RANDOM % 100 ))` - Store a random number from 0–99 in variable n
+`exit 0` = success
+`exit 1` or any non-zero = error/failure
+`[] [[]]` - Difference + +`chmod +x failing_script.sh` +`chmod +x watcher.sh` + +![alt text](image-1.png) + +4. +`find . -name '*.html' -print0 | xargs -0 zip html_files.zip`
+`-print0` - prints them separated by a null character (\0) instead of a newline, which is safe for filenames with spaces or special characters. + +5. +`find . -type f -printf '%T@ %p\n' | sort -nr`- Lists all files, from most recent to oldest.
+`find . -type f -printf '%T@ %p\n' | sort -n | tail -1` - Sorts from oldest to newest and gives the last(newest) file diff --git a/assignment1/q2/image-1.png b/assignment1/q2/image-1.png new file mode 100644 index 0000000..438e4cd Binary files /dev/null and b/assignment1/q2/image-1.png differ diff --git a/assignment1/q2/image.png b/assignment1/q2/image.png new file mode 100644 index 0000000..c8f939e Binary files /dev/null and b/assignment1/q2/image.png differ diff --git a/assignment1/q5/5.md b/assignment1/q5/5.md new file mode 100644 index 0000000..df5ba2f --- /dev/null +++ b/assignment1/q5/5.md @@ -0,0 +1,32 @@ +# Bash Script Automation Task: Clean Up Downloads Folder + +`clean_downloads.sh` +```bash +#!/bin/bash + +# Description: +# This script cleans up the Downloads folder by deleting files +# older than 2 months (~60 days). + +DOWNLOAD_DIR="/mnt/c/Users/Anushka/Downloads" +DAYS=60 +SIZE="+100M" + +echo "Cleaning up files older than $DAYS days and larger than $SIZE in $DOWNLOAD_DIR..." + +# Find and delete matching files +find "$DOWNLOAD_DIR" -type f -mtime +"$DAYS" -size "$SIZE" -print -delete + +echo "Cleanup complete." +``` + +Make the file executable: `chmod +x cleanup_downloads.sh`
+Run the script: `./cleanup_downloads.sh` + +## Justification +This Bash script automates the cleanup of the Downloads folder by deleting files that are larger than 100 MB and older than 60 days. It is particularly useful for freeing up disk space by removing outdated and bulky files that are no longer needed.
+Two parameters `$DAYS` and `$SIZE` are defined at the top of the script, making it easy to modify the criteria as needed
+By scheduling this script to run periodically, users can maintain a cleaner and more efficient file system without manual intervention. + +## Output +![alt text](image-2.png) diff --git a/assignment1/q5/image-2.png b/assignment1/q5/image-2.png new file mode 100644 index 0000000..1f92c93 Binary files /dev/null and b/assignment1/q5/image-2.png differ