How To

Generate Files with Random Content and Size in Bash

Occasionally you need to generate a bunch of random files with random content, usually for testing compression, user quotas or miscellaneous stuff.

Here’s one way, using the bash shell and a few handy linux utilities.

  1. The bash $RANDOM function. It generates a random number between 0 – 32767.
  2. Linux DD utility, to output files.
  3. /dev/(h|s)da, your hard drive in linux.
  4. All wrapped in a bash while loop.

So lets start. First define a bash variable with the number of files we wish to create, lets say 10.

no_of_files=10

Then we’ll assign the a bash variable for the counter.

counter=1

As for the dd command, this creates a file with random content from your hard disk. (Mine’s /dev/sda) which is 1KB in size. The count switch tells dd to repeat 1024 bytes 1 time, thus the 1Kb file size. skip makes dd skip an x amount of bytes before reading further. Since this requires raw access to your hard drive, you’ll have to run this as root unfortunately. :(

dd bs=1024 count=1 skip=0 if=/dev/sda of=random-file

So now imagine, if we let bash assign the count and skip random numbers, we get random file contents. (Light bulbs flashing eh?)

Of course, all of it will be written to a single file called random-file in my case. To add just slight amount of variety, we can add the counter variable that we will use in our while loop as an extension. The dd command will now be:-

 dd bs=1024 count=$($RANDOM) skip=$($RANDOM) if=/dev/sda of=random-file.$counter

Finally, we will wrap it up in a bash while loop like this:-

no_of_files=10;
counter=1;
while [[ $counter -le $no_of_files ]];
 do echo Creating file no $counter;
  dd bs=1024 count=$RANDOM skip=$RANDOM if=/dev/sda of=random-file.$counter;
  let "counter += 1";
 done

When you run it, you will get output like this:-

Creating file no 1
16614+0 records in
16614+0 records out
17012736 bytes (17 MB) copied, 0.29308 s, 58.0 MB/s
Creating file no 2
14456+0 records in
14456+0 records out
14802944 bytes (15 MB) copied, 0.100101 s, 148 MB/s
.................
Creating file no 10
25224+0 records in
25224+0 records out
25829376 bytes (26 MB) copied, 0.492113 s, 52.5 MB/s

when you do a directory listing, you’ll see this:-

[root@atreides rd-test]# ls -lh
total 226M
-rw-r--r-- 1 root root 17M 2009-07-29 00:25 random-file.1
-rw-r--r-- 1 root root 25M 2009-07-29 00:25 random-file.10
-rw-r--r-- 1 root root 15M 2009-07-29 00:25 random-file.2
-rw-r--r-- 1 root root 20M 2009-07-29 00:25 random-file.3
-rw-r--r-- 1 root root 21M 2009-07-29 00:25 random-file.4
-rw-r--r-- 1 root root 30M 2009-07-29 00:25 random-file.5
-rw-r--r-- 1 root root 22M 2009-07-29 00:25 random-file.6
-rw-r--r-- 1 root root 27M 2009-07-29 00:25 random-file.7
-rw-r--r-- 1 root root 25M 2009-07-29 00:25 random-file.8
-rw-r--r-- 1 root root 29M 2009-07-29 00:25 random-file.9

For more info, refer to the $RANDOM function from the Advanced Bash Scripting Guide.

How to Enable Flash in Google Chrome for Fedora 11 i686

So you’ve installed the latest development builds of Chromium for Fedora Linux. The only thing lacking is that youtube is not loading. If not, what are you waiting for? Refer to one of my previous posts.

First make sure you install the Adobe yum repository.

As root:-

rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-i386-1.0-1.noarch.rpm

Then install the flash plugin.

yum install flash-plugin.i386

The flash plugin will be installed in /usr/lib/flash-plugin/libflashplayer.so

Chrome plugins are supposed to be located in /usr/lib/chromium-browser/plugins. So lets create a symbolic link, so that whenever your Adobe flash plugin is updated, your Chrome automatically gets the latest version.

n -sv /usr/lib/mozilla/plugins/libflashplayer.so /usr/lib/chromium-browser/plugins/libflashplayer.so

Now start your Chrome browser from the command line, with the --enable-plugins switch. This will enable the flash plugin.

Here’s a screenshot of me watching a video from youtube on chrome:-

Flash Player in Google Chrome for Linux

Flash Player in Google Chrome for Linux

Say goodbye to geek productivity once this is done :D

Temporarily Disable a Yum Repository

Sometimes, you may run into problems updating Fedora via yum. What you could do is temporarily disable the offending repository, and update everything else while the errors are being fixed upstream.

  1. List out your enabled yum repositories.
    $ yum repolist

    You would get something like this:

    [raja@atreides ~]$ yum repolist
    Loaded plugins: fastestmirror, presto, refresh-packagekit
    repo id                   repo name                              status
    adobe-linux-i386          Adobe Systems Incorporated             enabled:     17
    chromium                  Chromium Test Packages                 enabled:      7
    fedora                    Fedora 11 - i386                       enabled: 13,289
    rpmfusion-free            RPM Fusion for Fedora 11 - Free        enabled:    377
    rpmfusion-free-updates    RPM Fusion for Fedora 11 - Free - Upda enabled:    210
    rpmfusion-nonfree         RPM Fusion for Fedora 11 - Nonfree     enabled:    110
    rpmfusion-nonfree-updates RPM Fusion for Fedora 11 - Nonfree - U enabled:    115
    updates                   Fedora 11 - i386 - Updates             enabled:  3,451
    repolist: 17,576
  2. The names on the left are the repo ids. To disable a particular one, for example rpmfusion-nonfree-updates while doing updates, run your yum like this:-
    # yum upgrade --disablerepo=rpmfusion-free-updates
  3. This will help you upgrade the rest of the packages in the mean time.

Configure a Caching-Only Name Server in a Chroot Environment for Fedora 11

Having a caching only name-server on your local Machine speeds up your browsing. Here’s how to set up a slightly more secure caching server using ISC Bind in Fedora 11.

  1. Install bind and bind-chroot packages
    # yum install bind bind-chroot
  2. Edit your /etc/sysconfig/named file.
    # vim /etc/sysconfig/named

    Add the following line:

    ROOTDIR="/var/named/chroot"

  3. Edit your /etc/named.conf file.
    # vim /etc/named.conf
  4. Change the following line:
    listen-on port 53 { 127.0.0.1; };

    to

    listen-on port 53 { any; };

    This allows the bind daemon to listen on all your network IPs, not just your loopback(127.0.0.1) address.

  5. Change this line:
    allow-query     { localhost; };

    to

    allow-query     { 192.168.0.0/24; };

    You now allow all the machines in your home LAN to use your DNS server.

  6. Make sure it starts at boot time.
    # chkconfig named on

    Restart your DNS server.

    # service named restart
  7. Make sure its listening on the correct ports.
    # netstat -ntupl | grep named

    In my case, the DNS server IP is 192.168.0.10. So, as seen here, the line udp 0 0 192.168.0.10:53 0.0.0.0:* 2851/named shows it is listening correctly.

  8. Then test your server from another machine in your network. Most probably another linux box or laptop.
    # dig @192.168.0.10 google.com

    The dig command, with the ‘@’ instructs it to get the IP address for google.com from your newly set up server. On my machine, it looked like this:-

    [root@atreides ~]# dig @192.168.0.10 google.com
     
    ; < <>> DiG 9.6.1-RedHat-9.6.1-2.fc11 < <>> @192.168.0.10 google.com
    ; (1 server found)
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER< <- opcode: QUERY, status: NOERROR, id: 6515
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 4, ADDITIONAL: 0
     
    ;; QUESTION SECTION:
    ;google.com.			IN	A
     
    ;; ANSWER SECTION:
    google.com.		300	IN	A	74.125.127.100
    google.com.		300	IN	A	74.125.45.100
    google.com.		300	IN	A	74.125.67.100
     
    ;; AUTHORITY SECTION:
    google.com.		171853	IN	NS	ns3.google.com.
    google.com.		171853	IN	NS	ns1.google.com.
    google.com.		171853	IN	NS	ns2.google.com.
    google.com.		171853	IN	NS	ns4.google.com.
     
    ;; Query time: 82 msec
    ;; SERVER: 192.168.0.10#53(192.168.0.10)
    ;; WHEN: Sat Jul 18 20:14:59 2009
    ;; MSG SIZE  rcvd: 148

    Note the SERVER: line. that shows you the answer for the query came from my DNS server (192.168.0.10).

  9. Finally, set up your /etc/resolv.conf accordingly.

    On the server:

    nameserver 127.0.0.1

    And on all your other machines:

    nameserver 192.168.0.10

How to Mass Rename Files in Linux

When entrusted with the chore of renaming multiple file, the convenience of a script shines. After all, we ain’t robots designed to do just one thing. Today, I’ll show one method of renaming files, using a for loop in bash.

First, the task we are going to do:

We have a list of files:

[raja@atreides test]$ ls
data_file_1   data_file_2  data_file_4  data_file_6  data_file_8
data_file_10  data_file_3  data_file_5  data_file_7  data_file_9

We have been instructed to replace the underscores (_) in the file names with hyphens (-).

A menial task not really suited for humans. We’ll start building a script for this, one piece at a time.

  1. Check which type of shell you are using.
    echo $SHELL

    As I am using bash scripting, make sure the result is /bin/bash

  2. List out all the file that you wish to rename
    $ ls data_file_*

    It should give you output like this:-

    [raja@atreides test]$ ls data_file_*
    data_file_1   data_file_2  data_file_4  data_file_6  data_file_8
    data_file_10  data_file_3  data_file_5  data_file_7  data_file_9
  3. Lets display each one of this in a for loop.
    for file_name in `ls data_file_*`; do echo "The file:" $file_name; done

    What this for loop does is take each of the file names given by the ls command and store it in the $file_name bash variable. You’ll see this:-

    [raja@atreides test]$ for file_name in `ls data_file_*`; do echo "The file:" $file_name; done
    The file: data_file_1
    The file: data_file_10
    The file: data_file_2
    The file: data_file_3
    The file: data_file_4
    The file: data_file_5
    The file: data_file_6
    The file: data_file_7
    The file: data_file_8
    The file: data_file_9
  4. Now that we’ve stored the file names in variables, we can use the sed command to edit it, replacing the underscores with a hyphen. I’ll echo the file name and pipe it to sed, to make sure it replaces the file name, and not modify the files itself.

    I used the sed replace command, s with a global option behind, the g, to make sure it replaces all occurrences of the underscore. Without the ‘g’, sed will only replace the first underscore and ignore the rest.

    for file_name in `ls data_file_*`; do echo "The file:" $file_name; echo $file_name | sed 's/_/-/g' ; done

    When you run this command in your console, you will see this output:

    [raja@atreides test]$ for file_name in `ls data_file_*`; do echo "The file:" $file_name; echo $file_name | sed 's/_/-/g' ; done
    The file: data_file_1
    data-file-1
    The file: data_file_10
    data-file-10
    The file: data_file_2
    data-file-2
    The file: data_file_3
    data-file-3
    The file: data_file_4
    data-file-4
    The file: data_file_5
    data-file-5
    The file: data_file_6
    data-file-6
    The file: data_file_7
    data-file-7
    The file: data_file_8
    data-file-8
    The file: data_file_9
    data-file-9
  5. Of course, all this script is doing right now is editing the file name in the $file_name variable itself. Even though you can see what the modified file name will look like, we have not actually renamed the files on your hard disk yet. (This can be a good method of testing your sed-fu, before you really do it ;) )
  6. In order to actually rename the file, I’ll put the results of sed’s transformation in a variable called $new_file_name.I’ll also print this out to the terminal, so you can see the changes that are about to take place.
    for file_name in `ls data_file_*`; do echo "The file:" $file_name; new_file_name=$(echo $file_name | sed 's/_/-/g'); echo "New name:" $new_file_name ; done

    You’ll then see this being output on the terminal:

    [raja@atreides test]$ for file_name in `ls data_file_*`; do echo "The file:" $file_name; new_file_name=$(echo $file_name | sed 's/_/-/g'); echo "New name:" $new_file_name ; done
    The file: data_file_1
    New name: data-file-1
    The file: data_file_10
    New name: data-file-10
    The file: data_file_2
    New name: data-file-2
    The file: data_file_3
    New name: data-file-3
    The file: data_file_4
    New name: data-file-4
    The file: data_file_5
    New name: data-file-5
    The file: data_file_6
    New name: data-file-6
    The file: data_file_7
    New name: data-file-7
    The file: data_file_8
    New name: data-file-8
    The file: data_file_9
    New name: data-file-9
  7. Now that we’ve got both the old AND new file names, nicely stored in variables, its time to actually rename them using the mv command. The syntax for renaming is mv current_file_name new_file_name. I think you’re starting to see where these variables come in handy. :D
    for file_name in `ls data_file_*`; do echo "The file:" $file_name; new_file_name=$(echo $file_name | sed 's/_/-/g'); echo "New name:" $new_file_name ; mv -v $file_name $new_file_name; done

    I’ve added a -v to the mv, so it displays what it is changing. As I always say, a little more verbosity is always good, especially on the terminal. Your final output will be something like this:-

    [raja@atreides test]$ for file_name in `ls data_file_*`; do echo "The file:" $file_name; new_file_name=$(echo $file_name | sed 's/_/-/g'); echo "New name:" $new_file_name ; mv -v $file_name $new_file_name; done
    The file: data_file_1
    New name: data-file-1
    `data_file_1' -> `data-file-1'
    The file: data_file_10
    New name: data-file-10
    `data_file_10' -> `data-file-10'
    The file: data_file_2
    New name: data-file-2
    `data_file_2' -> `data-file-2'
    The file: data_file_3
    New name: data-file-3
    `data_file_3' -> `data-file-3'
    The file: data_file_4
    New name: data-file-4
    `data_file_4' -> `data-file-4'
    The file: data_file_5
    New name: data-file-5
    `data_file_5' -> `data-file-5'
    The file: data_file_6
    New name: data-file-6
    `data_file_6' -> `data-file-6'
    The file: data_file_7
    New name: data-file-7
    `data_file_7' -> `data-file-7'
    The file: data_file_8
    New name: data-file-8
    `data_file_8' -> `data-file-8'
    The file: data_file_9
    New name: data-file-9
    `data_file_9' -> `data-file-9'

    An ls will reveal that all files have really been changed this time.

    [raja@atreides test]$ ls
    data-file-1   data-file-2  data-file-4  data-file-6  data-file-8
    data-file-10  data-file-3  data-file-5  data-file-7  data-file-9

    This should give you an idea on how to mass rename files. Of course there are probably better ways. Please do comment if you know some.

How to install a Dynamic DNS client for Fedora Linux

Many people use dynamic dns for different purposes. I use it to give my home machine a public presence. I choose DynDNS for my needs. Its a leader in this segment and have been around for a long time.

First, you’ll need to register for a free account at their site. Once you’ve chosen a domain name and setup your details, you’ll need a dynamic dns client to update your IP, which probably changes every time you sign on to your ISP.

I use inadyn, a free dynamic dns update client that works in linux.

The Inadyn package name is inadyn-mt. Install inadyn using yum:

yum install inadyn-mt

Then edit the /etc/inadyn.conf file with your details.

Then turn on the service.

service inadyn start

Check /var/log/messages. you should get some entires showing whether you edited the config file properly, and the update was successful.

Jul 10 22:50:43 atom INADYN[8251]: Fri Jul 10 22:50:43 2009: I:INADYN: Alias 'your-hostname.dyndns.org' to IP '12.34.56.78' updated successfully.

Once you know the client works, dont forget to turn it on permanently on startup.

chkconfig inadyn on

How to enable the Windows Key in Linux

After installing Fedora, or most linuxes, you may realise that the Windows key that you used during the unenlightened days doesnt work anymore. Even worse, you cannot seem to use it as part of you Keyboard shortcuts. Gives some weird irony when reading about Dead Keys.

Enabling the Windows Key is easy, Just go to System -> Preferences -> Keyboard.

You will get the Keyboard Preferences window up. Choose the Layouts tab.

The Keyboard type used

The Keyboard type used

Then choose Layout Options. A new Keyboard Layout Options window comes out.

Keyboard Layout Options

Keyboard Layout Options

Expand the Alt/Win Key Behavior menu, and choose Super is mapped to Win Keys Then close all the menus. You know have a functional Windows key.

Choose the Super is mapped to Win Keys

Choose the Super is mapped to Win Keys

You can now assign shortcuts using the Keyboard Shortcuts menu. In this screenshot, I assigned Windows key + R to opening a new terminal.

A Sample Shortcut Using the Windows Key

A Sample Shortcut Using the Windows Key

Pretty handy for me. I’m sure you will be able to think of other shortcuts that suite your fancy.

How to print usernames with a userid greater than 500

Occasionally, you need to print out a list of usernames, greater than a certain user id. This is usually useful an a slightly larger setup, or when you want to chain more commands to certain users.

Use the awk command.

awk -F":" ' $3 > 200  {print $1} '  /etc/passwd

2 CLI ways to determine which rpm package has a program you wish to install in Fedora Linux

There are 2 ways I know of to search for a particular program to install in Linux. As usual, I’ll be focusing on the Command Line Interface (CLI).

So, you’ve heard about the versatile port scanning tool called nmap and want to install it in Fedora Linux. The only problem is, you don’t know if it has been pre-packaged and is available as an rpm binary. No worries, all you need to do is search for using yum.

Use the yum search command.

yum search nmap

Based on its output, you know there are packages available, and all you need to do is yum install nmap

[raja@atreides ~]$ yum search nmap
Loaded plugins: fastestmirror, presto, refresh-packagekit
================================ Matched: nmap =================================
nmap.i586 : Network exploration tool and security scanner
nmap-frontend.i586 : the GTK+ frontend for nmap
onesixtyone.i586 : An efficient SNMP scanner
perl-Nmap-Parser.noarch : Parse nmap scan data with perl
psad.i586 : Port Scan Attack Detector (psad) watches for suspect traffic

Or, you might know that its called nmap, and want to search through you yum repositories to find if any of the available rpms have a file called nmap. To do that, use the yum provides */ command. e.g.

yum provides */nmap

Look at what it gives:

[raja@atreides ~]$ yum provides */nmap
Loaded plugins: fastestmirror, presto, refresh-packagekit
2:nmap-4.76-4.fc11.i586 : Network exploration tool and security scanner
Repo        : fedora
Matched from:
Filename    : /usr/share/nmap
Filename    : /usr/bin/nmap

From this, you know that the nmap-4.76-4.fc11.i586 rpm contains these files, and by installing it, you get nmap.

How to use a download accelerator with a Rapidshare Premium Account in Fedora Linux

After reading my previous post, the next question that’s bound to crop up is How do I use a download accelerator with my premium rapidshare share account in linux?

Being a command line junkie, I prefer using the terminal for my downloading needs. Let me introduce you to Aria2. As from the website.

aria2 is a multi-protocol & multi-source, cross platform download utility. The supported protocols are HTTP(S), FTP, BitTorrent (DHT, PEX, MSE/PE), and Metalink.

Using Fedora, my favourite distro of choice, you first have to install it via yum.

yum install aria2

Next, you’ll need to have your cookies saved. Refer to the previous post for more details.

Now comes the fun part, actually downloading the files.

Here’s a file which I uploaded, a movie trailer for a recent movie:-

http://rapidshare.com/files/207110649/terminatorsalvation-tlr1_h1080p.mov

To download with aria2, type in the following command:-

aria2c -s 5 -j 1 -c --load-cookies=/home/raja/cookies.txt http://rapidshare.com/files/207110649/terminatorsalvation-tlr1_h1080p.mov

The switches for this explained:-

  • -s 5 Split the download into 5 connections
  • -j 1 How many files to download at once.
  • -c Continue any paused / cancelled downloads
  • –load-cookies /path/to/cookie The rapidshare cookie file that was created earlier. This has to be an absolute path
  • And finally the actual URL of the file

If you have a list of files to download, you can enter them in to a text file, and pass them to aria2 for batch downloading via the -i list_file_name option. e.g.

aria2c -s 5 -j 1 -c --load-cookies=/home/raja/cookies.txt -i lists.txt

Happy batch downloading!!!