Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

July 07, 2008

How-To: Mass-Rename Files Using an Easy Method

You may find this useful to rename MP3 or image files in a directory, so they will all look the same.

The following script will work on directories with less than 100 files of the same type, and will rename all of them to file01, file02, file03, ... and so on.
j=1; 
for i in *; do

if [[ $j -lt 10 ]]; then
mv "${i}" "file0${j}";
fi;
if [[ $j -ge 10 && $j -lt 100 ]]; then
mv "${i}" "file${j}";
fi;
j=$(($j+1));
done
You can only use it for certain types of files in a directory, like this:
j=1;
for i in *.ogg; do
if [[ $j -lt 10 ]]; then
mv "${i}" "audio0${j}.ogg";
fi;
if [[ $j -ge 10 && $j -lt 100 ]]; then
mv "${i}" "audio${j}.ogg";
fi;
j=$(($j+1));
done
This will rename all the Ogg files in a directory to audio01.ogg, audio02.ogg, audio03.ogg, and so on. You can test it for whatever files you like, however make a back-up first, since you can accidentally rename files or even directories which you don't want renamed.

Updated: Jul 07, 2008 (Created: Jul 07, 2008)

May 27, 2008

How-To: Make a Simple Amarok Now Playing Script for Konversation in Bash

Konversation comes with a very good now playing script residing in /usr/share/apps/konversation/scripts/media. It's made in Python and has support for many audio players, among these being Amarok, JuK or XMMS. However, if you only use Amarok and want a real simple now playing script, you can make one in Bash. Just create the file anp.bash in your ~/.kde/share/apps/konversation/scripts/ directory, chmod 755 it and copy the following in it:

#!/bin/bash

PORT=$1;
SERVER=$2;
TARGET=$3;

nowPlaying="$(dcop amarok default nowPlaying)"
out="Rocks! $nowPlaying"

dcop $PORT default say $SERVER "$TARGET" "$out"


Then open Konversation and go to Settings -> Configure Konversation... -> Command Aliases and add anp as the Alias and /exec anp.bash as the Replacement. You can execute it with the command /ANP in any channel or query window. Note that the example is a very basic one, so if Amarok is not running the message which will appear will only be 'Rocks! '.

Updated: Jun 23, 2008 (Created: May 27, 2008)

May 24, 2008

How-To: Have a Welcome Message Each Time You Open a Shell

Updated: May 24, 2008

First, create a script which will echo the messages you want to be displayed when you open your shell. For example, create the script 'hello_user' with the following content:

#!/bin/bash

clear
echo -ne "Hello, $USER! Today is $(date).\n\n"
cal
echo -ne "System Info: $(uptime)\n\n"
echo -ne "Fortune of the Day:\n"
/usr/games/fortune

The above script welcomes the user, shows the current date and system info, and then prints a random fortune. Next, put this script into a directory, say ~/bin/, and make sure it is in your $PATH. To do that, edit ~/.bashrc and add the following line:

PATH=~/bin:$PATH

This will add the directory ~/bin to your $PATH.

Next, edit ~/.bashrc and add the command which will run your script:

hello_user

And this is it. Next time you'll open a shell, a message which looks like the one below will be displayed:


The idea to make this tutorial came after I read this post on UbuntuForums.

How-To: Create Your Own Aliases

Aliases are custom commands which can be used to make the work with the shell easier and faster. For example, if your current working directory is /usr/bin and you want to quickly go to /var/cache/apt/archives you can do a 'cd /var/cache/apt/archives'. If you want to save time, you may want to type only a command such as 'cache' or 'debs. Or you may want to update your Debian system in one command, so instead of typing 'su -c "apt-get update && apt-get upgrade"' you only type 'upgrade' or something of your choice. This is where aliases come to help.

In this How-To I will explain two ways of creating aliases.

Method 1
The first method is to add aliases directly into your ~/.bashrc file. The format should be:

alias name='command'

For example you can add something like:

alias ll='ls -l'
alias deb='cd /var/cache/apt/archives'
alias dld='cd ~/downloads'

alias upgrade='su -c "apt-get update && apt-get upgrade"'

Open a console and try your aliases. For example if you type 'deb', the current working directory will be changed to /var/cache/apt/archives.

Method 2
The second method lets you make a separate aliases file, so you won't have to put them in ~/.bashrc, but to a file of your choice. First, edit your ~/.bashrc file and add or uncomment the following lines:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

Save it and close the file. After that, all you have to do is create a ~/.bash_aliases file and add your aliases there, with the same format specified at the first method.