Linux.org User-created Helpful Shell Scripts

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
9,093
Reaction score
7,713
Credits
74,241
Did you write a new script and want to share it with fellow users, site browsers and searchers alike? Well, then add it to this thread!

Be sure to specify if you're not using Bash and maybe name the script something useful.

Be sure to use the CODE tags, to keep things legible for future readers.

If you see a script and think you can improve on it, feel free to do so. If the author likes the edit, they can edit their own posts to include it. Hmm... Kinda like a pull request, I guess.

Other than specific questions regarding existing scripts, let's *try* to keep things on-topic.
 


SlowCoder

Gold Member
Gold Supporter
Joined
May 2, 2022
Messages
455
Reaction score
316
Credits
3,611
Alrighty then, I'll start this thread off with my little contributions.

For more details on the following scripts, follow the links.

bat.sh - Configurable battery stats in the terminal
ls-octal.sh - Display octal permissions for files in your current directory.
readlink-all.sh - Follow a symlink, displaying all links to the destination.
 

Fanboi

Well-Known Member
Joined
Apr 16, 2021
Messages
499
Reaction score
409
Credits
6,114
That really neat code in @SlowCoder's battery script will make something I posted earlier look messy, but anyway...

Delete/move/archive files older than a week
Barebones. Use it to build on. Sorry about style, my best mate burned it into me. I just neatened it up a little (on my phone, hope I didn't break it).
 
Last edited:

dancaer69

New Member
Joined
Mar 10, 2020
Messages
14
Reaction score
6
Credits
109
Recenty I created an autoshutdown script using dialog, to use remotely to a server pc.


Code:
#!/bin/bash
choise=$(dialog --clear --fb --title "Auto shutdown/reboot/suspend" --menu "Please choose an action:" 0 0 0 \
1 "Suspend " \
2 "Reboot " \
3 "Power off " 3>&1 1>&2 2>&3)

option="$choise"

time=$(dialog --stdout  --timebox "Input the desired time:" 0 0 00 00 00)

echo $time
IFS=":"
read -a strarr <<< "$time"
hours="${strarr[0]}"
mins="${strarr[1]}"
sec=10

htosec=$(($hours * 60 * 60))
#echo $htosec
mtosec=$(($mins * 60))
#echo $mtosec
newdt=$(($htosec + $mtosec + $sec))

newdt=$(date -d  "+$addtime seconds" "+%H:%M:%S")


while [ $hours -ge 0 ]; do
                 while [ $mins -ge 0 ]; do
                         while [ $sec -ge 0 ]; do
                                 dialog --cr-wrap --colors --infobox "The action \n will launch \nat \Z2$newdt\ZN \n\n\Zb   \Z1$hours:$mins:$sec" 8 18
                                 let "sec=sec-1"
                                 sleep 1
                         done
                         sec=59
                         let "mins=mins-1"
                 done
                 mins=59
                 let "hours=hours-1"
               
done

if [ "$option" = "Suspend" ]; then
    #echo "Suspend..."
    sudo systemctl suspend
elif [ "$option" = "Reboot" ]; then
    #echo "Reboot..."
    sudo systemctl reboot
elif [ "$option" = "Power off" ]; then
    #echo "Power off..."
    sudo systemctl poweroff
fi
 
C

CrazedNerd

Guest
Here's something I made given the default lack of a timer on Ubuntu, would be even more practical if I could turn this into a clickable desktop shortcut:

Code:
#! /bin/bash
 
 firefox https://www.timeanddate.com/timer/ 2>/dev/null

the redirection at the end is to free up the terminal for other uses and get rid of the worthless error message. Given the auto-complete of firefox (after a couple of visits, entering the URL would only mean entering "t"), you can still win interms of efficiency if you know how to make this into a clickable shortcut!

EDIT: removed line numbers from script to keep any coding novice from getting confused.
 
Last edited by a moderator:

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,516
Reaction score
1,245
Credits
11,069
Here's something I made given the default lack of a timer on Ubuntu, would be even more practical if I could turn this into a clickable desktop shortcut:

Code:
#! /bin/bash
 2
 3 firefox https://www.timeanddate.com/timer/ 2>/dev/null

the redirection at the end is to free up the terminal for other uses and get rid of the worthless error message. Given the auto-complete of firefox (after a couple of visits, entering the URL would only mean entering "t"), you can still win interms of efficiency if you know how to make this into a clickable shortcut!
You might want to check this: https://linuxconfig.org/time-countdown-bash-script-example
 

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,516
Reaction score
1,245
Credits
11,069
Here's one I use to back up my dot/config files to a USB drive:
Bash:
#!/bin/bash
DIRLIST=(~/.config ~/.local ~/.mozilla ~/.zoom ~/.claws-mail ~/.makagiga ~/.fonts ~/.gnome ~/.thunderbird)
DIRLIST1=(~/Documents ~/Downloads ~/Pictures ~/Videos ~/Appimages ~/bin ~/txts)
TARGET="/media/$USER/USB-Drive/Bullseye/dot_files"
TARGET1="/media/$USER/USB-Drive/Bullseye/"

cp -r -v -u "${DIRLIST[@]}" "$TARGET" && cp -r -v -u "${DIRLIST1[@]}" "$TARGET1"
 
C

CrazedNerd

Guest
Here's one I use to back up my dot/config files to a USB drive:
Bash:
#!/bin/bash
DIRLIST=(~/.config ~/.local ~/.mozilla ~/.zoom ~/.claws-mail ~/.makagiga ~/.fonts ~/.gnome ~/.thunderbird)
DIRLIST1=(~/Documents ~/Downloads ~/Pictures ~/Videos ~/Appimages ~/bin ~/txts)
TARGET="/media/$USER/USB-Drive/Bullseye/dot_files"
TARGET1="/media/$USER/USB-Drive/Bullseye/"

cp -r -v -u "${DIRLIST[@]}" "$TARGET" && cp -r -v -u "${DIRLIST1[@]}" "$TARGET1"
that's real neat, how do you change your flash drive label through the command line? It's pretty straightforward with Disks, a GUI Program, i've wondered how you could change the label from the command line.
 

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,516
Reaction score
1,245
Credits
11,069
how do you change your flash drive label through the command line?
Hmmm ... not sure really, since I don't use (too scared to try lol) CLI- tools for that task, the couple of times I did try, just ruined an install lol That being said, maybe parted, mkfs or cfdisk might just do it. However, why bother when you have so many handy GUIs around? I use partition manager from KDE.

EDIT: Well, I found this: https://help.ubuntu.com/community/RenameUSBDrive
:)
 
Last edited:
C

CrazedNerd

Guest
Hmmm ... not sure really, since I don't use (too scared to try lol) CLI- tools for that task, the couple of times I did try, just ruined an install lol That being said, maybe parted, mkfs or cfdisk might just do it. However, why bother when you have so many handy GUIs around? I use partition manager from KDE.

EDIT: Well, I found this: https://help.ubuntu.com/community/RenameUSBDrive
:)
each filesystem type has a different tool, how confusing!

I usually just do it after formatting through either those GUI tools or the file manager, but it would be interesting if you could change the label without erasing the drive.
 

gvisoc

Well-Known Member
Joined
May 29, 2020
Messages
476
Reaction score
562
Credits
4,819
Here's one I use to back up my dot/config files to a USB drive:
Bash:
#!/bin/bash
DIRLIST=(~/.config ~/.local ~/.mozilla ~/.zoom ~/.claws-mail ~/.makagiga ~/.fonts ~/.gnome ~/.thunderbird)
DIRLIST1=(~/Documents ~/Downloads ~/Pictures ~/Videos ~/Appimages ~/bin ~/txts)
TARGET="/media/$USER/USB-Drive/Bullseye/dot_files"
TARGET1="/media/$USER/USB-Drive/Bullseye/"

cp -r -v -u "${DIRLIST[@]}" "$TARGET" && cp -r -v -u "${DIRLIST1[@]}" "$TARGET1"
Why not with rsync?

Even if you don't have more machines to keep in sync with the thumbdrive, off the top of my head I think I remember (or maybe it's a fabricated memory) that had some extra functionality and better performance.
 

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,516
Reaction score
1,245
Credits
11,069

Fanboi

Well-Known Member
Joined
Apr 16, 2021
Messages
499
Reaction score
409
Credits
6,114
Not to put too fine a point on it, but..
Other than specific questions regarding existing scripts, let's *try* to keep things on-topic.
I made a discussion/chat thread here.
Guys, let's please use that for discussion. If you follow the OP there, it's pretty easy to keep everything in context and cohesive.
David, if you want to move these last few posts in there (this one, too -- or just delete this one) and edit your OP to reflect the new setup if you approve. You can chown that thread, too, if you like.

Hope that saves a lot of housekeeping.
 

Tolkem

Well-Known Member
Joined
Jan 6, 2019
Messages
1,516
Reaction score
1,245
Credits
11,069
python is technically a scripted language can we mention those ?
what about php ?
I'm no moderator, but the title does read: "User-created Helpful Shell Scripts", and the shell is just the interpreter, it's not limited to bash only. So, I think yes, scripts written in any scripting language that the shell can read and run, should be fine to post. Speaking of python, this is a nice post you might want to check:

 

captain-sensible

Well-Known Member
Joined
Jun 14, 2019
Messages
2,910
Reaction score
1,971
Credits
18,114
OP
K

KGIII

Super Moderator
Staff member
Gold Supporter
Joined
Jul 23, 2020
Messages
9,093
Reaction score
7,713
Credits
74,241
C

CrazedNerd

Guest
Here's a script for counting the number of "error", "failed", and "warning" in any file, intended for logs in /var/log...even though my system works well enough that i can use it for everything i need it for, clearly the people making Ubuntu are not perfect programmers!

Output:
Code:
grep: syslog: binary file matches
Warnings: 2560

grep: syslog: binary file matches
Errors: 4847

grep: syslog: binary file matches
Failures: 386983

lines with warnings: 254

lines with errors: 515

lines with failures: 38961

Script:
Code:
#!/bin/bash
#uses file as input and displays word count and lines with these
#words in the file

file=$1

warnings=$(grep warning $1 | wc -w)

echo -e "Warnings: $warnings\n"

errors=$(grep error $1 | wc -w)

echo -e "Errors: $errors\n"

failures=$(grep fail $1 | wc -w)

echo -e "Failures: $failures\n"

warnings=$(grep -c warning $1)

echo -e "lines with warnings: $warnings\n"

errors=$(grep -c error $1)

echo -e "lines with errors: $errors\n"

failures=$(grep -c fail $1)

echo -e "lines with failures: $failures\n"

given the file has over 87,000 lines, it would be interesting to know if there are any problems with this script since i'm obviously not going to go through the whole thing and count as the end count wouldn't be accurate anyway.

Question: does anyone know how to use sed or awk to suppress word-specific strings? I don't want it to say "grep: syslog: binary file matches" in the output.
 
Last edited by a moderator:
MALIBAL Linux Laptops

Linux Laptops Custom Built for You
MALIBAL is an innovative computer manufacturer that produces high-performance, custom laptops for Linux.

For more info, visit: https://www.malibal.com

Members online


Top