junk food for the brain …
Posts tagged Linux
Generate Files with Random Content and Size in Bash
Jul 29th
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.
- The bash
$RANDOMfunction. It generates a random number between 0 – 32767. - Linux
DDutility, to output files. /dev/(h|s)da, your hard drive in linux.- All wrapped in a bash
whileloop.
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.
Configure a Caching-Only Name Server in a Chroot Environment for Fedora 11
Jul 18th
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.
- Install
bindandbind-chrootpackages# yum install bind bind-chroot
- Edit your
/etc/sysconfig/named file.# vim /etc/sysconfig/named
Add the following line:
ROOTDIR="/var/named/chroot"
- Edit your
/etc/named.conffile.# vim /etc/named.conf
- 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.
- 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.
- Make sure it starts at boot time.
# chkconfig named on
Restart your DNS server.
# service named restart
- 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/namedshows it is listening correctly. - 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). - Finally, set up your
/etc/resolv.confaccordingly.On the server:
nameserver 127.0.0.1
And on all your other machines:
nameserver 192.168.0.10
Change Your MAC Address in Linux
Jul 16th
Some times it necessary to change your network MAC address on your linux box.
Unlike Windows, which requires some registry editing or even specialized tools, its just a command line away in Linux.
For example, lets change the MAC address of your first netwrok card, usually called eth0.
Let’s swith to being the root user and view the current MAC address:-
ifconfig eth0
You should get something similiar:-
[root@rhel-5new ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 08:00:27:2C:D2:B5
inet addr:192.168.0.108 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe2c:d2b5/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:20545 errors:0 dropped:0 overruns:0 frame:0
TX packets:18348 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:10083277 (9.6 MiB) TX bytes:7489363 (7.1 MiB)
Interrupt:11 Base address:0xd020
The MAC address is listed next to the HWAddr column, which is 08:00:27:2C:D2:B5 in this case.
We’ll change it. First shutdown the network interface.
ifconfig eth0 down
Now change it to say, 08:00:27:2C:D2:B4.
ifconfig eth0 hw ether 00:00:27:2c:d2:b4
Bring the interface back up.
ifconfig eth0 up
Finally, lets have a look at the new mac address.
[root@rhel-5new ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:00:27:2C:D2:B4
inet addr:192.168.0.108 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::200:27ff:fe2c:d2b4/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:20957 errors:0 dropped:0 overruns:0 frame:0
TX packets:18642 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:10146055 (9.6 MiB) TX bytes:7520724 (7.1 MiB)
Interrupt:11 Base address:0xd020
You can see that the HWaddr field now reports 00:00:27:2C:D2:B4.
Check Your Spelling in Linux using the Command Line
Jul 15th
You can do a spell check on from the command line in Linux.
First, make sure you have aspell installed. In Fedora, just yum install it.
$ yum install aspell
You’ll also need a dictionary, from which aspell can search for words.
A yum search aspell will give you a list.
[raja@atreides visilon]$ yum search aspell ----------------------------------------------------------- aspell-af.i586 : Afrikaans dictionaries for Aspell aspell-bg.i586 : Bulgarian dictionaries for Aspell aspell-br.i586 : Breton dictionaries for Aspell aspell-ca.i586 : Catalan dictionaries for Aspell aspell-cs.i586 : Czech dictionaries for Aspell aspell-cy.i586 : Welsh dictionaries for Aspell aspell-da.i586 : Danish dictionaries for Aspell aspell-de.i586 : German dictionaries for Aspell aspell-el.i586 : Greek dictionaries for Aspell aspell-en.i586 : English dictionaries for Aspell aspell-es.i586 : Spanish dictionaries for Aspell aspell-fr.i586 : French dictionaries for Aspell aspell-ga.i586 : Irish dictionaries for Aspell aspell-gl.i586 : Galician dictionaries for Aspell aspell-hr.i586 : Croatian dictionaries for Aspell aspell-id.i586 : Indonesian dictionaries for Aspell aspell-is.i586 : Icelandic dictionaries for Aspell aspell-it.i586 : Italian dictionaries for Aspell aspell-no.i586 : Norwegian dictionaries for Aspell aspell-pl.i586 : Polish dictionaries for Aspell aspell-pt.i586 : Portuguese dictionaries for Aspell aspell-ru.i586 : Russian dictionaries for Aspell aspell-sl.i586 : Slovenian dictionaries for Aspell aspell-sr.i586 : Serbian dictionaries for Aspell aspell-sv.i586 : Swedish dictionaries for Aspell aspell-fo.i586 : Faeroese dictionaries for Aspell aspell-gd.i586 : Gaelic dictionaries for Aspell aspell-nl.i586 : Dutch dictionaries for Aspell aspell-ar.i586 : Arabic dictionary for Aspell aspell-bn.i586 : GNU Aspell Bengali Dictionary Package aspell-gu.i586 : GNU Aspell Gujarati Dictionary Package aspell-he.i586 : Hebrew dictionary for Aspell aspell-hi.i586 : GNU Aspell Hindi Dictionary Package aspell-ml.i586 : GNU Aspell Malayalam Dictionary Package aspell-mr.i586 : GNU Aspell Marathi Dictionary Package aspell-or.i586 : GNU Aspell Oriya Dictionary Package aspell-pa.i586 : GNU Aspell Punjabi Dictionary Package aspell-sk.i586 : Slovak dictionaries for Aspell aspell-ta.i586 : GNU Aspell Tamil Dictionary Package aspell-te.i586 : GNU Aspell Telugu Dictionary Package
That’s a lot of languages. I’ll just install the English dictionaries.
yum install aspell-en.i586
Now the fun part. Checking your spelling via command line.
Just type aspell -a and it will give you a prompt. I’ll intentionally type a misspelled word and see what it returns:-
[raja@atreides visilon]$ aspell -a @(#) International Ispell Version 3.1.20 (but really Aspell 0.60.6) psychatrist & psychatrist 5 0: psychiatrist, psychiatrists, psychiatrist's, psychiatry's, psychiatric
I spelt ‘psychiatrist’ wrongly, and it gave me back a few suggested corrections. (Ok I admit, I didn’t know how to spell it in the first place.
)
Lets see what happens when you give it a correctly spelled word:-
[raja@atreides visilon]$ aspell -a @(#) International Ispell Version 3.1.20 (but really Aspell 0.60.6) disapprove *
Yes, it returns an asterisk(*), showing you’re spelling is correct.
Press Ctrl + D when you’re done, to bring you back to the command line.
If you have a text file, you could check that using aspell as well. For example, here’s some sample text:-
Mary had a little lamb. It's fleede was whote as snow.
We’ll save it as mary.txt and run aspell like this to check the file:
aspell check mary.txt
You’ll get a interface like this, highlighting every misspelled word, with the suggested actions.
When done with the checking, aspell will exit, saving the previous copy with a .bak extension.
How to Mass Rename Files in Linux
Jul 12th
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.
- Check which type of shell you are using.
echo $SHELL
As I am using bash scripting, make sure the result is
/bin/bash - 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
- 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
lscommand and store it in the$file_namebash 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
- Now that we’ve stored the file names in variables, we can use the
sedcommand to edit it, replacing the underscores with a hyphen. I’llechothe 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,
swith a global option behind, theg, 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
- 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
) - 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
- Now that we’ve got both the old AND new file names, nicely stored in variables, its time to actually rename them using the
mvcommand. The syntax for renaming ismv current_file_name new_file_name. I think you’re starting to see where these variables come in handy.
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
-vto themv, 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
lswill 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.
VirtualBox 3.0.2 released!
Jul 11th

VirtualBox 3.0.2 has been released. This is a maintenance release, but it solves a lot of problems. For example, 3D accleration works way better now if you have Desktop Effects enabled. A definite recommended upgrade.
How to install a Dynamic DNS client for Fedora Linux
Jul 10th
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
Jul 10th
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. The Keyboard Menu under Preferences
You will get the Keyboard Preferences window up. Choose the Layouts tab.
Then choose Layout Options. A new Keyboard Layout Options window comes out.
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.
You can now assign shortcuts using the Keyboard Shortcuts menu. In this screenshot, I assigned Windows key + R to opening a new terminal.
Pretty handy for me. I’m sure you will be able to think of other shortcuts that suite your fancy.
2 CLI ways to determine which rpm package has a program you wish to install in Fedora Linux
Jul 8th
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 */
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
Jul 8th
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!!!




