Browsing all articles tagged with Tips

How to Copy Terminal Session into a File

Have you ever wondered how to copy the output of your terminal into a text file? Or maybe you teach Linux and you want to see what your students typed in and as well as the output? You think that running history is not enough? Then you need the script command.

Running script command

Open the man page of script command and you will see this:

Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).

In a nutshell, it is history and tee all rolled into one. It will record everything you see on your screen, even the color. So if you typed in an invalid command, you will see the error in the log or if you run it correctly, you will have the output. But commands like top that refreshes the screen at an interval will most likely ruin the session or the log, so try to avoid similar commands.

To use it, just type the command script and it will begin recording the session. Once you are done, just type exit.

This is script in action:

rai@host1:~> script -a /tmp/script_test.log

Script started, file is /tmp/script_test.log
rai@host1:~> ls /home
R20 r200 R21 rai xx19
rai@host:~> thisnotacommandbutirunitanyway
bash: thisnotacommandbutirunitanyway: command not found
rai@host1:~> exit
Script done, file is /tmp/script_test.log
rai@host1:~> cat /tmp/script_test.log
Script started on Mon 17 Jan 2011 06:24:12 PM PHT
rai@lhost1:~> ls /home
R20 r200 R21 rai xx19
rai@host1:~> thisnotacommandbutirunitanyway
bash: thisnotacommandbutirunitanyway: command not found
rai@host1:~> exit

Script done on Mon 17 Jan 2011 06:24:54 PM PHT

The example above shows that script was started with -a option meaning it will append the output the specified file.

A better way to do this is to use it together with mkfifo command:

On Terminal 1 (Student’s terminal):

rai@host1:~> mkfifo /tmp/script_test.fifo
rai@host1:~> script -f /tmp/script_test.fifo

On Terminal 2 (Teacher’s terminal, same machine):

rai@host1:/tmp> cat /tmp/script_test.fifo

The above scenario will perform the following:
1) On the Student’s terminal, it will create an named pipe /tmp/script_test.fifo (man mkfifo) then run the script command with the -f option that ‘flushes’ out the output after each run. The Student’s terminal will look like it is not responding at this point, but don’t worry, it is perfectly normal.
2) On the Teacher’s terminal, the command cat will read the output file. Once you run the cat command, the session will be started.

Try the above steps and see how each screen behaves. Check also if doing the script command will create a populated output file.


How to Use watch Command

If you need to execute a certain command repeatedly, you may use the watch command to do the repeating for you.

In this example, the command ps will be run every 2 seconds to monitor how fast the new processes spawn:

$ watch -n2 "ps aux|grep http"

The watch command will run ps every two seconds and it will display the output in stdout. You may increase or decrease the interval as necessary. This is useful if you want to monitor a process at certain time intervals. Personally, I use this to check for processes that spawns children way too fast.


How to Use the alias Command

Let us say you execute the command ‘ls –alh’ every time you need a long listing of files and directories. Now you want a shortcut to do this. Fortunately, you can do so by using the command alias.

The alias command is useful for creating shortcuts for long commands or for correcting typing mistakes.

To create a shortcut for ls, you can do this:

$ alias ls=”ls -alh”

Now, everytime you execute ls command, it will be run as if you are executing the whole ls –alh command. Be reminded that this will replace the existing ls command. You may use a different name for the new shortcut like so:

$ alias ll=”ls -alh”

However, once you exit the current terminal, the alias will not be saved. To make the alias permanent, you may edit the .bashrc file in user’s home directory:

$ vi ~rai/.bashrc

Then insert the alias command after the line that says #system wide functions and aliases. Save and exit.

That should do it!


Tip: Can Download Torrents But Cannot Browse

I have recently purchased a new home office router that connects two desktops and one laptop by wireless connection. Back at the store, I made my mind to purchase Linksys WRT54G over D-Link (I forgot the model) because there are a lot of good feedbacks on Linksys.

Everything was good the first two weeks we were using the router. The signal strength is good and my laptop can get decent W-Fi signal anywhere inside. Then the network became rather choppy, pings are intermittent, and worst, one desktop (particularly my desktop PC) cannot browse.

The weird thing I was able to download stuff using torrent, my Pidgin can sign in to my accounts on Yahoo and MSN Messenger and streaming video and music works. Everything works, except for the browser. I blamed the Linksys for this and contacted the support thrice, and I was given a lot of configurations that should be done in the router settings, none of which solved my problem.

Googling around got me some help and apparently, this weird symptoms are caused by the traffic from torrent downloads. To fix this, these settings must be done on the torrent client:

1. Reduce the download and upload speed of torrent client. Since I use the lightweight uTorrent Client, I can simply do this by right-clicking the uTorrent taskbar and selecting the download speed that I want. The procedure could be different depending on your client.

2. Disable DHT. DHT stands for Distributed Hash Tables and disabling this in torrent client effectively solved my problem with browsing. Again in uTorrent, this can be done by pressing CTRL+P to open the Preferences window and selecting the BitTorrent tab. Uncheck the DHT selections and click OK to save the new settings. Restarting the client helps too.

I did these two steps and this solved the issue of not being able to browse the Internet while downloading torrents.


10 Things You Didn’t Know You Could Do In Ubuntu

Author Rai    Category Linux     Tags , , , , ,

UbuntuUbuntu is one of the popular Linux distros around, still gaining popularity by the minute. It is very easy to use, lightweight and newbie-friendly. Ubuntu is the reason why the Linux stereotyping became less as this distro is becoming similar to Windows in terms of ease of usage but without the extra fat.

Since more people are migrating from Windows to Ubuntu, here are some interesting Ubuntu tips from that you may want to try with your Ubuntu pc. These tips are provided by Keir Thomas, author of the book Ubuntu Kung Fu, which contains over 300 other tips for Ubuntu 8.04.1.

1. Create website links that automatically install software

Did you know that you can create a link that will automatically launch Ubuntu’s package manager and install the software? This is very useful if you are helping someone install certain programs in Ubuntu. To create ‘software install hyperlink” just create a hyperlink but instead of pointing to the usual http:// address, use

apt:< package name >

So if you are trying to install firefox, create a hyperlink and put apt:firefox in Hyperlink bar. This will create a hyperlink that will launch the package manager and install the package when clicked.

2. Do stuff without touching the mouse

If you know how to launch the ‘Run’ dialog box in Windows, this certain command is also available in Ubuntu. Press ALT+F2 and the similar ‘Run’ dialog box will appear, type in the command or the program name, let’s say firefox, hit enter, and firefox will launch.

3. Instantly Search Google for Any Word or Phrase

Googlizer is an app that you can install (this is available from the package manager) and use it to search Google for anything using the keywords directly from your file. An example is if you have a PDF file that contains the word ‘lethargy’, with Googlizer, all you need is to highlight the word, and click Googlizer’s icon to search the web.

4. Create a File Delete Command That Uses the Trash

If you are a frequent user of rm command, you can create a command that will move the file you wish to delete to the Trash directory, instead of completely deleting the file. To do this, just use the command alias and few tweaks with Linux files:

- Open a terminal window, and type gedit ~/.bashrc
- Add this line after the last line of the file:

alias trash=”mv -t ~/.local/share/Trash/files –backup=t”

- Save and close.

To use the command, you need to use the trash command instead of rm:

trash mydoc.txt

5. Repair Windows from Within Ubuntu

You can mount your Windows partition inside your Ubuntu and do stuff with it. With stuff I mean you can access your files in Windows partition, or you can also repair it within Ubuntu. To be able to repair a near-death Windows partition, unmount it and use the command ntfsfix:

sudo ntfsfix /dev/sda1

This is assuming that your Windows partition is /dev/sda1 and the filesystem used is NTFS.

6. Dump the Text on a Virtual Console to a File

Large files can be tiresome to read so you may want to filter the words that you need and dump into a new and smaller file. This can be done using this command:

ls > output.txt 2>&1

The command will execute ls command, put the results into the output.txt file and display errors, if there are any.

7. Instantly Hide a File or Folder

In Linux, any file that begins with period (.) is considered as hidden file. So if you want to hide a certain file from a younger sibling or parent, rename a file and put . at the beginning of the filename

mv grades.txt .grades.txt (use this command inside a terminal)

Or if inside Nautilus, highlight the file, press F2, and rename the file.

8. Print at the Command Line

Did you know that you can print files from the command line? Try this command to print a file without the fancy format for fast printing:

lp -o page-top=72 /home/myfile.txt

This is a quick and dirty way of printing files since the formatting is disregarded, but very useful if you want to print something fast and easy.

9. Listen to MP3s when no GUI is running

If you need to work in your Ubuntu using text mode only and no GUI running, install vlc using the apt-get command and use it to play your MP3 music from the command line:

vlc -I ncurses /home/*.mp3

This will play all mp3 files in the /home folder using the CLI mode of vlc.

10. Turn your desktop into your /home folder

If you want to make your default Desktop directory into something else beside the typical /home/user/Desktop location, you can do so by hitting Alt+F2 and type in gconf-editor. This will launch the gconf-editor app, put a check beside /apps/nautilus/preferences, save and exit. The change will take after after your next login.