‹ Back to Home

Five Bash Scripts That Replace Manual Folder Copying — Backups, Renames and Deploys

Small bash scripts still run backups, bulk renames and deploys in Indian Linux shops. Here are five you can copy, plus the safety line seniors keep pasting at the top.

Keerthika 8 min read
Follow on Google
Programming Five Bash Scripts That Replace Manual Folder Copying — Backups, Renames and Deploys 8 min left Follow on Google

TamilTech AI summary

  • Quote every variable or a space in a folder name can wreck tar, mv, or rm.
  • cron does not use your laptop PATH, so give it full paths and a log file.
  • set -euo pipefail stops a failed command from reporting success.

AI-assisted summary, checked by the TamilTech editorial team.

0:00
0:00
🔒 Listen is for subscribers. Subscribe
  • Quote every path. Spaces in folder names are the fastest way a backup script touches the wrong directory.
  • for loops and [[ ]] cover bulk renames and skip-if-missing checks without a GUI.
  • Functions return exit codes. That is what cron, Jenkins, and your team lead actually trust.
  • cron does not load your laptop aliases. Use full paths and a log file.
  • Start every script with set -euo pipefail so a failed tar cannot print done.

You know the 7.40 pm scene. Chennai office, last bus already a worry, and someone is still copying a project folder onto a USB drive in case Monday is ugly. That person is one small bash script away from going home.

Bash is not the cool kid. It is the glue under Docker, Ansible, Jenkins, and the random VM that still hosts a client site. If you can write five honest scripts, you can automate backups, bulk renames, and a basic deploy without waiting for a DevOps ticket.

What is bash scripting, in one line?

A bash script is a plain text file of Linux commands that the bash interpreter runs in order. You type the backup, rename, or deploy steps once, then reuse them.

That is the whole job. The five patterns below keep showing up on real servers and in interviews.

How do you keep variables from wrecking a backup?

Variables hold the boring bits: source folder, destination file, today's date. Quoting is the seatbelt. Without double quotes, a space in a folder name splits one path into two arguments. Then tar or rm gets creative, and you get a very long night.

Double-quote every expansion. Use ${var} when you glue text onto a value. Prefer "$HOME" over ~ inside scripts. Do not echo secrets into logs.

Here is a backup you can run after you create a test folder. It stamps the archive with the date so yesterday's copy is not overwritten.

#!/usr/bin/env bash
set -euo pipefail

SRC="${HOME}/projects"
DEST="${HOME}/backups/projects-$(date +%F).tar.gz"

mkdir -p "${HOME}/backups"
tar -czf "$DEST" "$SRC"
echo "Backup written to $DEST"

Read it like a senior at the next desk. SRC is where the work lives. DEST is a gzip tar under your home backups folder, so you do not need root. Both paths are quoted, so a space in your username does not split them. Try it on a copy. Do not point SRC at / unless you enjoy pain.

How do loops and conditions take over the boring clicks?

A for loop walks a list. [[ ]] asks a yes or no question. Together they rename a hundred log files while you drink tea. This is also how you skip a deploy when the build folder is missing.

Turn on nullglob so a pattern with zero matches does not become a fake filename. Use mv -- so a file that starts with a dash is not treated as an option.

#!/usr/bin/env bash
set -euo pipefail

shopt -s nullglob
for file in ./*.log; do
  if [[ -f "$file" ]]; then
    mv -- "$file" "${file%.log}.bak"
  fi
done

${file%.log} strips the suffix. The if is extra caution. Want a dry run? Put echo in front of mv, read the lines, then remove echo. That habit has saved more directories than any tool page.

Why do functions and exit codes matter on a real server?

Copy-pasting the same five lines into three scripts is how they drift. A function is a name for that chunk. Exit codes are the handshake with cron and CI. Zero means success. Anything else means stop.

${1:?...} fails loudly if someone forgot the argument. local keeps the variable inside the function. rsync plus a remote restart is a classic small deploy. Change nginx to your systemd unit.

#!/usr/bin/env bash
set -euo pipefail

deploy() {
  local src="${1:?source dir required}"
  local host="${2:?host required}"
  rsync -az --delete "$src"/ "${host}:/var/www/app/"
  ssh "$host" "systemctl restart nginx"
}

deploy ./dist [email protected]
echo "Deploy finished with exit code $?"

Test against a staging VM, not the box that takes customer traffic. --delete on rsync is powerful. It removes files on the server that vanished from ./dist. Great when you mean it. Brutal when ./dist is empty because you ran the script from the wrong folder.

How do you let cron run the backup while you sleep?

cron is the Linux scheduler. Edit yours with crontab -e. Five fields decide the time: minute, hour, day of month, month, day of week.

30 2 * * * means 2.30 am every night. Use the full path to the script. Redirect both output streams to a log, or you will debug silence.

30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

cron starts with a thin PATH. Your nvm, pyenv, and aliases are not there. If a job works at 2 pm and dies at 2 am, call /usr/bin/tar instead of tar. A cleanup that deletes archives older than seven days is fine after you test find on dummy files.

Do not mail yourself a huge tar. Log one line. Keep a week of files. Rotate the log so it does not fill the disk you were trying to protect.

What does this cost you besides a bit of time?

Bash itself costs nothing. It already sits on the Ubuntu, Debian, and RHEL-family boxes you SSH into. No INR licence. No extra App. The real bill is a script that deletes data, or a cron job that fills the disk. Run by hand first, run on a copy, then schedule. If the team already lives in Ansible or CI, keep bash for the tiny jobs.

Safety switchWhat it stops
set -eContinuing after a failed command
set -uUsing a variable you never set
set -o pipefailHiding a failure in the middle of a pipe

Why do seniors start every script with set -euo pipefail?

Because production lies. Without set -e, tar can fail and the next line still prints Backup done. Without set -u, an empty DEST turns a command into a surprise. Without pipefail, a fail in the middle of a pipe can look green because the last command exited zero.

Put this under the shebang and treat it as non-negotiable.

#!/usr/bin/env bash
set -euo pipefail

One caveat. Some commands return non-zero on purpose, like grep with no match. Handle those with an if, or add || true with a comment. Not as a reflex on every line.

Which mistakes get people into trouble?

Unquoted variables are the classic. A space, and mv sees two names. rm -rf "$DIR/" with DIR empty is a horror story if you skipped set -u. Testing inside the real web root instead of a copy is how weekends disappear.

Windows editors sneak CRLF into the shebang. Linux then looks for an interpreter called bash with a hidden character. cron jobs fail because PATH is short, the file is not executable, or the script used a relative path. Deploying as root when the app user would do leaves root-owned files at the next restart. Do not parse ls in a for loop. Glob like the rename script above.

Where does this show up in Indian companies and interviews?

Walk into a services project in Chennai, Bengaluru, Hyderabad, or Pune and you will still find bash wrapping backups of web roots, renaming invoice PDFs before they hit a share, and rsync to a staging VM. Product teams hide the same ideas inside CI, even when the UI is Jenkins or GitHub Actions.

Interviews love this because it is small and revealing. You may be asked to archive logs older than seven days, explain why you quoted a variable, or say what set -e does. They want to know you will not nuke a directory on day two.

What should you do now?

Do not collect more tabs. Write one script tonight on a throwaway folder.

Practice task: create ~/practice-src with three dummy files. Write backup-practice.sh that copies them to ~/practice-bak/YYYY-MM-DD/, refuses to run if that destination already exists, logs the exit code to ~/practice-bak/backup.log, and uses set -euo pipefail plus quoted variables. Run it twice. The second run should fail on purpose. That failure is the lesson.

When that feels boring, add a find that would delete copies older than seven days, but keep the delete behind an echo until you have read the list.

Frequently asked questions

5

Do I need bash, or will sh work?

These examples need bash for [[ ]], ${var:?}, and pipefail. Use #!/usr/bin/env bash, not sh, so the same file behaves on Ubuntu and RHEL-family boxes.

How do I make the script executable?

Save it, then run chmod +x backup.sh and call ./backup.sh. If you only want a one-off test, bash backup.sh works without the executable bit.

Why did cron fail when the same script worked in my terminal?

cron has a thin PATH and does not load your aliases or nvm. Use absolute paths, redirect logs, and confirm the file is executable.

Should I run backup and deploy scripts as root?

Only when the task truly needs it. Home-folder backups should run as you. App deploys should run as the app user so files are not root-owned.

I am on Windows. Can I still practise this?

Use WSL with a current Ubuntu, or a Linux VM. That matches the bash you will meet on a real server much better than Git Bash.

Get tomorrow’s tech news on WhatsApp

One short update a day, free. Follow the TamilTech channel.

What do you think?

people reacted

Keerthika

TamilTech editorial team · 3,875 articles

Keerthika is an editor at TamilTech, the Tamil and English technology publication founded by Praveen Kumar S. She covers AI, smartphones, gadgets, EVs, startups and cybersecurity i...

More from Keerthika

Ask TamilTech on WhatsApp

Tech doubt? Ask in Tamil or English — our WhatsApp assistant answers from TamilTech articles in seconds.

Related stories

Comments (0)

| Supports **bold**, *italic*, `code`

Be the first to comment!

Next story Java in 2026 for freshers: the setup campus drives still expect
TamilTech

TamilTech

Install app for faster access

Earn XP 🏆
WhatsApp
Notifications