Code Snippets

This page is here to remind me of the code snippets that I sometimes need.

I don’t guarantee they will work for you so please use at your own risk!

Rename files

This snippet will rename all .jpg files in a folder with a new random 10 character filename. There is no checking for duplicates. I.E. test.jpg will become 3euy54ms63.jpg

for fname in `find . -type f`; do mv "$fname" $(echo "$fname" | < /dev/urandom tr -dc A-Za-z0-9_ | head -c8 | cut -f1 -d' ').jpg; done

Set a users password using BASH script

This bash snippet will take a username stored in $USER and password stored in $PASS then change the users password. Useful if you are creating a new user inside a BASH script. Needs "expect" to be installed on the server.

# Set users password
echo -e "Setting users password"
echo -e "WAIT! Do not answer the prompts!"
expect << EOF
spawn passwd $USER
expect "New Password:"
send "${PASS}\r"
expect "Retype New Password:"
send "${PASS}\r"
expect eof;

Search and replace in a mysql table

A bit of SQL code ideal for updating wordpress databases when moving content to a new domain name. I used it to update all the links in the database to the new domain name.

UPDATE `wp_posts` SET `post_content` = replace(post_content, '', '')

Install W3m and Ncurses on a RPi AllStar Node

Update Pacman

pacman -Sy

Install W3m

pacman -S w3m

Install Ncurses

pacman -S ncurses

Create link to new lib

cd /usr/lib
ln -s