342 Networking: A Beginner’s Guide File Management and Manipulation This section provides an overview of the basic command-line tools for managing files and directories. Most of this overview should be familiar if you have used a command- line interface before. Basically, you use the same old functions, just with new commands. cp: Copy Files The cp command is used to copy files. By default, this command works silently, displaying status information only if there is an error condition. For example, to copy index.html to index-orig.html, type the following: [root@ford /root]# cp index.html index-orig.html The cp command has a large number of options, which are detailed on its man page. The most common options are -f to force copy (do not ask for verification) and -1 for an interactive copy (ask for verification before copying). For example, to copy interactively all files ending in .html to the /tmp directory, type the following: [root@ford /root]# cp -i *.html /tmp mv: Move Files Use the mv command to move files from one location to another. The command can move files across partitions as well; however, realize that when moving a file between partitions that you are actually copying the file to the other partition, and then erasing the original, so it can take longer than moving a file within a partition. Moving a file within a single partition just tells the system that the file is in a different directory; the file isn’t copied or physically moved on the disk. For example, to move a file from /usr/src/myprog/bin/* to /usr/bin, type the following: [root@ford /root]# mv /usr/src/myprog/bin/* /usr/bin The most common options are -f to force a move and -l to move interactively. Although Linux has no explicit rename tool, you can use mv to accomplish this task. To rename /tmp/blah to /tmp/bleck, type the following: [root@ford /root]# mv /tmp/bleck /tmp/blah ln: Link Files The ln command allows you to establish a hard link or a soft link. (See the “About Files and Directories” section earlier in this chapter for additional information.) The general format of this command is as follows: [root@ford /root]# ln original_file new_file The ln command has many options, most of which you’ll never need to use. The most common option is -s, which creates a symbolic link instead of a hard link. For 343 Chapter 21: Introduction to Linux Systems Administration example, to create a symbolic link so that /usr/bin/myadduser points to /usr/ local/ bin/myadduser, type the following: [root@ford /root]# ln -s /usr/local/bin/myadduser /usr/bin/myadduser find: Find a File The find command enables you to find files based on a number of criteria. The following is the command’s general format: [root@ford /root]# find start_dir [options] where start_dir is the directory from which the search should start. find, like the other commands that we have already discussed, has a large number of options that you can read about on its man page. Table 21-5 list the most common options used with find. Option Description -mount Do not search file systems other than the file system from which you started. -atime n Specify that the file was accessed at least n*24 hours ago. -ctime n Look only for files changed at least n*24 hours ago. -inum n Find a file that has i-node n. -amin n Specify that the file was accessed n minutes ago. -cmin n Look only for files that were changed n minutes ago. -empty Find empty files. -mmin n Specify that the file was modified n minutes ago. -mtime n Search only for files modified n*24 hours ago. -nouser Find files whose UID does not correspond to a real user in /etc/passwd. -nogroup Look only for files whose GID does not correspond to a real group in /etc/group. -perm mode Specify that the file’s permissions are exactly set to mode. -size n[bck] Search only for files at least n blocks/characters/kilobytes big. One block equals 512 bytes. -print Print the filenames found. -exec cmd\; On every file found, execute cmd. If you are using the bash shell, be sure to follow every cmd with a \; otherwise, the shell will become very confused. -name name Specify that the filename should be name. You can use regular expressions here. Table 21-5. Common find Command Options 344 Networking: A Beginner’s Guide For example, to find all files in /tmp that have not been accessed in at least seven days, type the following: [root@ford /root]# find /tmp -atime 7 -print To find all files in /usr/src whose names are core and then remove them, type the following: [root@ford /root]# find /usr/src -name core -exec rm {} \; To find all files in /home with the extension .jpg that are bigger than 100KB, type the following: [root@ford /root]# find /home -name "*.jpg" -size 100k dd: Convert and Copy a File The dd command reads the contents of a file and sends them to another file. What makes dd different from cp is that dd can perform on-the-fly conversions on the file and can accept data from a device (such as a tape or floppy drive). When dd accesses a device, it does not assume anything about the file system and instead pulls the data in a raw format. Thus, you can use the data to generate images of disks, even if the disk is of foreign format. Table 21-6 lists the most common parameters for dd: For example, to generate an image of a floppy disk (which is especially useful for foreign file formats), type the following: [root@ford /root]# dd if=/dev/fd0 of=/tmp/floppy_image Option Description if=infile Specify the input file as infile. of=outfile Specify the output file as outfile. count=blocks Specify blocks as the number of blocks on which dd should operate before quitting. ibs=size Set the block size of the input device to be size. obs=size Set the block size of the output device to be size. seek=blocks Skip blocks number of blocks on the output. skip=blocks Skip blocks number of blocks on the input. swab Convert big endian input to little endian, or vice versa. Table 21-6. Common dd Command Options 345 Chapter 21: Introduction to Linux Systems Administration gzip: Compress a File In the original distributions of UNIX, a tool to compress a file was appropriately called compress. Unfortunately, an entrepreneur patented the algorithm, hoping to make a great deal of money. Instead of paying out, most sites sought and found gzip, a different compression tool with a patent-free algorithm. Even better, gzip consistently achieves better compression ratios than compress does. Note that gzip compresses the file “in place,” meaning that after the compression takes place, the original file is removed, leaving only the compressed file. TIP You can usually distinguish files compressed with gzip from those compressed by compress by checking their extensions. Files compressed with gzip typically end in .gz whereas files compressed with compress end in .Z. Table 21-7 lists the most-used optional parameters to gzip. See the man page for a complete list. For example, to compress a file and then decompress it, type the following: [root@ford /root]# gzip myfile [root@ford /root]# gzip -d myfile.gz To compress all files ending in .html using the best compression possible, enter the following command: [root@ford /root]# gzip -9 *.html NOTE The gzip tool does not share file formats with either PkZip or WinZip. However, WinZip can decompress gzip files. Option Description -c Write compressed file to the standard output device (thereby allowing the output to be piped to another program). -d Decompress. -r Recursively find all files that should be compressed. -9 Provide the best compression. -1 Achieve the fastest compression. Table 21-7. Common gzip Command Options 346 Networking: A Beginner’s Guide mknod: Make Special Files As discussed earlier, Linux accesses all of its devices through files. To create a file that the system understands as an interface to a device, you must specify that the file is of type block or character and has a major and minor number. To create this kind of file with the necessary values, you use the mknod command. In addition to creating interfaces to devices, you can use mknod to create named pipes. The command’s format is as follows: [root@ford /root]# mknod name type [major] [minor] where name is the name of the file; and type is the character b for block device, c for character device, or p for named pipe. If you choose to create a block or character device, you need to specify the major and minor number. The only time you will need to create a block or character device is when installing some kind of device driver that requires it. The documentation that comes with your driver should tell you which values to use for the major and minor numbers. For example, to create a named pipe called /tmp/mypipe, type the following: [root@ford /root]# mknod /tmp/mypipe p mkdir: Create a Home Directory The mkdir command in Linux is identical to the one in other flavors of UNIX as well as in MS-DOS. For example, to create a directory called mydir, type the following: [root@ford /root]# mkdir mydir The only option available is -p, which creates a parent directory if none exists. For example, if you need to create /tmp/bigdir/subdir/mydir, and the only directory that exists is /tmp, using -p will automatically create bigdir and subdir along with mydir. NOTE Under Linux, you cannot abbreviate the mkdir command as md as you can under DOS. rmdir: Remove Directory The rmdir command offers no surprises if you are familiar with the DOS version of the command. It simply removes an existing directory. For example, to remove a directory called mydir, type the following: [root@ford /root]# rmdir mydir One command-line parameter available for this command is -p, which removes parent directories as well. For example, if the directory /tmp/bigdir/subdir/mydir exists and you want to get rid of all of the directories from bigdir to mydir, issue the following command: [root@ford /tmp]# rmdir -p bigdir/subdir/mydir . and can accept data from a device (such as a tape or floppy drive). When dd accesses a device, it does not assume anything about the file system and instead pulls the data in a raw format. Thus,. files. To create a file that the system understands as an interface to a device, you must specify that the file is of type block or character and has a major and minor number. To create this kind. original distributions of UNIX, a tool to compress a file was appropriately called compress. Unfortunately, an entrepreneur patented the algorithm, hoping to make a great deal of money. Instead