Lab Plan 2
Lab Plan 2
Lab. Plan – 2
Objective
THEORY
Files & Filenames
The most basic concept of a file defines it as a distinct chunk of information that is found on the hard drive.
Distinct means that there can be many different files, each with its own particular contents. To keep files from
getting confused with each other, every file must have a unique identity. In Linux, you identify each file by its
name and location. In each location or directory, there can be only one file by a particular name. Linux allows
filenames to be up to 256 characters long. These characters can be lower- and uppercase letters, numbers, and
other characters, usually the dash (-), the underscore (_), and the dot (.). They can't include reserved meta
characters such as the asterisk, question mark, backslash, and space, because these all have meaning to the shell.
Directories
Linux, like many other computer systems, organizes files in directories. You can think of directories as file
folders and their contents as the files. However, there is one crucial difference between the Linux file system
and an office filing system. In the office, file folders usually don't contain other file folders. In Linux, file
folders can contain other file folders. In fact, there is no Linux "filing cabinet"—just a huge file folder that holds
some files and other folders. These folders contain files and possibly other folders in turn, and so on.
Imagine a scenario in which you have a directory, A, that contains another directory, B. Directory B is then a
subdirectory of directory A, and directory A is the parent directory of directory B.
In Linux, the directory that holds all the other directories is called the root directory. This is the ultimate parent
directory; every other directory is some level of subdirectory. From the root directory, the whole structure of
directory upon directory springs and grows like some electronic elm. This is called a tree structure because,
from the single root directory, directories and subdirectories branch off like tree limbs.
Naming Directories
Directories are named just like files, and they can contain upper- and lowercase letters, numbers, and characters
such as -, ., and _. The slash (/) character is used to show files or directories within other directories. For
instance, usr/bin means that bin is found in the usr directory. Note that you can't tell, from this example, whether
bin is a file or a directory, although you know that usr must be a directory because it holds another item—
namely, bin. When you see usr/bin/grep, you know that both usr and bin must be directories, but again, you can't
be sure about grep. The ls program shows directories with a following /—for example, fido/. This notation
implies that you could have, for instance, fido/file; therefore, fido must be a directory. The root directory is
shown simply by the symbol / rather than mentioned by name. It's very easy to tell when / is used to separate
directories and when it's used to signify the root directory. If / has no name before it, it stands for the root
directory. For example, /usr means that the usr subdirectory is found in the root directory, and /usr/bin means
that bin is found in the usr directory and that usr is a subdirectory of the root directory. Remember, by definition
the root directory can't be a subdirectory.
Linux provides each user with his or her own directory, called the home directory. Within this home directory,
users can store their own files and create subdirectories. Users generally have complete control over what's
found in their home directories. Because there are usually no Linux system files or files belonging to other users
in your home directory, you can create, name, move, and delete files and directories as you see fit. The location
of a user's home directory is specified by Linux and can't be changed by the user. This is both to keep things tidy
and to preserve system security.
Most of the directories that hold Linux system files are "standard." Other UNIX systems will have identical
directories with similar contents. This section summarizes some of the more important directories on a Linux
system.
/
This is the root directory. It holds the actual Linux program, as well as subdirectories. Do not clutter this
directory with your files!
/home
This directory holds users' home directories. In other UNIX systems, this can be the /usr or /u directory.
/bin
This directory holds many of the basic Linux programs. bin stands for binaries, files that are executable and that
hold text only computers could understand.
/usr
This directory holds many other user-oriented directories. Some of the most important are described in the
following sections. Other directories found in /usr include
/usr/bin
This directory holds user-oriented Linux programs.
/var/spool
This directory has several subdirectories. Mail holds mail files, spool holds files to be printed, and uucp holds
files copied between Linux machines.
/dev
Linux treats everything as a file! The /dev directory holds devices. These are special files that serve as gateways
to physical computer components. For instance, if you copy to /dev/fd0, you're actually sending data to the
system's floppy disk. Your terminal is one of the /dev/tty files. Partitions on the hard drive are of the form
/dev/hd0. Even the system's memory is a device!
A famous device is /dev/null. This is sometimes called the bit bucket. All information sent to /dev/null vanishes
— it's thrown into the trash.
/usr/sbin
This directory holds system administration files. If you do an ls -l, you see that you must be the owner, root, to
run these commands.
/sbin
This directory holds system files that are usually run automatically by the Linux system.
/etc
This directory and its subdirectories hold many of the Linux configuration files. These files are usually text,
and they can be edited to change the system's configuration (if you know what you're doing!).
Creating Files
Linux has many ways to create and delete files. In fact, some of the ways are so easy to perform that you
have to be careful not to accidentally overwrite or erase files!
Return to your home directory by typing cd. Make sure you're in your /home/<user> directory by running
pwd. A file can be created by typing ls -l /bin > test. Remember, the > symbol means "redirect all output to
the following filename." Note that the file test didn't exist before you typed this command. When you
redirect to a file, Linux automatically creates the file if it doesn't already exist.
What if you want to type text into a file, rather than some command's output? The quick way is to use the
command cat.
$ cat
The cursor moves down to the next line, but nothing else seems to happen. Now cat is waiting for some
input:
Everything you type is repeated on-screen as soon as you press Enter! How do you get out of this? At the
start of a line, type ^D (Ctrl-D). (In other words, hold down the Ctrl key and press D.) If you're not at the
beginning of a line, you have to type ^D twice. ^D is the Linux "end of file" character. When a program
such as cat encounters a ^D, it assumes that it has finished with the current file, and it goes on to the next
one. In this case, if you type ^D by itself on an empty line, there is no next file to go on to, and cat exits. So
how do you use cat to create a file? Simple! You redirect the output from cat to the desired filename:
Type as much as you want. When you are finished, press ^D by itself on a line; you will be back at the
Linux prompt. Now you want to look at the contents of newfile. You could use the more or less commands,
but instead, let's use cat. Yes, you can use cat to look at files simply by providing it with a filename:
You can also add to the end of the file by using >>. Whenever you use >>, whether with cat or any other
command, the output is always appended to the specified file. (Note that the ^D character does not appear
on-screen)
To discover what cat actually stands for, let's first create another file.
cat stands for concatenate; cat takes all the specified inputs and regurgitates them in a single lump. This by
itself would not be very interesting, but combine it with the forms of input and output redirection available
in Linux and you have a powerful and useful tool.
You often need to move or copy files. The mv command moves files, and the cp command copies files. The
mv command is much more efficient than the cp command. When you use mv, the file's contents are not
moved at all; rather, Linux makes a note that the file is to be found elsewhere within the file system's
structure of directories.
When you use cp, you are actually making a second physical copy of your file and placing it on your disk.
This can be slower (although for small files, you won't notice any difference), and it causes a bit more wear
and tear on your computer. Don't make copies of files when all you really want to do is move them!
In the Linux environment renaming a file is just a special case of moving a file. To move a file to /tmp use
this:
$ mv fileone /tmp
To move the file to /tmp and change the name of the file use this:
$ mv fileone /tmp/newfilename
By using the above a file can be renamed. Simply move a file from its existing name to a new name in the same
directory:
$ mv fileone newfilename
Because the mv command can accept more than two arguments so more than one file can be moved. To move
all files in the current directory with the extension .bak, .tmp, .old to /tmp use this:
$ ls
$ mv anotherfile movedfile
$ ls
$ cp thirdfile xyz
$ ls
You can use cat (or more or less) at any time to verify that another file became moved file, and that the contents
of file xyz are identical to the contents of third file.
The cp command found at /bin/cp is used for copying and provides a powerful tool for copy operations. The
most basic uses for cp command are to copy a file from one place to another or to make a duplicate file in the
same directory. For instance to copy a file this file in the current directory to a second file to be called this file-
copy in the same directory enter the following command:
$ cp thisfile thisfile-copy
Using ls –1 to look at the directory listing of the files, you would find two files with identical sizes but
different date stamps. The new file has a date stamp indicating when the copy operation took place:it is a
new, separate file. Changes to thisfile-copy do not affect the original thisfile file.
Similarly to make a copy of thisfile in the /tmp directory use the following command:
$ cp thisfile /tmp
and if you want to copy thisfile to /tmp but give the new file a different name, enter
$ cp thisfile /tmp/newfilename
Also to avoid overwriting a file accidentally use the –i flag of the cp command which forces the system to
confirm any file it will overwrite when copying. Then a prompt like the following appears:
Here an alias has been defined so that when the cp command is issued, then actually cp –i
Is issued. In this way the user is always prompted before overwriting a file while copying. If the user logs in
as the super user or root user this alias is set by default in most Linux distributions. This is especially
important because making a small mistake as the root user can have drastic consequences for the whole
system.
$ cp fileone /tmp
$ cp filetwo /tmp
$ cp filethree /tmp
Similarly wildcards can be used to mix and copy a large number of files in one command. For instance, this
command copies all files with any one of the three extensions in one command.
When copying multiple files in this way it is important to remember that the last argument must be a
directory, since it is impossible to copy two or more files into a single file.
To copy an entire directory and all its subdirectories the –R flag of the cp command is used. This command
indicates that a directory is to be recursively copied. For example if a subdirectory called ―somedir‖ exists in
the current directory and is to be copied to /tmp then the following command can be used:
$ cp –R somedir /tmp
Creating a Directory
To create a new directory, use the mkdir command. The syntax is mkdir <name>, where <name> is replaced
by whatever you want the directory to be called. This creates a subdirectory with the specified name in your
current directory:
$ ls
anotherfile newfile thirdfile
$ mkdir newdir
$ ls
Note: The mkdir command is already familiar to you if you have used MS-DOS systems. In MS-DOS, you
can abbreviate mkdir as md. You might think that md would work in Linux, because, after all, most of the
commands we've seen have extremely concise names. However, Linux doesn't recognize md; it insists on
the full mkdir. If
you frequently switch between Linux and MS-DOS, you might want to use mkdir for both systems.
However, be warned that you might start typing other Linux commands in MS-DOS—for example, typing
ls instead of dir!
Moving Directories
To move a directory, use the mv command. The syntax is mv <directory> <destination>. In the following
example, you would move the newdir subdirectory found in your current directory to the /tmp directory:
$ mv newdir /tmp
$ cd /tmp
$ ls
/newdir
The directory newdir is now a subdirectory of /tmp.
Note: When you move a directory, all its files and subdirectories go with it.
Removing Files
To remove (or delete) a file, use the rm command found at /bin/rm. (rm is a very terse spelling of remove).
The syntax is rm <filename>. For instance:
$ rm * removes all files from your current directory. (Be careful when using wildcards!)
$ rm /tmp/*files removes all files ending in ―files‖ from the /tmp directory.
Note: As soon as a file is removed, it is gone! Always think about what you're doing before you remove a
file. You can use one of the following techniques to keep out of trouble when using wildcards.
1. Run ls using the same file specification you use with the rm command. For instance:
$ ls *files
$ rm *files
In this case, you thought you wanted to remove all files that matched *files. To verify that this indeed was
the case, you listed all the *files (wildcards work the same way with all commands). The listing looked
okay, so you went ahead and removed the files.
2. Use the i (interactive) option with rm:
$ rm -i *files
Note: When you use rm -i, the command goes through the list of files to be deleted one by one, prompting
you for the OK to remove the file. If you type y or Y, rm removes the file. If you type any other character,
rm does not remove it. The only disadvantage of using this interactive mode is that it can be very tedious
when the list of files to be removed is long.
Removing Directories
The command normally used to remove (delete) directories is rmdir. The syntax is rmdir <directory>.
Before you can remove a directory, it must be empty (the directory can't hold any files or subdirectories).
Otherwise, you see
rmdir: <directory>: Directory not empty
This is as close to a safety feature as you will see in Linux! This one might mystify you (in your home
directory)
$ ls
fido/ root/ zippy/
$ ls zippy
core kazoo stuff
$ rm zippy/*
$ ls zippy
$ rmdir zippy
rmdir: zippy: Directory not empty
The reason for the Directory not empty message is that files starting with . usually are special system files
and are usually hidden from the user. To list files whose names start with ., you have to use ls -a. To delete
these files, use rm .*:
$ ls -a zippy
Note: You should use rm -r only when you really have to. To paraphrase an old saying, "It's only a shortcut
until you make a mistake." For instance, if you're logged in as root, the following command removes all
files from your hard disk, and then it's "Hello, installation procedure" time (do not type the following
command!):
rm -rf /
Believe it or not, people do this all too often. Don't join the club!
All Linux files and directories have ownership and permissions. You can change permissions, and
sometimes ownership, to provide greater or lesser access to your files and directories. File permissions also
determine whether a file can be executed as a command.
If you type ls -l or dir, you see entries that look like this:
The -rw-r—r— represents the permissions for the file myfile. The file's ownership includes fido as the
owner and users as the group.
When you create a file, you are that file's owner. Being the file's owner gives you the privilege of changing
the file's permissions or ownership. Of course, once you change the ownership to another user, you can't
change the ownership or permissions anymore!
File owners are set up by the system during installation. Linux system files are owned by IDs such as root,
uucp, and bin. Do not change the ownership of these files.
$ ls -l myfile
$ ls -l myfile
To make any further changes to the file myfile, or to chown it back to fido, you must use su or log in as root.
File Permissions
Linux lets you specify read, write, and execute permissions for each of the following: the owner, the group,
and "others" (everyone else).
read permission enables you to look at the file. In the case of a directory, it lets you list the
directory's contents using ls.
write permission enables you to modify (or delete!) the file. In the case of a directory, you must
have write permission in order to create, move, or delete files in that directory.
execute permission enables you to execute the file by typing its name. With directories, execute
permission enables you to cd into them.
The first character of the permissions is -, which indicates that it's an ordinary file. If this were a directory,
the first character would be d.
The next nine characters are broken into three groups of three, giving permissions for owner, group, and
other. Each triplet gives read, write, and execute permissions, always in that order. Permission to read is
signified by an r in the first position, permission to write is shown by a w in the second position, and
permission to execute is shown by an x in the third position. If the particular permission is absent, its space
is filled by -.
In the case of myfile, the owner has rw-, which means read and write permissions. This file can't be
executed by typing myfile at the Linux prompt.
The group permissions are r—, which means that members of the group "users" (by default, all ordinary
users on the system) can read the file but not change it or execute it.
Likewise, the permissions for all others are r—: read-only.
File permissions are often given as a three-digit number—for instance, 751. It's important to understand how
the numbering system works, because these numbers are used to change a file's permissions. Also, error
messages that involve permissions use these numbers.
The first digit codes permissions for the owner, the second digit codes permissions for the group, and the
third digit codes permissions for other (everyone else).
The individual digits are encoded by summing up all the "allowed" permissions for that particular user as
follows:
Read permission 4
write permission 2
execute permission 1
Therefore, a file permission of 751 means that the owner has read, write, and execute permission
(4+2+1=7), the group has read and execute permission (4+1=5), and others have execute permission (1).
If you play with the numbers, you quickly see that the permission digits can range between 0 and 7, and that
for each digit in that range there's only one possible combination of read, write, and execute permissions.
Note: If you're familiar with the binary system, think of rwx as a three-digit binary number. If permission is
allowed, the corresponding digit is 1. If permission is denied, the digit is 0. So r-x would be the binary
number 101, which is 4+0+1, or 5. —x would be 001, which is 0+0+1, which is 1, and so on.
$ ls -l myfile
-rw-r—r— 1 fido users 114 Dec 7 14:31 myfile
$ chmod 345 myfile
$ ls -l myfile
—wxr—r-x 1 fido users 114 Dec 7 14:31 myfile
This method has the advantage of specifying the permissions in an absolute, rather than relative, fashion.
Also, it's easier to tell someone "Change permissions on the file to seven-five-five" than to say "Change
permissions on the file to read-write-execute, read-execute, read-execute."
You can also use letter codes to change the existing permissions. To specify which of the permissions to
change, type u (user), g (group), o (other), or a (all). This is followed by a + to add permissions or a - to
remove them. This in turn is followed by the permissions to be added or removed. For example, to add
execute permissions for the group and others, you would type:
Other ways of using the symbolic file permissions are described in the chmod man page.
Note: Any user who has write permission in a directory can delete files in that directory, whether or not that
user owns or has write privileges to those files. Most directories, therefore, have permissions set to drwxr-
xr-x. This ensures that only the directory's owner can create or delete files in that directory. It is especially
dangerous to give write permission to all users for directories!
(Refer to Appendix for detail of few more commands)
EXERCISE
1. Explain what the following commands do (with examples) and practice them:
Lockfile
cksum
comm.
csplit
chattr
touch
2. What do the following do:
cat ch1
cat ch1 ch2 ch3 > “your-practical-group”
cat note5 >> notes
6. Write two commands to take the backup of your home-folder and all sub-folders. The destination folder should be
/home/bkup. NOTE: size of backup should be smaller than original folder.
7. What is the difference between the permissions 777 and 775 of the chmod command?
NOTE: Full details of all the commands can be found in the man pages for those commands .