Archive Page 2

For each file saved on a Linux filesystem, there is an i-node which contains information about the file, such as how large it is and who owns it. One of the nifty things you can do is to give a file two names. Let’s say you have a file called heinlein.txt. By entering the following command, it will also be called robert.txt:

ln heinlein.txt robert.txt

The ln command creates a hard link to the file. This can be useful you need to store a file in a new place but other users or programs are still trying to find it in its old location.

There’s a limitation with this: you can only create a hard link to a a file on the same disk because of the way i-nodes work. So if you need to link to a file on a different disk, you can use a symbolic link, e.g.

ln -s heinlein.txt robert.txt

Instead of linking robert.txt to an i-node, it links it to the filename heinlein.txt


I wanted to see if I could install some software from a repository using the command line. Fedora uses the following command:

yum install packagename

But what do you do when you don’t exactly know the package’s name, perhaps because it has something descriptive appended to the end of the filename?

Well this is what you do:

yum list available | grep emacs | less

Yum lists the available packages, grep searches for the ones containing the word ‘emacs’, and less displays the result in a scrollable viewer on the screen. Marvelous.


Pipes

10Dec07

Pipes work like redirection (discussed earlier today) except that instead of dealing with text files, we’re dealing with different programs. A pipe connects the output of one program into the input of another.

Let’s say you want to view all the files of a directory. You might type in:

ls -l

The -l bit makes the command the display in a long listing, giving you more information than a regular directory listing.

One difficulty with the ls command occurs when the directory listing is too long for the screen, because you only get to see the bottom of the listing. Using a pipe can be useful. Instead of using the ls command’s default display, you can pip the results into a different program, such as ‘less’ which lets you scroll up and down the results. This is how to do it:

ls -l | less

It’s the | bit that represents the pipe. More than one pipe can be used in the same command. This page suggests this:

du -sc * | sort -n | tail

The du command stands for disk usage. -sc lists the sizes of all files and directories in the current directory. sort -n, as you might image, sorts by number (smallest to biggest). The tail command displays just the last few (the largest) results. Together, this is great if you want to work out where all the disk space is going.

Oh, and this is a good point to set up an alias, so that instead of typing all that you just enter the word wasteofspace. Here’s how to set it up:

alias wasteofspace='du -sc * | sort -n | tail';

What I can’t do is to get the output piped into Vim or Nano, as in:

ls -l | vim

Vim complains that “Input is not from a terminal”, for some reason. The workaround is, alas, to redirect the output to a file and then separately load it into Vim.


Input/output redirection might sound wierd, but it is actually rather useful.

Redirection, for short, is a way of taking a program on the command line and making it work with a text file. For example, to display a directory listing, we would normally type:

ls

But let’s say we wanted to keep this listing for later. We could use redirection to output the ls command to a file, as in:

ls > listing.txt

The listing.txt file can then be read in a text editor.

Instead of outputting to a file, we can append to a file. Basically that’s the same thing, except it adds the output to the end of an already existing file. If we type:

ls -a >> listing.txt

It adds a different type of directory listing to the end of the listing.txt file. (The -a attribute lists both visible files and hidden ones.

So far I have explained output redirection. There is also input redirection, where we send a file as input. Here, the > is replaced by < as in:

grep 'lunch' < listing.txt

So what does this do? Well the grep command displays lines that match a pattern, and here it’s being asked to display lines containing the word lunch. Let’s say there’s a file in the current directory called luncheon.txt (it’s name is stored in our listing.txt file, which we made earler). So the result of typing in this command is that the computer replies with:

luncheon.txt

Su and sudo

09Dec07

Depending upon who you ask, the su command stands for substitute user, switch user or super user. While being logged in as yourself, it lets you use the privileges of another user. By default, this means becoming the all-powerful ‘root’ user. For that, you simply type

su

and you will be asked for your password. To get out of being root, you simply type

exit

More often, though, I find myself using the command sudo which (arguably) means ‘super user do’. You simply put sudo before a command that ordinarily you wouldn’t be allowed to do, and then the computer obeys.

sudo-make-me-a-sandwich.png

(Cartoon by Randall Munroe.)


Instructions for using particular commands can be found by simply entering:

man gawk

This gives nearly 75 screens worth of information about using the gawk command, can be rather useful. Gawk has a particularly long manual (Nano, for example, only has 11 screens, which is more more manageable). But what do you do if you wan
t something more brief?

The answer is to type –help after the command, as in:

gawk --help

That brings up a page or so, succinctly explaining the command.

Finally, there’s a command like man (inasmuch as gives a lot of information) but it operates in a hypertext format. Just type:

info gawk

When I first used Linux on the desktop some years back, installing software could be unitutive, often involving dropping into the command line. For geeks, it was fine, but it seemed to me that most people would take one look at that and run for Windows.

So I have been quite surprised over the past couple of days by just how easy it is to install software these days on Linux. Installing software found on the web is almost exactly the same as installing software in Windows. I visited Skype’s website, clicked on the .rpm file (Skype, for those who aren’t aware, helpfully said that this was the type of download for Fedora Linux). Firefox downloaded it and asked if I wanted to give it to the software installer, and that was that.

Was I just imagining how much effort it used to take? I googled “installing skype linux” and found this page. The instructions involved typing 15 lines at the command line, plus editing a text file.

Admittedly those instructions were for Ubuntu, but I think they were indicative of some of the difficulties with installing programs like Skype only a couple of years ago. My recent experience with Skype is duplicated with all sorts of other programs. This is excellent.


I think I’ve cracked it.

In my previous post, I talked about the difficulty of getting Linux to work correctly with my PC’s graphics card. So what did I do?

Firstly, I reinstalled Fedora, altered the /etc/X11/xorg.conf file so that it used the works-with-everything vesa video driver. The problem of this file reverting to a completely incompatible driver didn’t come back, which was nice.

Then I opened up my Linux box and found that it uses a Gigabyte GV-N66256DP graphics card, which I remember choosing because it was fanless. Its brain is a GeForce 6600. Upon Googling, I found that there is an official nVidia driver for it (yay!).

However, this driver wanted to recompile the kernel. This is not something I can remember ever doing in the past, although I read about it the other day in the systems administration book I’m ready. It needed the sources to be installed, which weren’t but I found them on the CD – or, rather, for the version of the kernel that I installed before I hooked the PC up to the internet and did a software update.

Not having the internet to hand (it’s a long story), I rebooted and chose the older kernel and the driver patched the kernel without problems and I was treated to a higher resolution screen mode.


Why Fedora?

08Dec07

 Jake from the excellent 8BitJoystick website asks (in the comments):

So why Fedora and not some other kind of Unix or Linux like Ubuntu?

I used Red Hat back in the day, I’ve set up Ubuntu on system for a friend and my girlfriend has an Xandros running Asus Eee laptop.

Well, that’s a very good question. Ubuntu is a very good distribution: I spent a couple of months almost exclusively using Ubuntu back in 2006. My primary reason for focusing on Fedora is a career one. RedHat is so widely used commercially, and I am planning to take a RedHat certification. Fedora (being RedHat’s brother) is, therefore, a good system to learn on.


One of the more puzzling things I’ve been experiencing with Fedora Linux is getting the graphics card and the operating system to play ball (and it’s not just Fedora: Ubuntu won’t do a graphical install on the computer). Here’s my main problem: each time I boot the system, I’m told that it “Failed to start the X server” and that the graphics card is not set up correctly. So each time I use Vim to edit a configuration file called /etc/X11/xorg.conf. Then I enter xstart and the GUI starts up fine. However, when I reboot the system, the problem occurs again and the configuration file has been reverted.

My plan is a reinstall, and then to open up the computer and find out the model number of the graphics card and do some googling.


I’ve been reading about some of the command line ways to configure users and also about how the Linux filesystem works. RedHat and Fedora use a filesystem called ext3. This is a journaling filesystem (rather than ReiserFS, which was apparently used by Novell Suse Linux until last year). ext3 is a good option because it is backwards compatible with the earlier, non-journaled ext2, and therefore a lot of utilities exist to work with it. It also has an excellent track-record of reliability.

I’m learning a lot at the moment from Linux Administration: A Beginner’s Guide, but also with the help of RedHat’s online documentation and other sites. I’ve only got the third edition (2003), which is a bit out of date now but still useful. I’ve been reading about important directories in Linux, although I think I will need to do a bit more practical messing about on a real machine before I remember everything.

I’m gradually getting more used to Vim, which I’ve using today to modify some code one a web server. It is not exactly what you might call user-friendly – you have to remember all the commands – but it is rather efficient. I learned today how to do searching. You enter / followed by the text you want, and n if you want to go the next instance of the search text. It highlights the found text (actually Vim is really good at highlighting code in all manner of ways).

Following instructions on the net, I needed to alter a particular line of code in a script. In Vim I typed n231G and it took me there (the numbers represent the line number). Great.


Using Vim

04Dec07

I’ve been using the Linux text editor Vim quite a lot today for updating a website. I am now at the point where I can happily modify files – navigating them, deleting lines at a time, and switching between Vim’s different modes. I googled for Page Down and found that Ctrl-F did it, which was pretty useful.

I was doing something rather tedious, though I appreciated the practice in Vim, nevertheless. My aim was change a few lines towards the bottom of many dozens of HTML files. What I really wanted was to be able do a folder-wide search and replace. I tried to find if there was an easy way to do this. A GNU program called gawk seemed like a possibility, and someone from GLLUG recommended another GNU program called Sed. Both seemed very complicated to use, so I decided to do it manually.


Learning Bash

04Dec07

Three things I have just learned about Bash, the most popular “shell” or command line in Linux.

Firstly, tab completion is very handy if you want to save yourself the time of typing in a full directory name. For example, if you want to type cd Documents, to move into the Documents folder, you can simply type cd Do[Tab] and Bash will automatically complete what you’re typing. If you type cd D[Tab], the system will beep (well mine will anyway) because there are two folders starting with D. But if you type cd d[Tab][Tab], it will give you a list of folders starting with D.

Secondly, and I knew this already but I’m putting it in here for completeness, pressing the Up arrow key in Bash lets you move up commands that you have already entered previously. What I didn’t know is that Bash stores the history in a file called ~/.bash_history

Thirdly, I wrote my very first Bash script in Vim. A Bash script is simply a text file containing a command on each line which you run by typing bash scriptname.

There’s still a heck of a lot for me to learn on Bash. Here are three webpages I’ll be studying. Firstly, a handy A-Z index of Bash commands. Secondly, GNU’s Bash Reference Manual. And if I need some extra help, I’ll think about the book Learning the Bash Shell from O’Reilly.


Bash and Vim

03Dec07

I’m currently learning about two aspects of Linux: Bash, a command line interface (think DOS or the BBC Micro…) and Vim (a non-GUI text editor).

Bash I’ve used for a long time in a very basic way. It’s the default command line in Mac OS X (well, recent editions anyway) and it’s there in Fedora and Red Hat Linux as the default. Now I want to learn some of its more complicated features, including “tab completion” and “shell scripting”, whatever they are, along with configuring Bash scripts.

Vim is a text editor which, when I first loaded it, seemed completely confusing. Then I found, with the help of Google, that you can type in vimtutor at the command line, er, Bash shell, and it helps you understand what’s going on. Up to now I’ve been using Pico (which I started using on the Solaris systems we had at University) and, more recently, GNU Nano (an open source clone). Vim is nowhere near as intuitive as Nano but high end users seem to like it.

Vim is wierd compared with other non-GUI software. Normally what you type is entered into your document and then you use Ctrl or Alt and another key, or you press a function key, to perform a command. Not so with Vim. Here, you switch between two modes. Esc gets you into Normal Mode, where pressing ordinary keys initiates commands, while pressing i switches the mode so that keys enter text into the document. I’m sure I’ll get used to it eventually!


Why this blog?

03Dec07

Every couple of years, since about 2000, I have dabbled into using Linux on the desktop. Of course, I’ve used it for hosting websites like many people. But Linux on the desktop has always, for me, been more of an educational experience, and sometimes a bit frustrating to be honest. Yet each time I return to Linux, it’s hugely improved.

Work-wise, Linux is going to be a more important part of what I do in the future. This blog exists to help me journal what I learn – and maybe others will find what I write here occasionally useful too.