Monday, July 18, 2011

Rename Multiple Files At Once


Let’s say you have a directory with hundreds of files with the wrong file names, and you’d like to replace every filename containing test with prod. (this is a contrived example). We can easily do this with the “for” command in bash, combined with a little bit of bash goodness. Today we’ll learn how to replace text in a variable in a for loop.
The “for” command works like this:
for var in ;do $var;done
You can replace with any file match pattern, such as * or *.txt, and you can replace with any linux command. The command will be run in sequence on each of the files matched by the file match pattern.
This is where the bash variable handling makes it even more interesting. Instead of just doing something like “mv $var”, we can replace text in the filename using this syntax:
${var/originaltext/replacetext}
So now, if we run this command on our directory:
for f in *;do mv $f ${f/test/prod};done
For each file matched by *, bash will execute a command similar to this:
mv test.config prod.config
I’ve found that knowledge of the shell is invaluable when administering servers or just for managing your file collection, and has saved me hours of what would have otherwise been manual work.
And yes, I realize there are a number of tools that can accomplish renaming of multiple files.

I use it frequently for changing filename suffixes.

However, if you are stuck with filenames that contain spaces, you need to quote around the variable and the replacement expression, as shown in the example below.



for f in *;do mv “$f” “${f/\.oga/.ogg}”;done


Friday, June 24, 2011

Using 'rar' on Linux

To compress a big video file, movie.avi and split it into multiple files, each size up to 12MB,

A list of files (myarchive.part1.rar, myarchive.part2.rar, ..) will be created in current directory.

rar a -m5 -v12m myarchive movie.avi

You may change the compression quality, -m5 is the best and the slowest, while -m0 do no compression at all (-m3 is default).

If you prefer the old file naming style (myarchive.rar, myarchive.r00, myarchive.r01, ..), add one extra switch -vn before the archive name.

To uncompress the files (myarchive.part1.rar, myarchive.part2.rar, ..),

rar e myarchive.part1.rar

Thursday, June 16, 2011

How to install VNC server on an Ubuntu PC and remote from Windows PC


On the Ubuntu 9.10 PC

The Ubuntu 9.10 PC shall hereafter be referred to as the SERVER.
  1. First of all, permit remote desktop on the SERVER:
    From the menubar, select System | Preferences | Remote Desktop
    1. Under Sharing:
      1. Check Allow other users to view your desktop
      2. Check Allow other users to control your desktop
    2. Under Security:
      1. Select Require the user to enter this password
        and enter a password.
    3. Under Notification area:
      1. Select Only display an icon where there is somebody connected.
    4. Make a note of the network address and name of the SERVER as displayed in the Remote Desktop Preferences.
  2. Now ensure the required applications are installed:
    From the menubar, select System | Administration | Synaptic Package Manager
    1. In the quick search box, type vnc
    2. For each of the following four packages, right-click on them and select Mark for Installation
      1. vnc4server
      2. vnc4-common
      3. vinagre
      4. vino
    3. In the quick search box, type xinetd and install as described above.
    4. Finally, select Apply on the menubar to complete the installation.
  3. Now lets complete the VNC server install and configuration.
    Open up a command terminal:
    1. Run vncpasswd and enter the password that will be required by the client when attempting to connect to the SERVER
      vncpasswd
    2. Run the vncserver on display number 1 in order to generate the configuration files that we will subsequently customize.
      vncserver :1
    3. If you were to connect via remote desktop from a client at this point, the resolution would be wrong and the screen would be plain grey. Do not worry, we will fix that now. First, kill the vncserver process by issuing the command:
      vncserver -kill :1
    4. Edit the xstartup file in the folder .vnc (make a backup of the file first)
      gedit ~/.vnc/xstartup
    5. Ensure the file has the contents as follows:
    6. #!/bin/sh
      unset SESSION_MANAGER
      sh /etc/X11/xinit/xinitrc
  4. Finally, to run the vnc server on the SERVER (on display 1), issue the command:
    vncserver :1 -depth 16 -geometry 1028x1024

On the Windows PC

The Windows PC shall hereafter be referred to as the CLIENT.
  1. Download and install the VNC viewer of your cloice. I am using the free open source UltraVNC viewer.
    Note: UltraVNC is highly customizable.
  2. Run vncviewer.exe
    1. Enter in the network address of the SERVER pc (as recorded in step 1) immediately followed by a colon : and the display number. The following is an example of the format:
    2. 111.222.333.444:1
    3. Under Quick Options, select Auto.
    4. When prompted, enter the password specified in step 3.1
  3. Well Done! You are now connected over remote desktop to the Ubuntu PC from your Windows PC.

How to configure the VNC server to run at bootup on the Ubuntu PC.

In practice, you will not want to logon to the SERVER just to initiate the VNC server. Moreover, you may not even have a monitor for your Ubuntu Server, because you may be using the monitor as a dual screen for your primary desktop, as in my case. :)
These instructions have been referenced from here.

To that end I will now explain how to ensure the VNC server is initialised at bootup time (simply by switching on the SERVER). All of the following steps will required you to be logged in as root.
  1. Open up a terminal and login as root
    sudo -i
  2. Run all the commands outlined in step 3 (for the Ubuntu PC) as root.
  3. Create a new file in /etc/init.d called vncserver and ensure it has the following contents:
    Note: Change the username (and optionally the other parameters) according to your circumstances.
  4. #!/bin/sh -e
    ### BEGIN INIT INFO
    # Provides: vncserver
    # Required-Start: networking
    # Default-Start: S
    # Default-Stop: 0 6
    ### END INIT INFO
    PATH="$PATH:/usr/X11R6/bin/"
    # The Username:Group that will run VNC
    export USER="YOUR_USERNAME"
    #${RUNAS}
    # The display that VNC will use
    DISPLAY="1"
    # Color depth (between 8 and 32)
    DEPTH="16"
    # The Desktop geometry to use.
    #GEOMETRY="x"
    #GEOMETRY="800x600"
    #GEOMETRY="1024x768"
    GEOMETRY="1280x1024"
    # The name that the VNC Desktop will have.
    NAME="your-vnc-server"
    OPTIONS="-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"
    . /lib/lsb/init-functions
    case "$1" in
    start)
    su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
    ;;
    stop)
    su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
    ;;
    restart)
    $0 stop
    $0 start
    ;;
    esac
    exit 0

  5. Make the above script executable:
    sudo chmod +x /etc/init.d/vncserver
  6. Make this script into a system service by adding this script to the runtime scripts invoked at bootup time.
    sudo update-rc.d vncserver defaults
  7. To start the service without rebooting the SERVER:
    sudo /etc/init.d/vncserver start
  8. If you made a mistake/typo in the script, you can undo the creation of the system-wide service (step 5) by running the command:
    sudo update-rc.d -f vncserver removeYou may then correct your mistake and then repeat steps 5 and 6.
  9. Well done! You may now connect to your Ubuntu PC SERVER via remote desktop by simply powering on the SERVER and giving it enough time to bootup and run all of its system-wide services.