junk food for the brain …
Posts tagged cli
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.
How to download using a Rapidshare Premium Account in Linux
Jun 30th
For a lot of people, the lack of a proper downloader in linux to use with their premium rapidshare accounts seems to be quite a turn off. Well, there’s jDownloader, but I find that a bit too heavy. Unknown to them, they already have a good downloading tool, called wget.
Its basically a two step process being:
- Saving a cookie with your Rapidshare login details in it. To do that, just enter the following command:
$ wget --save-cookies ~/cookies.txt --post-data "login=USERNAME&password=PASSWORD" -O - https://ssl.rapidshare.com/cgi-bin/premiumzone.cgi > /dev/null
Replace USERNAME & PASSWORD with your own Rapidshare details.
What this script does is login to Rapidshare and saves the cookie to a file namedcookies.txtin your home directory. Any normal HTML output is piped to the resident unix blackhole,/dev/null. - Downloading your file using the cookie to supply your Rapidshare credentials. Now that we’ve saved your cookie information, you can go ahead and download to your heart’s content (or maybe daily limit). Here’s an example of how you’d do that. Remember that the cookie you created previously was stored in your home directory. For most linux shells, you can refer to it like this:-
~/cookies.txtTo actually download, use the following command:-
wget --load-cookies ~/cookies.txt your-rapidshare-url.com/file-link.html
Happy downloading.
