| < Day Day Up > |
|
In Chapter 3 you were briefly introduced to some file and directory management commands. You were shown how to create directories, copy files, list contents of a directory, and remove files and directories. Several of the commands explored in Chapter 3, and a few new ones that we will examine now, are shown in Table 18.1.
Linux Command | Explanation and Example | Windows Command Prompt Equivalent |
---|---|---|
ls | This lists the contents of the current directory. Example: ls | The dir command is equivalent to the ls command. Example: dir |
cp | This copies one file to another directory. Example: Cp filename.txt directoryname | The copy command is equivalent to the ls command. Example: copy filename.txt directoryname |
mkdir | The mkdir command creates a new directory. Example: mkdir directoryname | The md command is the equivalent of the mkdir. Its syntax is identical to mkdir. Example: md directoryname |
cd | The cd command is used to change directories. Example: cd directory name | The cd command is also used in Windows, and its syntax is identical. Example: cd directoryname |
rm | The rm command is used to delete or remove a file. Example: rm filename | The del command is the equivalent to the rm command. Example: del filename |
rmdir | The rmdir command is used to remove or delete entire directories. Example: rmdir directoryname | The rd command is the equivalent of the rmdir command. Example: rd directoryname |
mv | This command is used to move a file. Example: mv myfile.txt myfolder | The equivalent command in Windows is move. Example: move myfile.txt myfolder |
diff | This command performs a byte by byte comparison of two files and tells you what is different about them. Example: diff myfile.txt myfile2.txt | The comp command will perform a binary comparison of two files. |
cmp | This command performs a textual comparison of two files and tells you the difference between the two. Example: cmp myfile.txt myfile2.txt | There is no equivalent command in Windows. |
spell | This command performs a spell check on a target file and tells you if any words are misspelled. Example: spell myfile.txt | There is no equivalent command in Windows. |
head | This command returns just the first few lines of a file. This is useful when searching for a particular file. Rather than opening the entire file, just view the first few lines to see if it is the one you want. Example: head myfile.txt | There is no Windows equivalent. |
cat | This command will concatenate two files. The two files are put into one, one file being appended to the end of the other. Example: cat myfile.text myfile2.text | There is no Windows equivalent. |
> | This is the redirect command. Instead of displaying the output of a command such as ls to the screen, it redirects it to a file. Example: ls > file1.txt | This works in the Windows command prompt in much the same way. |
These two commands are used to compare two files and determine what differences the two files have. In order to demonstrate these commands, we will have to create two sample text files. For demonstration purposes you can open up any Linux text editor (Kate or KWrite, for example) and type in the following three lines:
Linux is very cool I love Linux It is awesome
Save that as file1.txt. Start a new text file and enter these three lines:
Linux is very cool I love Linux It is great
Save that as file2.txt. You should notice that, except for the last line, these two files are identical. This is intentional, as it gives us a chance to explore the diff and cmp commands and find the differences between these two files. Now we can use both the diff command and the cmp command and see what they tell us about these two files. Figure 18.3 shows the output from running the cmp command. Figure 18.4 shows the output from running the diff command.
Figure 18.3: The cmp command.
Figure 18.4: The diff command.
You are probably thinking that the diff command gives you more information than the cmp command, but that is not exactly accurate. The two commands give different output because they compare the files differently. The cmp command compares the files byte by byte in their binary format. Remember that ultimately everything in your computer is stored as ones or zeros (binary format). Therefore, it reports the specific bytes that are different. The diff command performs a textual comparison and returns what line or lines of text are different. Both of these commands can be quite useful. For example, if you have two versions of the same file and want to know exactly what is different between the two, either the diff or the cmp command can tell you.
The next four commands we will look at are very useful when you are working with text files. The first of these commands, the spell command, is used frequently by most Linux users. You use this command to find any misspelled words in a file. When you run the spell command, it will return all words in that file that are misspelled. The misspelled words will be listed vertically as they are in Figure 18.5.
Figure 18.5: The spell command.
The head command is very useful for searching through files. If you are seeking a particular file and do not recall what the filename is, in Windows you would have to open each file that you think might be it and scan it to see if it is indeed the file you are looking for. The head command will return just the first few lines of a file, and that is usually enough to determine if it is the file you are seeking. This is shown in Figure 18.6.
Figure 18.6: The head command.
The cat command, shown in Figure 18.7, takes two files and appends one onto the end of the other. This is a very commonly used shell command. There is no equivalent command in Windows. The concatenated contents are then displayed to the screen.
Figure 18.7: The cat command.
The last, but certainly not least, file management command we will examine is the redirect command. All shell commands by default display their output to the screen. However, there may be instances when you would prefer the output be stored in a file. That is where the redirect command comes in. It causes the output from some other command to be redirected to a file. This is very useful, particularly for some of the system commands we are about to examine. To use the redirect command, enter the command you want to redirect, followed by the greater than sign (>), then the file you want to redirect to. For example, ls > myfile.txt takes the output from the ls command and places it in a file named myfile.txt.
| < Day Day Up > |
|