Archive for November, 2007

242 CHAPTER 15 WORKING (Free web hosting music) WITH TEXT FILES

Friday, November 9th, 2007

242 CHAPTER 15 WORKING WITH TEXT FILES Let s look at using less to read the OpenOffice.Org README file, which contains information about the current release of the office suite. The file is located at /usr/lib/openoffice2/README, so to use less to read it, type the following: less /usr/lib/openoffice2/README You can scroll up and down within the less display by using the cursor keys. If you want to scroll by bigger amounts of text, you can use the Page Up and Page Down keys. Alternatively, you can use the spacebar and B key, both of which are commonly used by old-hand Linux users for the same function. In addition, the Home and End keys will take you to the start and end of the document, respectively. When using less, keep an eye on the bottom part of the screen, where you ll see a brief status bar. Alongside the filename, you ll see how many lines the document has and which line you re currently up to. In addition, you ll see as a percentage the amount of document you ve already read through, so you ll know how much is left to go. less lets you search forward through the file by typing a slash (/), and then entering your search term. Any words that are matched will be highlighted on screen. To repeat the search, type n. To search backward in a file from your current point, type a question mark (?). To quit less, simply type q. Although it s supposedly a simple program, less is packed with features. You can see what options are available by reading its man page or by typing less –help. Using the head and tail Commands A couple of other handy commands that you can use to view text files are head and tail. As their names suggest, these let you quickly view the beginning (head) of a file or the end (tail) of it. Using the commands is simple: tail mytextfile or head mytextfile By default, both commands will display ten lines of the file. You can override this by using the -n command option followed by the number of lines you want to see. For example, the following will show the last five lines of mytextfile: tail -n5 mytextfile These two commands are very useful when viewing log files that might contain hundreds of lines of text. The most recent information is always at the end, so tail can be used to see what s happened last on your system, as shown in the example in Figure 15-1. Although they re powerful, all of these shell commands don t let you do much more than view text files. If you want to edit files, you ll need to use a text editor such as vi.

CHAPTER 15 Working with Text (Web hosting support)

Thursday, November 8th, 2007

CHAPTER 15 Working with Text Files Windows views text files as just another file type, but to Ubuntu, they can be essential components that make the system work. Configuration files are stored as plain text, and program documentation is also stored as text. This is clearly different from Windows, where it s very likely any information you re supposed to read will be contained in a Windows Help file, a rich text format (RTF) file, or even a Microsoft Word document. Because of the reliance on text files, the shell includes several commands that let you display, edit, and otherwise manipulate text files in various ways. Learning to use the shell, and therefore learning how to administer your Ubuntu system, involves having a good understanding of these text tools. You ll use text tools for editing configuration files and viewing log files, as just two examples. Viewing Text Files You can easily view files using command-line tools, including cat, less, head, and tail. The simplest command for dealing with text files is cat. Using the cat Command When followed with a filename, the cat command will display the text file on screen: cat mytextfile cat is short for concatenate, and it isn t designed just to display text files. That it can do so is simply a side effect of its real purpose in life, which is to join two or more files together. However, when used with a single file, it simply displays its contents on screen. If you try to use cat, you ll realize that it s good for only short text files; large files scroll off the screen. Using the less Command Because cat works well only with short files, and to give you more control when viewing text files, the less and more commands were created. The more command came first but was considered too primitive, so someone came up with less, which is preferred by many Linux users. However, both are usually available on the average Linux installation.

CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS (Michigan web site)

Thursday, November 8th, 2007

CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS Summary In this chapter, we examined how the Ubuntu file system lies at the heart of an understanding of how the operating system works. We also discussed how the file system and user accounts go hand-in-hand and are inextricably linked. This involved discussing the concept of file ownership and usage permissions, plus how these can be manipulated using command-line shell tools. We also discussed the overall structure of the Ubuntu file system and how external file systems can be mounted and made available within Ubuntu. Finally, we looked at how to find files and how to gauge how much free space there is within the file system. In the next chapter, we ll look at how the BASH shell can be used to view and otherwise manipulate text files, which are also important to the way Ubuntu works.

238 CHAPTER 14 (Disney web site) UNDERSTANDING LINUX FILES AND

Wednesday, November 7th, 2007

238 CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS However, du is limited by the same file permission problems as the find tool, as shown in Figure 14-6. If you run du as an ordinary user, it won t be able to calculate the total for any directories you don t have permission to access. Therefore, you might consider prefacing the command with sudo. Figure 14-6. The du command shows the size of a file, and the df command can be used to gauge the amount of free space on the disk. Finding Out the Amount of Free Space What if you want to find out how much free space is left on the disk? In this case, you can use the df command. This command is also demonstrated in Figure 14-6. The df command works on a partition-by-partition basis. Typing it at the command prompt will show you how much space is free on the entire disk. Once again, you can add the -h option to the dfcommand to have the file sizes returned in megabytes and gigabytes (and even terabytes if your hard disk is big enough!). The df command also returns information about something called tmpfs. These are temporary file stores and you can ignore them. Note There is as much space free in any directory as there is space on the disk, which is why df displays data about the entire partition. If you re using a system managed by a system administrator within a business environment, you might find that quotas have been used to limit how much disk space you can take up. However, if you re using a desktop PC and are the only user, this won t be activated.

Space web hosting - CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS

Tuesday, November 6th, 2007

CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS It s possible to update the locate database manually, although this might take a few minutes to work through. Simply issue the command: sudo updatedb After this, all files in the system should be indexed, making your search results more accurate. Using the whereis Command One other command worth mentioning in the context of searching is whereis. This locates where programs are stored and is an excellent way of exploring your system. Using it is simply a matter of typing something like this: whereis cp This will tell you where the cp program is located on your hard disk. It will also tell you were its source code and man page are located (if applicable). However, the first path returned by the search will be the location of the program itself. File Size and Free Space Often, it s necessary to know how large files are and to know how much space they re taking up on the hard disk. In addition, it s often handy to know how much free space is left on a disk. Viewing File Sizes Using the ls -l command option will tell you how large each file is in terms of bytes. Adding the -h option converts these file sizes to kilobytes, megabytes, and even gigabytes, depending on how large they are. In order to get an idea of which are the largest files and which are the smallest, you can add the -S command option. This will order the files in the list in terms of the largest and smallest files. The following will return a list of all the files in the current directory, in order of size (largest first), detailing the sizes in kilobytes, megabytes, or gigabytes: ls -Slh There s another more powerful way of presenting this information: using the du command, which stands for disk usage. When used on its own without command switches, du simply presents the size of directories alongside their names (starting in the current directory). It will show any hidden directories (directories whose names start with a period) and will also present a total at the end of the list. This will probably be quite a long list. Once again, you can add the -h command option to force the du command to produce human-readable measurements of kilobytes and megabytes. If you specify a file or directory when using the du command, along with the -s command option, you can find out its total file size: du -sh mydirectory This will show the size taken up on the disk by mydirectory, adding to the total any files or subdirectories it contains.

Web and email hosting - 236 CHAPTER 14 UNDERSTANDING LINUX FILES AND

Tuesday, November 6th, 2007

236 CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS Figure 14-5. The find command is useful for finding files but isn t problem-free. You can avoid these error messages in various ways, but perhaps the quickest solution is to preface the find command with sudo to invoke superuser powers. In this way, you ll have access to every file on the hard disk, so the find command will be unrestricted in where it can search and won t run into any directories it doesn t have permission to enter. Caution Using the sudo command with find may represent an invasion of privacy if you have more than one user on your system. The find command will search other users /home directories and report any instances of files found there, too. However, an even better solution for finding files is to use the locate command. Using the locate Command The alternative to using find is to use the locatecommand. This is far quicker than find because it relies on a central database of files, which is periodically updated. In other words, it doesn t literally search the file system each time. The problem is that if you ve saved a file recently and are hoping to find it, there s a chance that it won t yet appear in locate s database, so it won t turn up in the list of results. Using locate is easy. You can use the following command to search for a file (you don t need to precede the command with sudo): locate myfile

Jetty web server - CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS

Monday, November 5th, 2007

CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS If you re currently browsing the mounted directory, you ll need to leave it before you can unmount it, no matter whether you choose to do this manually or by hitting an eject button. The same is true of all kinds of access to the mounted directory. If you re browsing the mounted drive with Nautilus or if a piece of software is accessing it, you won t be able to unmount it until you ve quit the program and closed the Nautilus window (or browsed to a different part of the file system). File Searches Files frequently get lost. Well, technically speaking, they don t actually get lost. We just forget where we ve put them. But because of this, the shell includes some handy commands to search for files. Using the find Command Like the Search option on the Windows XP Start menu, the find command manually searches through all the files on the hard disk. It s not a particularly fast way of finding a file, but it is reliable. Here s an example: find /home/keir -name “myfile” This will search for myfile using /home/keir as a starting point (which is to say that it will search all directories within /home/keir, and any directories within those directories, and so on, because it s recursive). You can search the entire file system by leaving out the initial path. In this case, find will assume you want the search to start from /, the root of the file system. If the file is found, you ll see it appear in the output of the command. The path will be shown next to the filename. Period punctuation symbols have interesting meanings within file listings and therefore within the output of the find command. As you learned in Chapter 13, .. refers to the parent directory of the one you re currently browsing. In a similar way, a single . refers to the directory you re in at the moment; it s shorthand for right here. So, if find returns a result like ./myfile, it means that myfile is right here in the current directory. However, when a single period is used at the beginning of a filename, such as in .bashrc, it has the effect of hiding the file. In other words, that file won t appear when you type ls (although you can type ls -a to see all files, even those that are hidden). If you give find a try, you ll see that it s not a particularly good way of searching. Apart from being slow, it will also return a lot of error messages about directories it cannot search. This is because, when you run the find command, it takes on your user permissions. Whenever find comes across a directory it cannot access, it will report it to you, as shown in the example in Figure 14-5. There are frequently so many of these warnings that the output can hide the instances where find actually locates the file in question!

Best web hosting site - 234 CHAPTER 14 UNDERSTANDING LINUX FILES AND

Sunday, November 4th, 2007

234 CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS With this information in hand, you re now ready to mount the disk. For a FAT32 disk, type the following: sudo mount -t vfat o umask=000 /dev/hdb1 /mnt/windows For an NTFS disk, type the following: sudo mount -t ntfs -o umask=0222 /dev/hdb1 /mnt/windows The -t command option is used to specify the file system type. The -o flag indicates that you re going to specify some more command options and you do so in the form of umask, which tells mount to ensure that the directory is readable (and writable in the case of the FAT32 drive). After this, you specify the relevant file in the /dev directory (this file is only virtual, of course, and merely represents the hardware), and then specify the directory that is acting as your mount point. Note You can only read from NTFS drives under Linux because writing to them is considered too risky and might result in data loss. However, you can both read and write to FAT32 partitions because of the much simpler technology used. Now when you browse to the /mnt/windows directory, by typing cd /mnt/windows, you should find the contents of the hard disk accessible. For more information about the mount command, read its man page (type man mount). Removing a Mounted System To unmount a system, you use the special command umount (notice there s no n after the first u). Here s an example of using the command to unmount the CD-ROM drive: sudo umount /media/cdrom You need to tell the umount command where to find the mounted contents. You can specify either the mount directory, as in the example, or the file in the /dev directory that refers to mounted resource (this is a little complicated, so in most cases, it s better to simply specify the file system location of the mount). Note Sometimes you can automatically unmount a CD-ROM by hitting the eject button. This is because Ubuntu is clever enough to automatically unmount the CD if the button is pressed. However, if a program is accessing the CD, or if the CD is being browsed, the button won t work until all access to the CD has ceased.

Java web server - CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS

Sunday, November 4th, 2007

CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS Figure 14-4. Details of all frequently mounted file systems are held in the /etc/fstab file. Mounting a Drive Manually Let s look at an example of when you might need to mount a drive manually. Suppose that you ve just added a second hard disk to your PC that has previously been used on a Windows system. This has been added as the primary slave. The first thing to do is create a mount point, which is an empty directory that will act as a location where you can tell mount to make the disk accessible. You can create this directory anywhere, but under Linux, the convention is to create it in the /mnt directory. Therefore, the following command should do the trick (note that you need to use the sudo command because writing to any directory other than your /home directory requires administrator privileges): sudo mkdir /mnt/windows You now need to know what kind of partition type is used on the disk, because you need to specify this when mounting. To find this out, use the fdisk command. Type the following exactly as it appears: sudo fdisk -l /dev/hdb This will list the partitions on the second disk drive (assuming an average PC system). With most hard disks used under Windows, you should find a single partition that will be either NTFS or FAT32. The examples here assume that this is hdb1. Caution Be aware that fdiskis a dangerous system command that can damage your system. The program is designed to partition disks and can wipe out your data if you re not careful!

232 CHAPTER 14 UNDERSTANDING LINUX FILES (Web hosting ecommerce) AND

Saturday, November 3rd, 2007

232 CHAPTER 14 UNDERSTANDING LINUX FILES AND USERS Mounting Described in technical terms, mounting is the practice of making a file system available under Linux. This can take the form of a partition on your hard disk, a CD-ROM, a network server, or many other things. Mounting drives might seem a strange concept, but it actually makes everything much simpler than it might be otherwise. For example, once a drive is mounted, you don t need to use any special commands or software to access its contents. You can use the same programs and tools that you use to access all of your other files. Mounting creates a level playing field on which everything is equal and can therefore be accessed quickly and efficiently. Using the mount Command Mounting is usually done via the mount command. Under Ubuntu, you must have administrator powers to use the mount command, which means prefacing it with sudo and providing your password. With most modern versions of Linux, mount can be used in two ways: by specifying all the settings immediately after the command, or by making reference to an entry within the fstab file. fstab is a configuration file stored in the /etc directory that contains details of all file systems on the PC that can be mounted. Note The root file system is itself mounted automatically during bootup, shortly after the kernel has started and has all your hardware up and running. Every file system that Linux uses must be mounted at some point. Let s say that you insert a CD or DVD into your computer s DVD-ROM drive. To mount the CD or DVD and make it available to Linux (something that is actually done automatically as soon as you put a disk in the drive, so this example is for demonstration purposes only), you would type the following: sudo mount /cdrom The mount command first looks in your fstab file in the etc directory to find what you re referring to. Figure 14-4 shows an example of the contents of that file. (The example in the figure uses the cat command, which is discussed in Chapter 15). Using this information, the mount command attempts to make the contents of the CD available in the /cdrom directory. Note that this is done in a virtual way; the files are not literally copied into the directory. The directory is merely a magical conduit that allows you to read the CD s contents. There aren t any special commands used to work with mounted drives. The shell commands discussed in Chapter 13 should do everything you need. The mount command doesn t see widespread usage by most users nowadays, because most removable storage devices like CDs, and even memory card readers, are mounted automatically under Ubuntu and an icon for them appears on the desktop. However, there may be occasions when you need to mount a drive manually.