Small collection of Bash scripts to launch functionalities in folders when new files appear, such as optimizing videos, converting images or battery management.
- Video optimizer: Folder that watches when new videos are added and optimizes them.
- Battery hook: Folder with custom scripts to be launched in different battery states.
- Image to AVIF: Folder that watches when new image (PNG, JPEG or WebP) are added and transform to AVIF format.
- Image to WebP: Folder that watches when new image (PNG or JPEG) are added and transform to WebP format.
Folder that watches when new videos are added and optimizes them.
ffmpeg
Example in Debian.
sudo apt install ffmpeg inotify-tools
curl -o bash-folders-video-optimizer https://raw.githubusercontent.com/tanrax/bash-folders/main/bash-folders-video-optimizer.sh && chmod +x bash-folders-video-optimizer && sudo rm -f /usr/local/bin/bash-folders-video-optimizer && sudo mv bash-folders-video-optimizer /usr/local/bin && echo "π Successfully installed! π"
Test
bash-folders-video-optimizer --help
bash-folders-video-optimizer --folder [folder to watch]
Example.
mkdir optimizer
bash-folders-video-optimizer --folder optimizer
And leave a video that you want to optimize in the folder optimizer
.
Create a file in /etc/systemd/system/bash-folders-video-optimizer.service
with the following content.
[Unit]
Description=Folder that watches when new videos are added and optimizes them.
[Service]
Restart=always
RestartSec=5
User=[user]
ExecStart=bash-folders-video-optimizer --folder [folder to watch]
[Install]
WantedBy=multi-user.target
Edit it to your needs.
Recharge services.
sudo systemctl daemon-reload
And activate it.
sudo systemctl enable bash-folders-video-optimizer
sudo systemctl start bash-folders-video-optimizer
Open.
crontab -e
Add to document.
@reboot bash-folders-video-optimizer --folder [folder to watch] >/dev/null 2>&1 &
Folder with custom scripts to be launched in different battery states.
The filename of the scripts, or your custom scripts, must be:
discharging
: When the battery is in use.charging
: When the battery is charging.low
: When it reaches the low percentage. Default 20.high
: When it reaches the high percentage. Default 80.full
: When the battery is full.
They must have execution permissions. If any of them do not exist, they will be ignored.
curl -o bash-folders-battery-hook https://raw.githubusercontent.com/tanrax/bash-folders/main/bash-folders-battery-hook.sh && chmod +x bash-folders-battery-hook && sudo rm -f /usr/local/bin/bash-folders-battery-hook && sudo mv bash-folders-battery-hook /usr/local/bin && echo "π Successfully installed! π"
Test
bash-folders-battery-hook --help
bash-folders-battery-hook --folder [folder path]
Example.
mkdir battery-scripts
bash-folders-battery-hook --folder battery-scripts
Inside the folder all the empty scripts will be created, which you will have to edit to include the instructions in Bash.
Create a file in /etc/systemd/system/bash-folders-battery-hook.service
with the following content.
[Unit]
Description=Folder with custom scripts to be launched in different battery states.
[Service]
Restart=always
RestartSec=5
User=[user]
ExecStart=bash-folders-battery-hook --folder [folder path]
[Install]
WantedBy=multi-user.target
Edit it to your needs.
Now you will need the script to run every so often to check the battery status. The best solution is to create a timer
.
Create a file in /etc/systemd/system/bash-folders-battery-hook.timer
with the following content.
[Unit]
Description=Folder with custom scripts to be launched in different battery states every minute.
[Timer]
OnCalendar=*-*-* *:*:00
Persistent=true
[Install]
WantedBy=timers.target
Recharge services.
sudo systemctl daemon-reload
And activate it.
sudo systemctl enable bash-folders-battery-hook.timer
sudo systemctl start bash-folders-battery-hook.timer
Open.
crontab -e
Add to document. Collaborations & Pull Requests
* * * * * bash-folders-battery-hook --folder [folder path]
Folder that watches when new image (PNG, JPEG or WebP) are added and transform to AVIF format.
avifenc
Example in Debian.
sudo apt install libavif-bin
curl -o bash-folders-image-to-avif https://raw.githubusercontent.com/tanrax/bash-folders/main/bash-folders-image-to-avif.sh && chmod +x bash-folders-image-to-avif && sudo rm -f /usr/local/bin/bash-folders-image-to-avif && sudo mv bash-folders-image-to-avif /usr/local/bin && echo "π Successfully installed! π"
Test
bash-folders-image-to-avif --help
bash-folders-image-to-avif --folder [folder to watch]
Example.
mkdir image-to-avif-converter
bash-folders-image-to-avif --folder image-to-avif-converter
And leave a image that you want to optimize in the folder image-to-avif-converter
.
Create a file in /etc/systemd/system/bash-folders-image-to-avif.service
with the following content.
[Unit]
Description=Folder that watches when new image (PNG, JPEG or WebP) are added and transform to AVIF format.
[Service]
Restart=always
RestartSec=5
User=[user]
ExecStart=bash-folders-image-to-avif --folder [folder to watch]
[Install]
WantedBy=multi-user.target
Edit it to your needs.
Recharge services.
sudo systemctl daemon-reload
And activate it.
sudo systemctl enable bash-folders-image-to-avif
sudo systemctl start bash-folders-image-to-avif
Open.
crontab -e
Add to document.
@reboot bash-folders-image-to-avif --folder [folder to watch] >/dev/null 2>&1 &
Folder that watches when new image (PNG or JPEG) are added and transform to WebP format.
webp
Example in Debian.
sudo apt install webp
curl -o bash-folders-image-to-webp https://raw.githubusercontent.com/tanrax/bash-folders/main/bash-folders-image-to-webp.sh && chmod +x bash-folders-image-to-webp && sudo rm -f /usr/local/bin/bash-folders-image-to-webp && sudo mv bash-folders-image-to-webp /usr/local/bin && echo "π Successfully installed! π"
Test
bash-folders-image-to-webp --help
bash-folders-image-to-webp --folder [folder to watch]
Example.
mkdir image-to-webp-converter
bash-folders-image-to-webp --folder image-to-webp-converter
And leave a image that you want to optimize in the folder image-to-webp-converter
.
Create a file in /etc/systemd/system/bash-folders-image-to-webp.service
with the following content.
[Unit]
Description=Folder that watches when new image (PNG or JPEG) are added and transform to WebP format.
[Service]
Restart=always
RestartSec=5
User=[user]
ExecStart=bash-folders-image-to-webp --folder [folder to watch]
[Install]
WantedBy=multi-user.target
Edit it to your needs.
Recharge services.
sudo systemctl daemon-reload
And activate it.
sudo systemctl enable bash-folders-image-to-webp
sudo systemctl start bash-folders-image-to-webp
Open.
crontab -e
Add to document.
@reboot bash-folders-image-to-webp --folder [folder to watch] >/dev/null 2>&1 &
You must provide the documentation, as well as the scripts present, test that it works well and the script must pass a shellcheck
(below you will find an example of execution).
shellcheck [script]