UNIX Lecture 6
As it appeared when it was first sent via email.
Greetings to You,
This is UNIX Lecture 6. If you are new and need to catch
up, please visit the following link to read all of the previous
lectures:
http://www.russiannewsnetwork.com/unix-lectures.html
Today I will continue talking about UNIX commands.
The wc command
This command is pretty handy because it counts the number of
new line characters and words in whatever file you specify.
At your prompt, type: wc fileA
and you will get three numbers as output.
For example, lets say that your file, called fileA has
115 lines, 374 words and 2825 characters.
If you type: wc fileA, the output will look
like this:
115 374 2825 fileA
So you get three numbers, separated by spaces, and the
file name one the right hand side, telling you that those
three numbers correspond to the number of lines, words,
and characters found in fileA.
The cp command
This command is used to copy files.
Some options to use with the copy command:
-i prompts for confirmation
-p preserve ownergroup and permission
-r recursive
An example:
If you type the following at your prompt:
cp file_A file_B
where file_A and file_B are two different files,
the command will copy the contents of file_A onto
file_B.
If file_B is an existing file, then the contents are
replaced by file_A.
If file_B does NOT exist, then it is created with the
content of file_A.
If you want to copy several files to a directory, you can
do it all at the same time in the following way:
At the prompt, type:
cp file_A file_B file_C file_D directory_to_copy_to
To make it more clear, after the "cp", just type in
the file names of the files that you would like to
copy, followed by the name of the directory into which
you would like them to be copied to. (note: separate file
names and the name of the directory by a space).
The ln command
The ln command is used to make a copy of a file without actually
making a copy of it on the disk. This saves disk space.
To clarify, the cp command makes a copy of a file, and the end result
is that there are two copies of the copied file on the disk. The ln
command copies the file, using a different name, but does not store
the copies file on the computer's disk.
The letters "ln" were chosen to represent and to execute this
command because the end result of this command is essentially
a "link".
Here's how to use it:
At the prompt, type:
ln file_1 and file_2
where file_1 is an existing file and file_2 is the new file name.
Now, because you are using the ln command ("link" command), both
file_1 AND file_2 refer to THE SAME AREA OF THE DISK.
Thus, no distinction is made between file_1 and file_2, because both
names refer to the same file on the disk (basically this means that
there is no hierarchy between the two files).
Some things to remember about the ln command:
- Any number of links can be assigned to a file
- The link created is referred to as being a "hard link"
- The link you create with the ln command will work only
within the file system.
- You can link across file systems using the -s option WITH
the ln command, like this: ln -s etc etc...
- A link across a file system, using the -s option is
referred to as a symbolic link.
- To remove a link created with the ln command, you use
the same command you would use to remove a file.
The mv command
This command is used to move files and directories around in
the file system. Besides being useful because it allows you
to move files and directories, it also keeps intact the
following useful information about your files and directories:
groupid, userid, time, and date.
Important mv option: -f
This option is used to suppress system prompting.
Here's an example using the mv command:
At your prompt, type:
mv file_1 file_2.
What do you think that this will do?
It will move file_1 into file_2, which means that
file_1 will be renamed file_2.
If file_2 already exists, its contents will be replaced
by file_1.
If file_2 does not exist, a new file named file_2 will be created
with the contents of file_1.
With directories...
if at your prompt, you type:
mv directory_1 directory_2
what will happen?
It will move directory_1 to directory_2.
If directory_2 does not exist, it will be created.
If directory_2 does exist, the mv command will not work.
The following is also a useful way to use the mv command:
If you type:
mv file_1 file_2 directory_1
will move file_1 and file_2 into directory_1
Also, if you wish to rename a file, you must
use the mv command.
The rm command
This command is used to remove files. When you remove
a file, you have deleted a file.
Options/Usage
If you type the following things at your prompt:
rm file_1
..........you will remove (delete) file_1
rm file_1 file_2 file_3 file_4 file_5
..........you will remove (delete) all 5 files.
rm -r directory_1
..........you will remove all files and directories in
directory_1, and directori_1 itself, recursively.
rm -i file_1
..........the system will prompt you to confirm your
desire to remove (delete) file_1.
rm -f file_1
..........the system will remove (delete) the file without
asking you to confirm your desire to do so.
The mkdir command
This command, as you can probably guess by trying to "say it",
is used to create (make) a new directory. Keep in mind that
to use this command you, the user, must have write permission
in the directory.
Here's how to use it. At your prompt, type:
mkdir directory_1
..........this will create a directory called directory_1
mkdir directory_1 directory_2 directory_3 directory_4
....will create four directories, called.......
directory_1, directory_2, directory_3, directory_4.
If you type:
rmdir, you will remove (delete) EMPTY directories.
For example, typing the following at your prompt:
rmdir directory_1
........will remove (delete) directory_1 if it is an
EMPTY (repeat-->**EMPTY**) directory.
This command only works if the directory is EMPTY.
To remove (DELETE) a directory that is NOT empty, one that
contains files (ie, has content), you have to use the rm
command.
Using the rm -r command is VERY DANGEROUS, as applied to
directories. It can happen, and has happened, that people
only wish to remove a SINGLE FILE from a directory, and end
up deleting the ENTIRE DIRECTORY. This is not a good thing!
So, if you want to remove a file that is in a directory,
make sure that you do not issue the wrong command, and delete
the entire directory.
It is advisable to REMAIN ALERT when issuing commands that you
are not familiar with. Check, double check, and then check again
that you are using the correct command for your desired result.
This is especially important when deleting things.
The file command
This is a nice little command that is used to determine the
nature of a file, ie, the type of file.
Let's say that you find a file in your directory and you
are not sure what it is, using the file command, you can
determine whether it is a text file, a C file, a Fortran
file, a directory, etc.
For example, if at your prompt you type:
file file_1 file_2 file_3 file_4
.....the file command will work on all four files,
giving you the nature of each file (ie, is it a text file,
C file, etc).
Practice your commands so that they will be second nature
to you.
Enjoy your day,
Vladimir Polyakov
PS And a special thanks to my friends at the Russian News
Network website, for their help in this endeavor.
http://www.russiannewsnetwork.com