{"id":24788835,"url":"https://github.com/avisec1337/bashitout","last_synced_at":"2025-10-08T02:36:02.000Z","repository":{"id":272800280,"uuid":"917798213","full_name":"AviSec1337/BashItOut","owner":"AviSec1337","description":"This repository contains solutions to all the challenges from [CMD Challenge](https://cmdchallenge.com/), a platform for practicing command-line skills. Each solution is designed to help improve your proficiency with Linux/Unix shell commands and explore creative ways to solve tasks in the terminal.","archived":false,"fork":false,"pushed_at":"2025-01-16T20:38:39.000Z","size":30,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-29T17:15:01.189Z","etag":null,"topics":["bash"],"latest_commit_sha":null,"homepage":"https://cmdchallenge.com/","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AviSec1337.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-16T17:03:15.000Z","updated_at":"2025-01-24T03:17:32.000Z","dependencies_parsed_at":"2025-01-16T18:29:22.709Z","dependency_job_id":"1e81ff1a-2c26-4197-9c86-91f590e2d22b","html_url":"https://github.com/AviSec1337/BashItOut","commit_stats":null,"previous_names":["avisec1337/bashmastery","avisec1337/bashitout"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AviSec1337%2FBashItOut","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AviSec1337%2FBashItOut/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AviSec1337%2FBashItOut/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AviSec1337%2FBashItOut/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AviSec1337","download_url":"https://codeload.github.com/AviSec1337/BashItOut/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245267570,"owners_count":20587459,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash"],"created_at":"2025-01-29T17:15:12.366Z","updated_at":"2025-10-08T02:35:56.968Z","avatar_url":"https://github.com/AviSec1337.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"---\n\n## 🌟 CMD Challenge Solutions with Detailed Explanations\n\nThis repository contains solutions to various command-line tasks from the [CMD Challenge](https://cmdchallenge.com/). Each challenge is designed to test and improve your shell scripting and command-line skills. Below is the list of challenges and their solutions, along with detailed explanations to help beginners understand the concepts.\n\n---\n\n### 1. **Hello World 🚀**\n**Task:** Print \"hello world\".  \n**Solution:**\n```bash\necho \"hello world\"\n```\n**Explanation:**\n- `echo` is a command used to display text or strings on the terminal.\n- By typing `echo \"hello world\"`, the shell will output the text `hello world` to the screen.\n\n---\n\n### 2. **Current Working Directory 📂**\n**Task:** Print the current working directory.  \n**Solution:**\n```bash\npwd\n```\n**Explanation:**\n- `pwd` stands for \"print working directory.\"\n- This command displays the full path of the directory you are currently in.\n\n---\n\n### 3. **List Files 📜**\n**Task:** List all of the files in the current directory, one file per line.  \n**Solution:**\n```bash\nls -1\n```\n**Explanation:**\n- `ls` lists files and directories in the current directory.\n- The `-1` option ensures that each file is displayed on a separate line.\n\n---\n\n### 4. **Last Lines 📖**\n**Task:** Print the last 5 lines of `access.log`.  \n**Solution:**\n```bash\ntail -5 access.log\n```\n**Explanation:**\n- `tail` displays the last part of a file.\n- The `-5` option tells `tail` to show the last 5 lines of the file `access.log`.\n\n---\n\n### 5. **Find String in a File 🔍**\n**Task:** Print all lines in `access.log` that contain the string \"GET\".  \n**Solution:**\n```bash\ngrep GET access.log\n```\n**Explanation:**\n- `grep` searches for a specific pattern in a file.\n- Here, it searches for the word `GET` in the file `access.log` and prints all matching lines.\n\n---\n\n### 6. **Search for Files Containing String 🕵️**\n**Task:** Print all files, one per line, that contain the string \"500\".  \n**Solution:**\n```bash\ngrep -rl * -e 500\n```\n**Explanation:**\n- `grep` searches files for a given pattern.\n- `-r` means recursive, so it searches in the current directory and all subdirectories.\n- `-l` means it only prints the names of files containing the match, not the matching lines.\n- `*` searches all files in the current directory.\n- `-e 500` specifies the string `500` to search for.\n\n---\n\n### 7. **Search for Files by Extension 📂**\n**Task:** Print the relative file paths for all files that start with `access.log`.  \n**Solution:**\n```bash\nfind . -name \"access.log*\"\n```\n**Explanation:**\n- `find` searches for files and directories.\n- `.` specifies the current directory.\n- `-name` tells `find` to look for files matching a specific name pattern.\n- `\"access.log*\"` matches any file name starting with `access.log`.\n\n---\n\n### 8. **Search for String in Files Recursively 🔄**\n**Task:** Print all matching lines (without the filename or file path) in files starting with `access.log` and containing the string \"500\".  \n**Solution:**\n```bash\nfind . -name \"access.log*\" | xargs grep -h 500\n```\n**Explanation:**\n- `find . -name \"access.log*\"` finds all files starting with `access.log`.\n- The `|` symbol pipes the output of `find` into the next command.\n- `xargs` passes the file names to `grep`.\n- `grep -h` searches for `500` but hides file names from the output.\n\n---\n\n### 9. **Extract IP Addresses 🌐**\n**Task:** Extract all IP addresses from files that start with `access.log`, printing one per line.  \n**Solution:**\n```bash\nfind . -name \"access.log*\" | xargs grep -Eo '^[^ ]+'\n```\n**Explanation:**\n- `find . -name \"access.log*\"` finds files starting with `access.log`.\n- `xargs` passes the file names to `grep`.\n- `grep -Eo '^[^ ]+'` extracts patterns using regular expressions:\n  - `-E` enables extended regular expressions.\n  - `-o` prints only the matching part of the line.\n  - `'^[^ ]+'` matches the beginning of a line until the first space, which is often the IP address in logs.\n\n---\n\n### 10. **Delete Files ❌**\n**Task:** Delete all files in the current directory, including subdirectories and their contents.  \n**Solution:**\n```bash\nfind . -delete\n```\n**Explanation:**\n- `find .` searches in the current directory and all subdirectories.\n- `-delete` removes the files and directories found by `find`.\n**Note:** Be very careful when using this command as it will delete everything in the specified directory!\n\n---\n\n### 11. **Count Files 🔢**\n**Task:** Count the number of files in the current working directory.  \n**Solution:**\n```bash\nls | wc -l\n```\n**Explanation:**\n- `ls` lists files in the current directory.\n- The `|` passes the output of `ls` to `wc` (word count).\n- `wc -l` counts the number of lines, which equals the number of files.\n\n---\n\n### 12. **Simple Sort 🔀**\n**Task:** Print the contents of `access.log` sorted.  \n**Solution:**\n```bash\nsort access.log\n```\n**Explanation:**\n- `sort` arranges the lines of a file in alphabetical or numerical order by default.\n- Here, it sorts the lines in `access.log`.\n\n---\n\n### 13. **Count String in Line 🧴**\n**Task:** Print the number of lines in `access.log` containing the string \"GET\".  \n**Solution:**\n```bash\ngrep GET access.log | wc -l\n```\n**Explanation:**\n- `grep GET access.log` finds all lines containing `GET`.\n- The `|` passes these lines to `wc -l`.\n- `wc -l` counts the number of matching lines.\n\n---\n\n### 14. **Split on a Character 🛁**\n**Task:** Split the numbers in `split-me.txt` on the `;` character, printing one per line.  \n**Solution:**\n```bash\ncat split-me.txt | sed 's/;/\n/g'\n```\n**Explanation:**\n- `cat split-me.txt` outputs the content of the file `split-me.txt`.\n- `|` pipes the output to `sed`.\n- `sed 's/;/\n/g'` replaces each semicolon (`;`) with a newline character (`\n`), printing each number on a new line.\n\n---\n\n### 15. **Print Number Sequence 📈**\n**Task:** Print the numbers 1 to 100 separated by spaces.  \n**Solution:**\n```bash\necho {1..100}\n```\n**Explanation:**\n- `echo` outputs text to the terminal.\n- `{1..100}` is a sequence expression that generates numbers from 1 to 100.\n- This prints all numbers in a single line, separated by spaces.\n\n---\n\n### 16. **Remove Files with Extension 🧹**\n**Task:** Remove all `.doc` files recursively in the current directory.  \n**Solution:**\n```bash\nfind . -name \"*.doc\" -delete\n```\n**Explanation:**\n- `find . -name \"*.doc\"` searches for files ending with `.doc`.\n- `-delete` removes these files.\n\n---\n\n### 17. **Replace Text in Files ✍️**\n**Task:** Delete \"challenges are difficult\" from all `.txt` files.  \n**Solution:**\n```bash\nfind . -name \"*.txt\" -exec sed -i 's/challenges are difficult//g' {} +\n```\n**Explanation:**\n- `find . -name \"*.txt\"` locates all `.txt` files.\n- `-exec` runs a command on each file found.\n- `sed -i 's/challenges are difficult//g'` removes the text \"challenges are difficult\" from each file in place (`-i`).\n\n---\n\n### 18. **Sum All Numbers ➕**\n**Task:** Print the sum of numbers in `sum-me.txt`.  \n**Solution:**\n```bash\ncat sum-me.txt | xargs | sed 's/ /+/g' | bc\n```\n**Explanation:**\n- `cat sum-me.txt` outputs the file's content.\n- `xargs` joins all numbers into a single line.\n- `sed 's/ /+/g'` replaces spaces between numbers with `+` signs to form a mathematical expression.\n- `bc` evaluates the expression and prints the result.\n\n---\n\n### 19. **Just the Files 📁**\n**Task:** Print all files in the current directory recursively without leading paths.  \n**Solution:**\n```bash\nfind . -type f -printf \"%f\\n\"\n```\n**Explanation:**\n- `find .` searches the current directory and subdirectories.\n- `-type f` restricts the search to files (not directories).\n- `-printf \"%f\\n\"` prints only the filenames without their paths.\n\n---\n\n### 20. **Remove Extensions from Files 🛠️**\n**Task:** Remove file extensions recursively.  \n**Solution:**\n```bash\nfind . -type f -exec bash -c 'mv \"$1\" \"${1%.*}\"' - '{}' \\;\n```\n**Explanation:**\n- `find . -type f` finds all files in the current directory and subdirectories.\n- `-exec bash -c` runs a custom bash command for each file.\n- `mv \"$1\" \"${1%.*}\"` renames the file by removing everything after the last `.` (dot), which is the file extension.\n\n---\n\n### 21. **Replace Spaces in Filenames 🌌**\n**Task:** Replace spaces in filenames with a `.` character.  \n**Solution:**\n```bash\nfind . -type f -name \"* *\" | while read -r file; do mv \"$file\" \"${file// /_}\"; done\n```\n**Explanation:**\n- `find . -type f -name \"* *\"` finds files with spaces in their names.\n- The `|` pipes the list of filenames to a `while` loop.\n- `mv \"$file\" \"${file// /_}\"` renames each file by replacing spaces with underscores (`_`).\n\n---\n\n### 22. **Files Starting with a Number 🔢**\n**Task:** Print filenames starting with a number.  \n**Solution:**\n```bash\nfind . -name '[0-9]*' -type f -printf \"%f\\n\"\n```\n**Explanation:**\n- `find . -name '[0-9]*'` searches for files whose names start with a digit.\n- `-type f` ensures only files are included.\n- `-printf \"%f\\n\"` prints only the filenames.\n\n---\n\n### 23. **Print Nth Line 📜**\n**Task:** Print the 25th line of `faces.txt`.  \n**Solution:**\n```bash\nsed '25q;d' faces.txt\n```\n**Explanation:**\n- `sed` is used to process and transform text in a file.\n- `'25q;d'` tells `sed` to quit (`q`) after reading 25 lines, and delete (`d`) all but the 25th line.\n\n---\n\n### 24. **Remove Duplicate Lines ♻️**\n**Task:** Print `faces.txt` with only the first instance of each duplicate line.  \n**Solution:**\n```bash\nawk '!seen[$0]++' faces.txt\n```\n**Explanation:**\n- `awk` processes text line by line.\n- `!seen[$0]++` ensures each line is printed only the first time it is encountered.\n\n---\n\n### 25. **Corrupted Text 🛠️**\n**Task:** Remove random `!` marks from `war_and_peace.txt` to restore the original text.  \n**Solution:**\n```bash\n\u003c war_and_peace.txt tr -s '!' | sed 's/!//g'\n```\n**Explanation:**\n- `tr -s '!'` squeezes multiple `!` into a single one.\n- `sed 's/!//g'` removes all remaining `!` characters.\n\n---\n\n### 26. **Print Hello World (Alternate) 🚀**\n**Task:** Print \"Hello World\" using escaped space.  \n**Solution:**\n```bash\necho hello\\ world\n```\n**Explanation:**\n- `hello\\ world` escapes the space with a `\\`, ensuring the two words are treated as part of the same string.\n\n---\n\n### 27. **Count Tab Characters 📂**\n**Task:** Count lines containing tab characters in `file-with-tabs.txt`.  \n**Solution:**\n```bash\ngrep -c $'\\t' file-with-tabs.txt\n```\n**Explanation:**\n- `grep -c` counts the number of matching lines.\n- `$'\\t'` matches tab characters.\n\n---\n\n### 28. **Remove Files Starting with Dash 🗑️**\n**Task:** Remove files starting with a dash in the filename.  \n**Solution:**\n```bash\nfind . -name '-*' -delete\n```\n**Explanation:**\n- `find . -name '-*'` searches for files starting with a dash (`-`).\n- `-delete` removes those files.\n\n---\n\n### 29. **Remove Files without Specific Extensions ❌**\n**Task:** Remove all files without `.txt` and `.exe` extensions recursively.  \n**Solution:**\n```bash\nfind . -type f -not \\( -name '*.txt' -or -name '*.exe' \\) -delete\n```\n**Explanation:**\n- `find . -type f` finds all files.\n- `-not \\( -name '*.txt' -or -name '*.exe' \\)` excludes `.txt` and `.exe` files.\n- `-delete` removes all other files.\n\n---\n\n### 30. **Common IPs in Logs 🌐**\n**Task:** Print IP addresses common to `access.log.1` and `access.log.2`.  \n**Solution:**\n```bash\ncomm -12 \u003c(cut -d' ' -f1 access.log.1 | sort) \u003c(cut -d' ' -f1 access.log.2 | sort)\n```\n**Explanation:**\n- `cut -d' ' -f1` extracts the first column (IP addresses) from each file.\n- `sort` arranges the IPs in order.\n- `comm -12` finds common lines between the two sorted files.\n\n---\n\n### 31. **Reverse Lines 🔄**\n**Task:** Print the lines of the file `reverse-me.txt` in reverse order so that the last line appears first.  \n**Solution:**\n```bash\ntac reverse-me.txt\n```\n**Explanation:**\n- `tac` prints a file line by line in reverse order.\n\n---\n\n### 32. **Unique Primes 🔢**\n**Task:** Print the number of unique prime numbers in `random-numbers.txt`.  \n**Solution:**\n```bash\ncat random-numbers.txt | sort | uniq | factor | awk 'NF==2' | wc -l\n```\n**Explanation:**\n- `sort | uniq` eliminates duplicate numbers.\n- `factor` breaks numbers into their prime factors.\n- `awk 'NF==2'` filters lines with exactly two fields (prime numbers only).\n- `wc -l` counts the number of unique primes.\n\n---\n\n### 33. **Find Ports in Netstat 🌐**\n**Task:** Print all IPv4 listening ports from `netstat.out`, sorted in descending order.  \n**Solution:**\n```bash\ngrep '\\bLISTEN\\b' netstat.out | grep -oP 'tcp\\s+.*:\\K\\d+' | sort -nr\n```\n**Explanation:**\n- `grep '\\bLISTEN\\b'` filters lines containing `LISTEN`.\n- `grep -oP 'tcp\\s+.*:\\K\\d+'` extracts port numbers using Perl-compatible regular expressions.\n- `sort -nr` sorts the port numbers in descending numerical order.\n\n---\n\n### 34. **Monitor File Changes 📋**\n**Task:** Watch for any changes in the `logs` directory and display them in real-time.  \n**Solution:**\n```bash\ninotifywait -m logs\n```\n**Explanation:**\n- `inotifywait` is a tool for monitoring file system events.\n- `-m` enables monitoring mode to keep the command running.\n- `logs` specifies the directory to monitor. It displays events like file modifications, deletions, and creations.\n\n---\n\n### 35. **Detect Failed SSH Login Attempts 🚨**\n**Task:** Count the number of failed SSH login attempts in `/var/log/auth.log`.  \n**Solution:**\n```bash\ngrep \"Failed password\" /var/log/auth.log | wc -l\n```\n**Explanation:**\n- `grep \"Failed password\"` searches for failed login attempts in the log file.\n- `wc -l` counts the number of matching lines.\n\n---\n\n### 36. **List Open Network Connections 🌐**\n**Task:** Display all open network connections and their statuses.  \n**Solution:**\n```bash\nnetstat -tuln\n```\n**Explanation:**\n- `netstat` displays network statistics.\n- `-tuln` shows TCP/UDP connections (`t`/`u`), listening ports (`l`), and numeric addresses (`n`).\n\n---\n\n### 37. **Search for SUID Files 🛡️**\n**Task:** Find all files with the SUID bit set on the system.  \n**Solution:**\n```bash\nfind / -perm -4000 2\u003e/dev/null\n```\n**Explanation:**\n- `find /` searches the entire filesystem.\n- `-perm -4000` matches files with the SUID bit set.\n- `2\u003e/dev/null` suppresses permission-denied errors.\n\n---\n\n### 38. **Analyze Disk Usage 📊**\n**Task:** Find the top 5 largest directories in the `/var` directory.  \n**Solution:**\n```bash\ndu -h /var | sort -rh | head -n 5\n```\n**Explanation:**\n- `du -h /var` calculates the size of each directory in human-readable format.\n- `sort -rh` sorts by size in reverse order.\n- `head -n 5` displays the top 5 results.\n\n---\n\n### 39. **Check for Active Users 👤**\n**Task:** List all currently logged-in users.  \n**Solution:**\n```bash\nwho\n```\n**Explanation:**\n- `who` displays information about users currently logged into the system.\n\n---\n\n### 40. **Identify Open Files by Process 🔍**\n**Task:** List all open files by the `nginx` process.  \n**Solution:**\n```bash\nlsof -c nginx\n```\n**Explanation:**\n- `lsof` lists open files.\n- `-c nginx` filters for files opened by processes named `nginx`.\n\n---\n\n### 41. **Verify File Integrity 🔒**\n**Task:** Generate a SHA256 checksum for the file `backup.tar.gz`.  \n**Solution:**\n```bash\nsha256sum backup.tar.gz\n```\n**Explanation:**\n- `sha256sum` calculates the SHA256 hash of a file to verify its integrity.\n\n---\n\n### 42. **Securely Delete Files 🧹**\n**Task:** Permanently delete `sensitive.txt` without recovery.  \n**Solution:**\n```bash\nshred -u sensitive.txt\n```\n**Explanation:**\n- `shred` overwrites the file multiple times before deleting it.\n- `-u` ensures the file is deleted after shredding.\n\n---\n\n### 43. **Monitor Memory Usage 📈**\n**Task:** Display real-time memory usage.  \n**Solution:**\n```bash\nwatch -n 1 free -h\n```\n**Explanation:**\n- `watch -n 1` runs a command every second.\n- `free -h` displays memory usage in human-readable format.\n\n---\n\n### 44. **Find Suspicious Processes 🕵️‍♂️**\n**Task:** Identify processes using high CPU usage.  \n**Solution:**\n```bash\nps aux --sort=-%cpu | head -n 10\n```\n**Explanation:**\n- `ps aux` lists all running processes.\n- `--sort=-%cpu` sorts them by CPU usage in descending order.\n- `head -n 10` shows the top 10 results.\n\n---\n\n### 45. **Extract Subdomain from URL 🌐**\n**Task:** Extract the subdomain from a list of URLs in `websites.txt`.  \n**Solution:**\n```bash\nawk -F[/:] '{print $4}' websites.txt\n```\n**Explanation:**\n- `awk` processes text line by line.\n- `-F[/:]` uses `/` and `:` as field separators.\n- `{print $4}` extracts the fourth field, which is the subdomain or domain.\n\n---\n\n### 46. **List Recently Modified Files 📆**\n**Task:** Find files modified in the last 24 hours.  \n**Solution:**\n```bash\nfind . -type f -mtime -1\n```\n**Explanation:**\n- `find . -type f` locates all files.\n- `-mtime -1` matches files modified within the last day.\n\n---\n\n### 47. **Check Open Ports 🔓**\n**Task:** Display all open ports on the system.  \n**Solution:**\n```bash\nss -tuln\n```\n**Explanation:**\n- `ss` displays socket statistics.\n- `-tuln` shows TCP/UDP connections, listening ports, and numeric addresses.\n\n---\n\n### 48. **Kill Processes by Name 🛑**\n**Task:** Terminate all processes named `apache2`.  \n**Solution:**\n```bash\npkill apache2\n```\n**Explanation:**\n- `pkill` sends signals to processes by name.\n- `apache2` is the name of the processes to terminate.\n\n---\n\n### 49. **Detect Hidden Files 📁**\n**Task:** Find all hidden files in the current directory.  \n**Solution:**\n```bash\nfind . -type f -name \".*\"\n```\n**Explanation:**\n- `find . -type f` locates all files.\n- `-name \".*\"` matches hidden files (names starting with `.`).\n\n---\n\n### 50. **Track System Uptime ⏳**\n**Task:** Display how long the system has been running.  \n**Solution:**\n```bash\nuptime\n```\n**Explanation:**\n- `uptime` shows the current time, uptime duration, and load averages.\n\n---\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favisec1337%2Fbashitout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Favisec1337%2Fbashitout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favisec1337%2Fbashitout/lists"}