Available CategoriesAdobeMacromediaProgrammingSQLServer AdministrationNetworkingMicrosoft ProductsMac OSLinux systemsMobile devicesXMLCertificationMiscAvailable TutorialsLearning red hat enterprise linux and fedoraRed Hat Linux 9 Professional Secretsred hat linux bible. fedora and enterprise editionEmbedded Linux SystemsSecure Linux-based ServersRed HatLinux securityRunning LinuxMoving to LinuxCluster Computing with LinuxHow linux worksPDF hacksPpractical unix & internet security |
File Utilities and the find CommandFile Utilities and the find CommandThe file utilities include programs you can use to work with files and directories in your Red Hat Linux system. Table 8-3 briefly describes each of these programs. You can copy and delete files, create new directories, and change file permissions and ownerships. You can also list directories and see the amount of disk space the files use. Because you need to use the file utilities often, the next few sections show you how to use some of them. Also provided is a brief introduction to the find command, which enables you to locate all files that meet specified criteria.
Working with FilesOften, you may copy files from one directory to another. Use the cp command to perform this task. To copy the file /usr/X11R6/lib/X11/xinit/Xclients to the Xclients.sample file in the current directory (such as your home directory), type the following: cp /usr/X11R6/lib/X11/xinit/Xclients Xclients.sample If you want to copy a file to the current directory and retain the same name, use a period (.) as the second argument of the cp command. Thus, the following command copies the XF86Config file from the /etc/X11 directory to the current directory (denoted by a single period): cp /etc/X11/XF86Config . The cp command makes a new copy of a file and leaves the original intact. Another common file operation is deleting a file. Use the rm command to delete a file named old.list, for example, by typing the following command: rm old.list
Manipulating DirectoriesTo organize files in your home directory, you have to create new directories. Use the mkdir command to create a directory. For example, to create a directory named images in the current directory, type the following: mkdir images After you create the directory, you can type the cd images command to change to that directory. You can create an entire directory tree by using the -p option of the mkdir command. For example, suppose that your system has a /usr/src directory and you want to create the directory tree /usr/src/book/java/examples/applets. You can create this directory hierarchy by typing the following command: mkdir -p /usr/src/book/java/examples/applets When you no longer need a directory, use the rmdir command to delete it. You can delete a directory only when the directory is empty. To remove an empty directory tree, you can use the –p option, like this: rmdir -p /usr/src/book/java/examples/applets This command removes the empty parent directories of applets. The command stops when it encounters a directory that’s not empty. Copying Disks with ddThe dd command is useful for copying binary data to a floppy disk (or to other devices, such as tape). For example, if you want to make a Red Hat boot disk on a Linux system, you can use the dd command to prepare the boot disk before you install Red Hat Linux from this book’s companion CD-ROMs. Perform the following steps to prepare the boot disk:
If you want to copy another boot image to the floppy, replace bootdisk.img with that file’s name. Viewing Disk Usage InformationTwo programs in the GNU file utilities—df and du—enable you to check disk-space usage. These commands are simple to use. The df command shows you a summary of disk-space usage for all mounted devices, as shown in the following example: df Filesystem 1K-blocks Used Available Use% Mounted on /dev/hda5 7392428 3412688 3604224 49% / /dev/hda3 101107 8865 87021 10% /boot none 127328 0 127328 0% /dev/shm /dev/cdrom 75322 75322 0 100% /mnt/cdrom The output is a table that shows the device, the total kilobytes of storage, how much is in use, how much is available, the percentage being used, and the mount point. For example, on my system, the /dev/hda5 device (a disk partition) is mounted on the Linux file system’s root directory; it has about 7.4GB of space, of which 3.4GB (or 49 percent) is being used, and 3.6GB is available. Similarly, you can see from the last line that the CD-ROM has about 75MB of storage in use. The other command, du, is useful for finding out how much space a directory is using. For example, type the following command to view the contents of all the subdirectories in the /var/log directory (this directory contains various error logs): du /var/log 4 /var/log/vbox 12528 /var/log/cups 4 /var/log/samba 4 /var/log/news/OLD 8 /var/log/news 60 /var/log/httpd 4 /var/log/squid 36 /var/log/gdm 14764 /var/log Each directory name is preceded by a number—that number denotes the number of kilobytes of disk space that directory uses. Thus, the /var/log directory, as a whole, uses 14764KB, or about 14.7MB, of disk space, whereas the /var/log/httpd subdirectory uses 60KB. You can use the -h option with the du command to view the disk-space usage in human-readable format. For example, here’s what you get when you type du -h /var/log to view the disk space used by the /var/log directory and its contents: du -h /var/log 4.0K /var/log/vbox 13M /var/log/cups 4.0K /var/log/samba 4.0K /var/log/news/OLD 8.0K /var/log/news 60K /var/log/httpd 4.0K /var/log/squid 36K /var/log/gdm 15M /var/log If you simply want the total disk space a directory uses (including all the files and subdirectories contained in that directory), use the -s option together with the -h option, as follows: du -sh /var/log 15M /var/log Notice that the -s option causes du to print just the summary information for the /var/log directory. Learning the find CommandThe find command is very useful for locating files (and directories) that meet specified search criteria. The Linux version of the find command also comes from GNU, and it has more extensive options than the standard UNIX version. I show the syntax for the standard UNIX find command, however, because it syntax works in GNU find, and you can use the same format on other UNIX systems. I must admit that when I began using UNIX many years ago (Berkeley UNIX in the early 1980s), I was confounded by the find command. I stayed with one basic syntax of find for a long time before graduating to more complex forms. The basic syntax I learned first was for finding a file anywhere in the file system. Suppose that you want to find any file or directory with a name that starts with gnome. You can use find to perform this search, as follows: find / -name "gnome*" -print This command tells find to start looking at the root directory (/), to look for filenames that match gnome*, and to display the full pathname of any matching file. You can use variations of this simple form of find to locate a file in any directory (as well as any subdirectories contained in the directory). If you forget where in your home directory you have stored all files named report* (names that start with the text string report), you can search for the files by using the following command: find ~ -name "report*" -print When you become comfortable with this syntax of find, you can use other options of find. For example, to find only specific types of files (such as directories), use the -type option. The following command displays all top-level directory names in your Linux system: find / -type d -maxdepth 1 -print To find all files that exceed 20,000KB (20MB) in size, you can use the following find command: find / -size +20000k -print You probably do not have to use the complex forms of find very often in a typical Linux system, but you can look up the rest of the find options by using the following command: man find
|