-->
Previous | Table of Contents | Next |
After youve been working on your Linux system for a while, youll accumulate many files if you do any serious work at all. If youre a careful worker, youll be able to keep track of files by their locations and filenames. However, if youre not a careful worker, you may run into situations where you have no idea what a file contains. You dont 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 numbersfound somewhere in the binary fileshould 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, youd use the following command line:
gilbert:~$ file 45edfsdwe
At the very least, file will tell you the files type (executable, ordinary, etc.) and how its compiled (such as dynamically linked). If youre lucky, the file command will also tell you if the file is related to your machine. However, if this file is merely text, youll see the following information:
gilbert:~$ file 45edfsdwe 45edfsdwe text
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 users machine (provided youre 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 erics home directory.
You may want to give textfile a new name when its moved to the new directory. In this case, youre giving textfile a new name of textfile.kr when its 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 thingas 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, doesnt check to see if theres 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.
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 its 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 |