How to edit text in a string with sed

Say you’ve got a bunch of files to organize from something like mypic20190327.jpg to 2019/03/mypic20190327.jpg. sed is a nifty tool to help you with that. You’ll need to use regex to match each part, then reference each matched part in the result, and you could use extended regex to save you some escape characters. Since this approach, touches on several features of sed, I wanted to write it down.

for filename in $(ls); do dir=$(sed -r 's/([[:digit:]]{4})([[:digit:]]{2}).*/\1\/\2/g' <<< $filename); mkdir -p $dir; mv $filename $dir; done

First, we iterate all the files in the directory.

for filename in $(ls)

Then, we calculate the directory (e.g., 2019/03) from the filename by extracting the first 4 digits ([[:digit:]]{4}) into variable 1 and the next two digits ([[:digit:]]{2}) into variable 2. Note the use of parentheses to indicate variable storage.

Note the use of the -r flag in the command which indicates the use of extended regex so we don’t have to escape our parentheses and curly braces. If you’re not using GNU sed, you might use -E or something else instead.

sed -r

Note the .* at the end of the expression so our result replaces the entire line and not just the year and month parts.

([[:digit:]]{4})([[:digit:]]{2}).*

We refer to our stored variables using \1 and \2 in our result string to make a directory path year/month.

\1\/\2

Note the here-string syntax used to indicate sed should use the literal string as input instead of interpreting it as the name of the file from which to read and match data.

<<< $filename

Now that we have our directory name, we make it with mkdir using the -p flag to make any intermediary directories. And finally, we do the move.

How to completely dim brightness in XFCE

At least for me, the GUI brightness controls only allow the brightness to be dimmed so far.

$ cat /sys/class/backlight/intel_backlight/brightness 
618

By editing that file, you can bring it down further.

echo "1" | sudo tee /sys/class/backlight/intel_backlight/brightness

For me, the value 0 works fine as well, but it doesn’t appear to be any different than 1, and I get this weird feeling that some XFCE is going decide to black out the screen on 0 someday so I just play it safe with 1.

Signing your git commits

Install GPG.

sudo apt-get update && sudo apt-get install gnupg -y

Generate a GPG key.

gpg --full-generate-key

The default prompts are probably sufficient, but going for more security (like 4096-bit security) probably won’t hurt. After you get through creating the key, it’ll automatically be imported into your trusted key list.

Next, get your GPG key fingerprint.

gpg --fingerprint YOUR_GPG_KEY_NAME

Next, go into your Git project directory and configure it with your name, email, and GPG signing key.

git config user.name YOUR_NAME
git config user.email YOUR_EMAIL
git config user.signingkey YOUR_GPG_KEY_FINGERPRINT

You can sign your commits by adding the -S flag.

git commit -S

Or you can configure your project to sign your commits by default:

git config commit.gpgsign true

You can also add the --global flag to your git config commands to set the configuration globally instead of for just one project.

git config --global commit.gpgsign true

References

My $99 development Chromebook

This is inspired by Kenneth White’s $169 development Chromebook. This is mostly just some updates for 2019.

Last Black Friday, Wal-Mart had a Samsung Chromebook 3 for $99. It’s powered by a dual-core Intel Celeron with 4GB RAM, 16GB SSD, Bluetooth, 802.11n, SD card slot, two USB ports, webcam, and 10+ hours of battery runtime. I thought it would only be good for doing browser-based activities in CoderDojo, but it’s grown on me as a tablet replacement. Now, I’m hoping to even replace my notebook.

Following White’s advice, I made a Google account just for the device, but ultimately decided to go back to using my personal account because I wanted access to my Google Play purchases. But like White said, I can always Powerwash and use the device account when I travel to potentially hostile environments.

I changed my authentication to be through a hardware key (and Google Authenticator as a backup) which turns out to be very easy to use.

I’ve more or less gone with White’s guide in my build. In addition to his goals, I added a few:

  • Docker: I’m looking into this, but it looks like I might need a 32-bit x86 version to run under Termux.
  • ADB: I’m not even close to getting this working yet.

Some things to note:

  • It takes some getting used to Termux’s long-press on touchpad to copy/paste. There’s no CTRL+C/V options AFAIK.
  • If you store data in SD card, remember that it’s not encrypted by default.

Fix Cygwin SSH error: `Ignored authorized keys: bad ownership or modes for directory`

This error always gets me when I change my Cygwin home directory to my Windows home directory and I’m using Cygwin SSH with StrictModes enabled. Then, I have to go digging around the web for the solution. So the error looks like this:

$ ssh -v YOUR_REMOTE_HOST
### some stuff happening ###
debug1: Remote: Ignored authorized keys: bad ownership or modes for directory /cygdrive/c/Users/YOUR_USERNAME

For some reason, Windows makes user directories group-readable and StrictModes doesn’t like that. So you’ll have to do some permission editing:

chown YOUR_USERNAME:None /cygdrive/c/Users/YOUR_USERNAME
chmod 700 /cygdrive/c/Users/YOUR_USERNAME

There. Now you should be able to log in with pubkey authentication. Cheers!