How to record your Linux shell session

A lot of people are probably not aware of the command script. Script is a quick and easy way to record everything you do in a terminal session.  I use script to record sessions of me fixing a server, or troubleshooting Linux issues, and save it for future needs, or to pass on to others as training material.  Here is what the Linux man page says:

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).

Using script to record your terminal session

It’s really quite simple to record your bash session.  All you need to do is type script -a filename to start recording your session:

laptop:~ foogazi# script -a session1_jun162010
Script started, output file is session1_jun162010
laptop:~ foogazi#

Now that the recording has started, everything you type, as well as everything that returns as output, will be saved into the filename you chose to output to.

How to configure static IP on Ubuntu

Do you use Ubuntu and wish to set a static IP for your machine?  It’s simple. Follow the steps below to find out how.

  • Right click the network manager icon at the top right of your desktop
  • Select Edit Connections
  • Select Wired
  • Click the EDIT button
  • Click the IPv4 settings tab
  • Select Manual from the method drop down list
  • Click the ADD button to add your static IP address
  • Add your DNS addresses in the DNS servers field. You can separate each DNS entry with a comma
  • Click OK.
  • Restart networking using this command: /etc/init.d/networking restart

Quickzi: How To Block Incoming Access to Port 80

Here is a quick Linux tip to block incoming access to port 80 using iptables.

iptables -A INPUT -j DROP -p tcp --destination-port 80 -i eth0

The code above will drop all tcp packets coming into your Linux computer on device eth0 on port 80.  If your Internet connection runs through a device other than eth0, go ahead and make the adjustment.

To remove the iptables rule use the following code:

iptables -D INPUT -j DROP -p tcp --destination-port 80 -i eth0

For more information on using iptables visit the iptables man page.

Getting help with the Linux Command Line

This is a guest post by Taylor Douglass

The command line interface (CLI) is a very powerful part of Linux. Linux beginners undoubtedly agree that at first it can seem a bit daunting and even dangerous. While it is true that there far more Linux commands available than most will ever want or need to know and that some of these commands are capable of wreaking havoc on your system if used incorrectly; it’s reassuring to know that help isn’t far away. While a new Linux user who is Internet savvy might be inclined to run straight to Google when they need help with a particular command or are searching for the command they need, this is not necessary. In fact, most of the time you don’t even need to leave your terminal. How is this possible? Simple, it’s the man command.

Getting Started With Man

Man is short for Manual, and basically means that by using the man command you are accessing the manual for a particular command. Let’s begin by exploring how man is used. Startup a terminal and type “man intro”. This brings up a basic introduction to user commands, and the CLI in general. Once you have finished reading the intro go ahead and exit the manual page by hitting the letter “q”. You should now be back at the command prompt. The next step you will want to take is to read the manual page for the man command itself. This is accomplished by typing “man man“. You may be beginning to see a pattern here. To see the manual for any command simply type “man command” at the prompt. You may also begin to notice a pattern in how the manual pages look. Most manual pages consist of several different sections. Let’s take a look at the sections available for mv command. Type “man mv” into your terminal.

Name
This section shows the command and gives a brief description of the command. In our example we see that the mv command is used to move or rename files.

Synopsis
Here you can find precise information on using the command that you are viewing. This includes all the switches and options available for the command. The first two sections (Name and Synopsis) are meant to be a quick reference for using the command that you are viewing. Further sections dive a little deeper and give you even more information.

Description
The description section breaks down all of the options available and tells you what they do. In our example with mv we can see that using the -i option prompts the user before overwriting any files that are being moved. Also note that we could also use –interactive to do the same thing.

Other sections
Other sections that you might find in the manual pages are author information, bugs, copyright, files, see also, and examples. The two that might be worth mentioning are the See Also and Example sections. See Also gives you a list of other manual pages that may be relevant to the page you are currently viewing, while the Example section gives you a real world example of using the command you are viewing.

Searching For Commands

Let’s say that you want to copy a file but you are not sure what the command for copy is. Perhaps one of the most useful features of the Linux manual pages is the ability to search them. If we look at the manual page for the command man (Again that is “man man” at the command prompt) we can see that one of the options available is -k. What this option does is allow us to search the short description for keywords and display any matches. Back to our example for finding the command to copy files, let’s get to the command prompt and type “man -k copy“. This displays all the commands that have “copy” listed in the description. If you glance through the list of commands that man -k has found you should see “cp(1) -copy files and directories“. That looks like the command we need, but to be sure let’s look at the manual page for that command to be sure. “man cp” brings up the manual page for the command cp. As you can see by looking at the name and synopsis sections, this is indeed the command we need to copy a file.

Man I’m Glad There are Manual Pages

The man command has gotten me out of a jam on more than one occasion. I recommend that if you are just beginning to explore the CLI that you do a quick check in the manual before using any command. As I mentioned before the CLI is very powerful and it’s better to be safe than sorry when using it.

Creating Custom Linux Commands

This is a guest post by Taylor Douglass

The other day I ran across a script at shell-fu that generates a random quote from their website (here). I thought it would be useful to make this a command that I could execute at any terminal at anytime. Let’s get started.

First of all you are going to need links. If you don’t have links or are not sure if you do or not, fire up a terminal and type the following:

# sudo apt-get install links

Next navigate to your /bin folder and create the file

# sudo touch shell-fu

Make the file readable and executable

# sudo chmod +rx shell-fu

Now edit the file and add the code for retrieving the shell-fu quote (I use nano for this)

# sudo nano shell-fu
# links -dump "http://www.shell-fu.org/lister.php?random" | grep -A 100 -- ----
^O (writes the file from nano)
^X (exits nano)

You now have a custom command named “shell-fu” that you can type from anywhere and retrieve a random shell-fu quote.

Quickzi: How To Remove Blank Lines from a File

If you want to remove all blank lines from a file, a quick way of doing it from the Linux command line is to use sed.

So, lets say that your file looks like this:

# vi example_file

This is an example text file.
We will be using sed to remove blank lines from this example file.

sed is a stream editor for filtering and transforming text

There are a lot of powerful things you can do with sed.

:wq

Now, to remove the blank lines in this file, we’ll use sed.

# sed -i '/^$/d' example_file

# cat example_file

This is an example text file.
We will be using sed to remove blank lines from this example file.
sed is a stream editor for filtering and transforming text
There are a lot of powerful things you can do with sed.

If you want to preserve the original file, then you can pipe the changes to a new file:

sed '/^$/d' example_file > example_file_new

Cheers!

Quickzi: How To List Files with a Certain Date Stamp

Here is a quick tip on how to list files in a directory with a certain date stamp using awk.

Let’s say that you want to list all files stamped with 2008-04-11.

# ls -l | awk '{if($6=="2008-04-11") print $N }'

-rw-r--r-- 1 adam users 0 2008-04-11 16:27 file1
-rw-r--r-- 1 adam users 0 2008-04-11 16:27 file2
-rw-r--r-- 1 adam users 0 2008-04-11 16:27 file3

If you want to list only the file names, add an F switch.

# ls -l | awk '{if($6=="2008-04-11") print $NF }'
file1
file2
file3

How To Explain Linux to a Windows User

Theres been a question I have seen on multiple occasions both on the “How do I explain Linux to a Windows user” end as well as the “What is Linux?” question. That got me thinking.. what is the best way to explain Linux to a normal Windows user who has never heard of Linux? Have you ever been using a laptop in a public place, or have someone over at your house, and you’re running Linux and someone asks you why it looks different? Do you take the easy way out and say “it’s Linux, it’s like Windows but different!” or do you actually explain what Linux is? Here are some ideas of getting the message across as easy and straightforward as possible.

Explaining Linux to a Windows user

Here are a few ideas you can put together to help you explain Linux:

  • Every computer has an Operating System. Windows is an Operating System. So is Linux.
  • Linux was written in the early nineties by a college student name Linus.
  • Linux is not owned by any one person.
  • Linux is free, unlike Windows. Most people pay the Windows fee when they buy the computer that comes “pre-installed” with Windows.
  • It’s fun to use.
  • You have complete control of all aspects of the operating system.
  • It’s “look and feel” is completely customizable. You can make it look like Windows or you can make it look unique.
  • You can’t use all of the same software applications that you use on Windows, but there are alternatives to windows programs.
  • If your computers primary use is for playing popular computer games, hold off on installing Linux.
  • Linux is secure and practically virus and spyware free.
  • It can be a lot faster than Windows with the right setup and configurations.

How do you explain Linux to a Windows user?

Quickzi: How To Set Cron to Run Every 5 Minutes

Here is a quick tip on how to run crontab every 5 minutes.

*/5 * * * * /home/adam/script.sh will execute script.sh every 5 minutes.

Also, here’s a quick guide to understaning the layout of cron:
# MIN HOUR DAYOFMONTH MONTH DAYOFWEEK COMMAND
5 * * * * echo 'Hello'

Also, the Crontab Man page

For further reading on Crontab check out Understand Cron Jobs in 5 Minutes