Fun with Linux Commands-III - Being productive
Happy Linux commanding!
Who says Linux commands are just for geek people? And who says it is just a fun toy? Linux is simple yet productive, the only limitation is your imagination. Those who argue me with me for Linux being simple, here is a popular saying:
*NIX is basically a simple operating system, but you have to be a genius to understand the simplicity
In this post, we will talk about few commands and write a couple of scripts (don’t worry, it will be damn simple). Some guys might blame that these commands/ scripts have no use and might shout “why the hell do we need that.” Remember, these are just the tools. It’s upto you how well you use these tools for your tasks. Also remember, one who discovers the alternative uses of a tool is often called a Genious. Let’s get started:
1. Make Linux speak that he loves himself.
espeak "I Love Linux"
Now you should be asking why the hell I need that? Well, what about you have a document, or a story and someone in your family is blind, or can’t see nicely. You don’t have enough time reading the document for him/ her. Ask him to sit in front of a computer and run this: espeak < documentName
We have more to do with espeak, you can even output the file to a .wav file or a .ogg file so that you can record them in a CD and mail to someone you care!
Still not impressed? What about making it to read your email, or run it in the background so that it alerts you whenever a new mail arrives in your Inbox and then reads the sender’s name, and subject. Also, if you are little ambitious, you can even make it say the weather, if the weather changes drastically. I won’t discuss how to make it read your mails, or weather; I’m just talking about possibility. When I get some time, I’m thinking to write a script which reads my Gmails. Just keep coming back!
2. Making your own commands.
You have heard Linux is highly customizable. How about writing your own simple command. We will write a small script which allows you a convenient way to change the directories, actually to go back several levels up. Let’s suppose you are inside /home/yourhome/a/b/c/d/e/f/g/h/i/j directory. You want to change the directory (cd) to several levels up. You can easily do this with somthing like cd ../../../../.. But what about something as similar as up 3 which will take you 3 levels up
Fireup your favorite text editor and type the following (don’t be intimated by thinking that you are programming something, I will explain this script line by line, don’w worry!):
#!/bin/bash
LEVEL=$1
for ((i = 1; i <= LEVEL; i++))
do
CDIR=../$CDIR
done
cd $CDIR
echo "You are in: "$PWD
exec /bin/bash
Save the file as up and issue following two commands:
$ chmod 755 ./up
$ sudo cp up /usr/bin
Now, from your home directory try using this command:
$ up 2
Where are you at? At root directory! See how easy it was? Let’s see how our little script chef made pizza for us:
#! /bin/bash -> you are using bash script
LEVEL=$1 -> $1 is the first parameter passed to this script assigned to LEVEL
for ((i = 1; i <= LEVEL; i++)) -> for some times (upto LEVEL)…
do -> …we will go round…
CDIR=../$CDIR -> …creating our path and assigning it to CDIR and…
done -> …when we are done…
cd $CDIR -> …we will change our directory to the path we have created above and…
echo "You are in: "$PWD -> …we will let you know where you are and finally…
exec /bin/bash -> …we are done so let’s get a new shell
That’s was not to easy but wasn’t too hard either. It is not too hard to ease your repetitive tasks with a single file and increase your productivity.
Let’s make another little script…
3. What do you usually do changing a directory? List it contents right? How about this little script?
#!/bin/bash
cd $1
ls
exec /bin/bash
That’s it! Save it as cdls or something like that and then issue this command:
$ chmod 755 & sudo cp cdls /usr/bin
For Ubuntu users, if you want a script that keeps track of all your “apt-get” activities by posting them to your Twitter account, try this little handy script.
4. One more thing about cd. Which is the fastest and easiest cd command that take you to your home directory? cd /home/yourhome ? cd ~ ? cd itself!
$ cd
It takes you to your home directory
5. Tired of typing clear to clear your screen? Press ctrl + l
That’s it for today. Happy Halloween!
Hello from Linux! Says:
[...] Quicktweaks shows how to let your GNU/Linux system say something. Isn’t that fascinating? You can have your computer tell you that you have mail, or you could have a story read out loud if you need to be away from the keyboard for a while. [...]
Posted on November 2nd, 2008 at 8:43 am
alan Says:
In your scripts you are execing /bin/bash over and over again? For someone who leaves a shell open and uses it continually, your scripts would use up resources because you’ll have many instances of bash running. This method also hoses up your history. Try this:
Open a shell and execute the command
ps -ef | grep bash
Then run your script twice:
up 2
up 2
Rerun the ps command and you will see two more instances of /bin/bash running. Each of these uses resources. Also if you now look at your history, you’ll see that some recent commands are not present.
In order to “fix” this, you’d need to run an exit command for each instance of the up script, which would of course undo the directory change.
The easiest way to make a cd in a script “stick” is to use a “. ” (dot space) before the command. For example,
. myscript
could make cd commands and other environment changes in the script stick after the script exits without spawning a new shell
Also, its generally considered bad form to copy scripts into /bin. Most users I know create a local bin directory under their user account and put their own scripts there. This has the added benefit that all the scripts you create are immune to system upgrades if /home is on a separate partition or you back up user data before upgrading.
I don’t mean to be picky, but new users will look at what you write as gospel. Teaching them bad habits to begin with is not a good way to start.
Regard,
alan
[Reply]
Posted on November 4th, 2008 at 9:39 am
GoblinX Project » Issue 172, News & Note Says:
[...] Fun With Linux Command III [...]
Posted on November 6th, 2008 at 6:46 am