{"id":1051,"date":"2025-08-20T03:06:59","date_gmt":"2025-08-20T03:06:59","guid":{"rendered":"https:\/\/yairmartinezcybersecurityportfolio.com\/?p=1051"},"modified":"2025-12-26T23:53:39","modified_gmt":"2025-12-26T23:53:39","slug":"linux-project-bash-audit-backup-script","status":"publish","type":"post","link":"https:\/\/yairmartinezcybersecurityportfolio.com\/?p=1051","title":{"rendered":"Linux Project: Bash Audit &amp; Backup Script"},"content":{"rendered":"\n<p>As part of my self-study in scripting and Linux, I worked on a challenge designed to test my Bash skills. A prompt asked me to create a script that could scan a directory, identify sensitive or large files, and archive the results with clear logging and output.<\/p>\n\n\n\n<p>I didn\u2019t come up with the idea, but I did write all the Bash code myself, structured the logic, tested the behavior, and refined it until it worked reliably. <\/p>\n\n\n\n<div class=\"wp-block-stackable-text stk-block-text stk-block stk-81739a4\" data-block-id=\"81739a4\"><p class=\"stk-block-text__text\">Below is me using my script.<\/p><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div class=\"wp-block-stackable-image stk-block-image stk-block stk-12e9b9a\" data-block-id=\"12e9b9a\"><style>.stk-12e9b9a .stk-img-wrapper{width:100% !important;height:465px !important;}:where(.stk-hover-parent:hover,  .stk-hover-parent.stk--is-hovered) .stk-12e9b9a .stk-img-wrapper::after{background-color:#000000B3 !important;}<\/style><figure><span class=\"stk-img-wrapper stk-image--shape-stretch\"><img loading=\"lazy\" decoding=\"async\" class=\"stk-img wp-image-1331\" src=\"https:\/\/yairmartinezcybersecurityportfolio.com\/wp-content\/uploads\/2025\/08\/BashandAudt.gif\" width=\"1290\" height=\"676\"\/><\/span><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">What the Script Does<\/h2>\n\n\n\n<p>The script (<code>audit_backup.sh<\/code>) combines a few core tasks:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Find the Largest Files<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>find \"$target_dir\" -type f -exec du -h {} + | sort -rh | head -5 &gt; logs\/largest_files.txt\n<\/code><\/pre>\n\n\n\n<p>This scans the target directory, lists file sizes, sorts them in human-readable format, and logs the top 5 largest files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Identify Potentially Sensitive Files<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>find \"$target_dir\" \\( -size +1M -o -name \"*.log\" -o -name \"*.key\" \\) | grep -vi 'debug' &gt; logs\/findings.txt\n<\/code><\/pre>\n\n\n\n<p>This flags files over 1MB or with <code>.log<\/code> \/ <code>.key<\/code> extensions, then <strong>excludes any paths containing \u201cdebug\u201d (case-insensitive)<\/strong> to keep noisy debug logs out of the results. The filtered list is saved to <code>logs\/findings.txt<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Create a Clean Archive<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Default (preserves full path)\ntar --exclude='*.bin' -czf \"archive\/$(basename \"$target_dir\")-$timestamp.tar.gz\" \"$target_dir\"\n\n# Alternative (flatter archive, no full path)  \n# tar --exclude='*.bin' -czf \"archive\/$(basename \"$target_dir\")-$timestamp.tar.gz\" \\\n#   -C \"$(dirname \"$target_dir\")\" \"$(basename \"$target_dir\")\"\n<\/code><\/pre>\n\n\n\n<p>This compresses the directory into a timestamped archive while excluding <code>.bin<\/code> files. A commented-out alternative is also included, letting you choose whether to preserve the original directory path or not.<\/p>\n\n\n\n<p>below is me using my script with the alternative on a different directory <\/p>\n\n\n\n<div class=\"wp-block-stackable-image stk-block-image stk-block stk-e404ddc\" data-block-id=\"e404ddc\"><style>.stk-e404ddc .stk-img-wrapper{width:100% !important;height:465px !important;}:where(.stk-hover-parent:hover,  .stk-hover-parent.stk--is-hovered) .stk-e404ddc .stk-img-wrapper::after{background-color:#000000B3 !important;}<\/style><figure><span class=\"stk-img-wrapper stk-image--shape-stretch\"><img loading=\"lazy\" decoding=\"async\" class=\"stk-img wp-image-1333\" src=\"https:\/\/yairmartinezcybersecurityportfolio.com\/wp-content\/uploads\/2025\/08\/BashandAuditAlt.gif\" width=\"1290\" height=\"676\"\/><\/span><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">4. Log Disk Usage Summary<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    echo \"Disk usage of target folder:\"\n    du -sh \"$target_dir\"\n    echo\n    echo \"Available disk space on system:\"\n    df -h \/\n} &gt; logs\/summary.txt\n<\/code><\/pre>\n\n\n\n<p>This logs both the folder\u2019s total size and the system\u2019s available disk space, giving quick context for storage health.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Purpose of the Exercise<\/h2>\n\n\n\n<p>This project was about applying the Bash skills I had been learning:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Writing modular, readable scripts<\/li>\n\n\n\n<li>Using core commands effectively (<code>find<\/code>, <code>du<\/code>, <code>grep<\/code>, <code>tar<\/code>, <code>df<\/code>)<\/li>\n\n\n\n<li>Building automation with proper logging<\/li>\n\n\n\n<li>Thinking practically about how simple scripts can help with audits and backups<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What I Learned<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>How to combine arguments, variables, and pipes to make Bash scripts more powerful.<\/li>\n\n\n\n<li>The importance of <strong>filtering output<\/strong> with tools like <code>grep -v<\/code> to ignore noise (such as DEBUG files).<\/li>\n\n\n\n<li>Why including options (like the two tar methods) makes scripts more flexible.<\/li>\n\n\n\n<li>That even small automation scripts can make system administration tasks faster and more reliable.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Final Thoughts<\/h2>\n\n\n\n<p>Through this project I sharpened my skills on arguments, variables, and pipes\u2014essentials in automation\u2014and strengthened both my confidence and competence in Bash.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As part of my self-study in scripting and Linux, I worked on a challenge designed to test my Bash skills. A prompt asked me to create a script that could scan a directory, identify sensitive or large files, and archive the results with clear logging and output. I didn\u2019t come up with the idea, but [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1331,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,1],"tags":[43,42,51],"class_list":["post-1051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-projects","tag-bash","tag-linux","tag-logging"],"_links":{"self":[{"href":"https:\/\/yairmartinezcybersecurityportfolio.com\/index.php?rest_route=\/wp\/v2\/posts\/1051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yairmartinezcybersecurityportfolio.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yairmartinezcybersecurityportfolio.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yairmartinezcybersecurityportfolio.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yairmartinezcybersecurityportfolio.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1051"}],"version-history":[{"count":12,"href":"https:\/\/yairmartinezcybersecurityportfolio.com\/index.php?rest_route=\/wp\/v2\/posts\/1051\/revisions"}],"predecessor-version":[{"id":1346,"href":"https:\/\/yairmartinezcybersecurityportfolio.com\/index.php?rest_route=\/wp\/v2\/posts\/1051\/revisions\/1346"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yairmartinezcybersecurityportfolio.com\/index.php?rest_route=\/wp\/v2\/media\/1331"}],"wp:attachment":[{"href":"https:\/\/yairmartinezcybersecurityportfolio.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yairmartinezcybersecurityportfolio.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yairmartinezcybersecurityportfolio.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}