Skip to main content
Image

r/bash


Tips for Speeding Up Your Bash Scripts
Tips for Speeding Up Your Bash Scripts

I wrote a script that renames a lot of files (tens of thousands). Performance was kind of slow (over 50 s), but with two changes it now runs at under 3 seconds, a 20x speed boost. Thought I'd share, in case it helps anyone else. These tips are good if you do things thousands of time in a script, I don't think they are relevant in all scripts.

Don't spawn sub shells if you can avoid it

Instead of command substitution with printf:

new_file="$(printf "%s/%s_%.${len}d.%s" "$new_dir" "$prefix" "$i" "$ext")"

do:

printf -v new_file "%s/%s_%.${len}d.%s" "$new_dir" "$prefix" "$i" "$ext"

This will save a sub shell.

Use builtins instead of external programs

Bash has optional builtins that you can enable. On my system they are located in /usr/lib/bash . You can enable them in your script by using enable <builtin>. Be sure to check the exit code. On my system a mv builtin is not available, but since I was renaming on the same file system I figured I could use ln and rm instead.

So instead of using external mv for each file I did:

builtin ln "$old_file" "$new_file" && builtin rm "$old_file"

In this case the builtin probably isn't required since builtins are prioritized over external commands, but I used them to be more explicit. Just be sure to use && so that rm never runs unless the hard link has been successfully created.


Advertisement: Rise or die. THE UPRISING starring Andrew Garfield is only in theaters September 10.
Rise or die. THE UPRISING starring Andrew Garfield is only in theaters September 10.
media poster


Linux Interview Question
Linux Interview Question

Today I had a mock interview with a senior Linux administrator, and he asked me a question that completely caught me off guard:

"Can you collect CPU, Memory, and Disk usage in a Bash script without using top**,** free**,** df**, or any external commands?"**

My immediate answer was No.

Honestly, I've been working with Linux for years, but I've always relied on standard commands and monitoring tools to troubleshoot systems. I never stopped to think about where those commands actually get their data from.

The interviewer then gave me a hint about the /proc and /sys filesystems. That completely changed my perspective. I realized these commands are just reading data that the kernel already exposes.

But then came the follow-up questions, and once again I was stuck:

  • Is reading directly from /proc and /sys the correct approach?

  • How would you calculate CPU utilization using /proc/stat?

  • How would you determine memory usage from /proc/meminfo?

  • How would you calculate filesystem usage without relying on df?

I found this really interesting because it tests your understanding of Linux internals rather than just your ability to use commands.

Has anyone here been asked similar questions in Linux SysAdmin, DevOps, or SRE interviews? I'd really appreciate any explanations, learning resources, or examples on how you'd answer these follow-up questions. It help me for my preparation for Linux interviews


bash script to find and change directory
bash script to find and change directory

Hello All,

I’m not a bash programmer at all but I want to write a bash script which does the following,

First, determine which "Downloads" folders is present.

The English one"Downloads" , or the French "Téléchargements" one.

The username is unknown so must be determined also as a variable.

$USER if not mistaking ?

After determining the "Downloads" folder, I want to set the complete folder path as a variable.

I’m sure it’s possible but after trying several times, I can’t get it right.

Can you please help me?

Thank You very kindly,

Tom