Archive for the ‘bash’ tag
Bash script for editing the hosts file
I just read a post about local development in apache by Jesper Rasmussen and thought one thing was missing. I usually test my sites and applications locally with the real domain – to ensure that functionality based on the url works as expected. However this means editing the hosts file several times a day. Now I have made a small shellscript that will add and remove lines from the hosts file.
#! /bin/bash
DEFAULT_IP=127.0.0.1
IP=${3:-$DEFAULT_IP}
case "$1" in
add)
echo "$IP $2" >> /etc/hosts
;;
remove)
sed -ie "\|^$IP $2\$|d" /etc/hosts
;;
*)
echo "Usage: "
echo "hosts.sh [add|remove] [hostname] [ip]"
echo
echo "Ip defaults to 127.0.0.1"
echo "Examples:"
echo "hosts.sh add testing.com"
echo "hosts.sh remove testing.com 192.168.1.1"
exit 1
;;
esac
exit 0
As you can see the script defaults the ip to 127.0.0.1 for easy creation of local domains. Next step is to create a quicksilver (and a gnome do) plugin for easy creation without ever touching the terminal. (Even though we all love the terminal, right?)
Converting Filenames to Lowercase with Bash
Once again I have a bash-oneliner. For some reason it is all I blog about at the moment.
I had an import script for importing demo files (in mp3 format) to a database used at Voicearchive. Only problem the import script depended on the files being named precisely according to a specific rule. But for some reason some times it was named with uppercase letters, and sometimes with lowercase letters. I decided to rename all files to lowercase, and change the importscript accordingly.
find . -maxdepth 1 -type f -execdir rename 'y/A-Z/a-z/' '{}' \;
Bash Oneliner for Counting Files
I needed to find a way to count the number of files in a directory and only output that number. Bash is still my best friend for this sort of thing – especially with a little help from google.
ls -1 | wc -l
by the way – the answer was 2062 files.
Renaming file extensions with bash script
A couple of weeks ago I had to rename the file extension of several view scripts from html (from my own mvc framework) to phtml (zend framework). I found, as always, bash to be my friend.
for i in * do ( file $i | grep html ) && mv $i $(echo $i | sed s/html$/phtml/) done
How I sorted All My Mp3′s With a Simple Bash Script
First of all, the headline is a lie. I am in the process of copying all mp3 files that we in our household have had on a number of different machines to one central server, which all computers talk to via webdav. But since we have had a lot of different naming schemes and ways of sorting files we needed some sort of uniform naming of files and directories. We decided on the ever so nice /Artist/Album/01_Title.mp3. But since we have a lot of mp3′s, this seemed a daunting task.
In comes bash. 6 lines of code is all it takes.
#!/bin/bash
end="/destination/dir/of/mp3s"
find -type f -name *.mp3 -print0 | while read -d $'\0' file; do
mkdir --parent "$end"/"$(mp3info -f -p %a "$file")"/"$(mp3info -f -p %l "$file")"
mv "$file" "$end"/"$(mp3info -f -p %a "$file")"/"$(mp3info -f -p %l "$file")"/"$(mp3info -f -p %n "$file")"_"$(mp3info -f -p %t "$file")".mp3
done
The small program mp3info is used to extract id3 tags from the files. (Fortunately they are entered for almost all files). I am not the greatest bash-hacker in the world, so there might be a better way to do this. But I have tested this, and it works, and is quite fast.
If you know of a better way to do this, please comment. I would love to learn.