This post has been rewritten and republished in 2016.
Category: bash
Record and share shell activity via shelr.tv

Shelr.tv allows Unix/Linux command line users to record something interesting from their terminal and share it to followers.
It is a bit like YouTube for plain text shellcasts. A great feature is that you can copy and paste everything you see.
A nice intro with interesting comments from one of the core developers can be found on linuxaria.com.
A Debian package has been proposed through the Debian “package mentor” system.
Backup/restore XFCE desktop icons
Sometimes the XFCE desktop icons get messed up, for example by games that temporarily change the screen resolution to 800×600.
A solution to this problem has been mentioned here. It suggests using “sudo chattr +i” to lock the config file where XFCE stores the icon positions.
Alternatively (and without the repeated need for sudo and chattr) you can also backup and restore the ~/.config/xfce4/desktop/icons* file(s) like this:
Create a script /usr/local/bin/save-xfce-desktop-icons.sh like this:
#! /bin/sh mkdir -p ~/.config/xfce4/desktop.bak cp -f ~/.config/xfce4/desktop/icons* ~/.config/xfce4/desktop.bak
Create another script /usr/local/bin/load-xfce-desktop-icons.sh like this:
#! /bin/sh cp -f ~/.config/xfce4/desktop.bak/icons* ~/.config/xfce4/desktop
Make the scripts executable like this
sudo chmod ugo+x /usr/local/bin/save-xfce-desktop-icons.sh sudo chmod ugo+x /usr/local/bin/load-xfce-desktop-icons.sh
Then in the XFCE start menu, go to “Settings” – “Keyboard” – “Application Shortcuts” and configure 2 keyboard shortcuts:
| Command | Shortcut |
|---|---|
| save-xfce-desktop-icons.sh | <Control><ALT>S |
| load-xfce-desktop-icons.sh | <Control><ALT>L |
Then you will be able to backup and restore your icons like this:
- Backup: Press F5 then <Control><ALT>S then F5
- Restore: Press F5 then <Control><ALT>L then F5
The F5 is necessary to synchronize what you see on the screen with the content of ~/.config/xfce4/desktop/icons*.
Shell script to generate index.html (directory listing)
A simple shell script – let’s call it index-html.sh – to turn a list of file names into html links:
#!/bin/sh echo '<html><body>' sed 's/^.*/<a href="&">&<\/a><br\/>/' echo '</body></html>'
Example use:
ls | index-html.sh > index.html
bash script to recursively sanitize folder and file names
Below is a bash script to recursively sanitize folder and file names. It leaves all numbers, letters, dots, hyphens and underscores untouched, but replaces all other characters with underscores.
#! /bin/bash
sanitize() {
shopt -s extglob;
filename=$(basename "$1")
directory=$(dirname "$1")
filename_clean="${filename//+([^[:alnum:]_-\.])/_}"
if (test "$filename" != "$filename_clean")
then
mv -v --backup=numbered "$1" "$directory/$filename_clean"
fi
}
export -f sanitize
find $1 -depth -exec bash -c 'sanitize "$0"' {} \;
Simple webradio playback
I listen to web radio stations but I don’t want to use any player ui for that. All I want is:
- Select a station from a list of my favorites and listen to it
- Be able to stop current web-radio playback
- Never have more than one station playing at the same time
I do it like this:
- For each radio station save a playlist file (*.pls, *.m3u or sometimes *.asx) in a folder called “radio” on my local machine. I download most of them from the shoutcast or icecast stream directories. I also add one special (empty) file called “none.pls” (which serves to turn off all radio).
- Add a toolbar to the taskbar that lists the content of the radio folder, i.e. all the webradio playlist files as clickable items. In XFCE add a “Directory Menu” item to the panel.
- Configure the default app for the playlist mime types mentioned above to be my bash script “radio.sh”. It kills any existing webradio playback and plays the selected playlist file. See below for how to configure mime-type association defaults.
- Install mpv – the de-facto successor of the now dormant mplayer – to do the actual playback.
This is my little “radio.sh” script (requires the pkill and mpv commands):
#! /bin/bash pkill -f "mpv --playlist" mpv -playlist "$@"
To set this script as the default handler for the most common playlist file types, put the following into ~/.local/share/applications/defaults.list:
[Default Applications] audio/x-mpegurl=radio.sh.desktop audio/x-scpls=radio.sh.desktop
Make sure you have a file “radio.sh.desktop” in ~/.local/share/applications or in /usr/local/share/applications with contents like this:
[Desktop Entry] Exec=radio.sh %U MimeType=audio/x-mpegurl;audio/x-scpls;video/x-ms-asf Name=radio.sh StartupNotify=false Terminal=false Type=Application
If I have to use M$ Windows then I do something similar using a taskbar toolbar for the radio folder and the VLC player, configured to run minimized as systray icon.
Bash script to remove CSS from epub
I have a Kobo Reader to read epub eBooks. It is a first generation Kobo and has a flaw: If the epub file contains a CSS stylesheet, the reader (that’s me) cannot adjust the font size via Kobo’s font selection.
Since epub is just zipped HTML with some metadata and conventions, I was able to fix the problem by removing all CSS files from the offending epub. I don’t want any fancy “style” or formatting for the books that I read anyway.
Here is the bash script that I use to automate the task (requires the zip/unzip commands):
file=$1
if [[ $file != *.epub ]]
then
echo "Usage: $(basename $0) something.epub"
exit 0
fi
folder=$(basename "$file" .epub)
mkdir "$folder"
unzip -d "$folder" "$file"
find "$folder" -name *.css -exec rm {} \;
cd "$folder"
zip -r ../"$folder"_nocss.epub .
cd ..
rm -rf "$folder"