-->
Previous Table of Contents Next


Finding the Magic Number with File

After you’ve been working on your Linux system for a while, you’ll accumulate many files if you do any serious work at all. If you’re a careful worker, you’ll be able to keep track of files by their locations and filenames. However, if you’re not a careful worker, you may run into situations where you have no idea what a file contains. You don’t want to view a binary file with cat or another command designed to view text files, because doing so will probably result in huge amounts of garbage being displayed to your screen (which may require you to try and relogin your system).

Linux features a command, file, that will look at a file and return specific information about the contents of the file (most of the time, anyway). The UNIX world of late has supported magic numbers, and in theory these numbers—found somewhere in the binary file—should match a database of magic numbers on your system. (These magic numbers can be found in the /etc/magic file.) To run file on a file named 45edfsdwe, you’d use the following command line:


     gilbert:~$ file 45edfsdwe

At the very least, file will tell you the file’s type (executable, ordinary, etc.) and how it’s compiled (such as dynamically linked). If you’re lucky, the file command will also tell you if the file is related to your machine. However, if this file is merely text, you’ll see the following information:


     gilbert:~$ file 45edfsdwe

     45edfsdwe                       text

Copying Files with Cp

The cp command is used to copy existing files. When you use the cp command, the original file is left intact. This is handy when copying files to another user’s machine (provided you’re networked, of course) or to another directory for backup purposes. (There are more formal ways to make system backups on your Linux system, of course, but the cp command works well for single files or small groups of files.) The following command line copies a file named textfile to the /home/eric directory:


     gilbert:~$ cp textfile /home/eric

When this command is run, the file named textfile will appear in both your home directory and eric’s home directory.

You may want to give textfile a new name when it’s moved to the new directory. In this case, you’re giving textfile a new name of textfile.kr when it’s moved to the /home/eric directory:


     gilbert:~$ cp textfile /home/eric/textfile.kr


WARNING:  Linux will do exactly what you tell it to do. In some cases, this is a good thing. In other cases, this is a very bad thing—as can be the case with the cp and mv commands.

If (using the previous command-line example) there were already a file called textfile.kr in the /home/eric directory, the cp command would overwrite the existing file with the new file. The cp command, by default, doesn’t check to see if there’s a file already in that directory with the same name. (The same goes for the mv command; this will be covered in the next section, “Moving and Renaming Files with mv.”)

On the other hand, both the cp and mv commands have an option (-i) that prevents you from overwriting existing files, as seen in this command line:


     gilbert:~$ cp -i textfile /home/eric/textfile.kr

     cp: overwrite `textfile.kr'?

If you type y, cp will overwrite the existing textfile.kr. If you type anything else, cp will not overwrite the file.

Options to the cp command are listed in Table 4.8.

Table 4.8 Options to the cp Command
Option Result

-d Maintains a symbolic link as a link rather than as a copy of the original file.
-i Prevents overwriting of existing files with the same filename.
-p Retains the existing permissions.
-r Copies the entire directory structure, including subdirectories.
-v Runs in verbose mode; lists each file as it’s copied.

Copying Directories with Cp

cp also has the power to copy entire directories (including all files and subdirectories), in the form of the -r option:


     gilbert:~$ cp -r /users/data /users/eric


Previous Table of Contents Next