Photo courtesy of f1r3storm85 In the previous article, we discussed about Linux environment variables PS[1-4] and PROMPT_COMMAND. If used effectively, PS1 can provide valuable information right on the command prompt. In Tomb Raider, Angelina Jolie has all the gadgets and weapons at her finger tips t...
Customising PS1 to Include a Shell Script
Today – instead of making actual progress in the course – I got lost in the supplementary reading about setting yourself up with a custom prompt. Since most shells come pre-configured with many of the ideas of the blog post already implemented, the only option that really caught my attention was using a shell script inside the PS1 variable. The example given was a script to count the size of regular files in the current directory and show it next to the prompt. I was more interested in the total size of the current directory, including all files in subdirectories, so I set out to try that. This post is more of an answer to the call to post one's progress, than an actual offer of useful code, since I still don't deem having the filesize shown important enough to incorporate into my personal prompt.
Step by Step to a Prompt Which Displays File Size
Finished Product First
All this produces is a simple addendum to the prompt, displaying the size of the files in the current directory and its subdirectories. Can be placed anywhere in the custom prompt:
[X bytes]
The Bash Script
To achieve this, one has to write a bash script to recursively sum up filesizes:
```bash #!/bin/bash
calculate_size() { local directory=$1 local totalsize=0
for filesize in $(find "$directory" -type f -exec stat -c %s {} \; 2>/dev/null); do ((totalsize += filesize)) done
for subdir in $(find "$directory" -type d); do if [[ "$subdir" != "$directory" ]]; then ((totalsize += $(calculate_size "$subdir"))) fi done
echo "$totalsize" }
Usage example
total=$(calculate_size .) echo "$total bytes" ```
Adding the Script to PATH
After making the script executable, one has to include the script itself or its folder into PATH. This can be done by including the following line in ~/.bashrc
, which is where customisation is done on my Ubuntu LTS server, or in whatever file is the correct one for your distribution.
export PATH="$PATH:/path/to/script.sh"
or
export PATH="$PATH:/path/to/scripts/folder"
Customising PS1
Find PS1 in whatever customisation file it is defined in on your system, and add the following snippet anywhere, where you would like the file size to be displayed.
[$(path/to/your/script.sh)]
Notes
- The Bash script was in part written by me with the knowledge I gained from a previous shell scripting course, and in part written and refined by ChatGPT 3.5
- This by no means has to be the most elegant way to calculate total file size, I am new to shell scripting
- There might very well be cases where the script fails due to special characters or other unexpected input, it has no error catching mechanisms
- It could be further refined to show sizes in bytes/kilobytes/megabytes/... according to the total calculated
- Unfortunately, Lemmy does not seem to support syntax highlighting for code blocks
tl;dr
I talked about how to customise the PS1 prompt in a shell to display the total size of files in the current directory and its subdirectories. This is done by creating a Bash script to calculate the total file size and incorporating it into the PS1 variable. The script should be added to the PATH and the PS1 should be modified to display the file size information.
Are we supposed to guess in the comments? If so, I guess
spoiler
Rosebud.
People have already tried to explain the idea behind Mastodon. If you are also looking for an overview of how Mastodon "works", please have a look at https://fedi.tips/.
MC01, a passport-sized smartphone with a QWERTY keyboard and an exquisite minimalist design. The MC01 never made it to market because - as Kevin pointed out in his story - of industry constraints but it had a strong resonance with readers, sparking a conversation among the community about the need for a device with a physical keyboard and a privacy-centric operating system. punkt.ch