We discussed directory creation in Working with directories module. For the following discussion, create a directory called testdir in your home directory where you can experiment with the commands covered in this module.
In your home directory run the following commands :
mkdir testdir
chmod 700 testdir
The first command creates the directory and the second sets permissions so that only you can access its contents.
mv - move or rename a file.
rm - remove a filename from a directory list.
ln - link or create an additional name for an existing file.
To copy a file, use the command cp :
cp source-file [target-directory/]destination-file
[target-directory/] as an optional path to an alternative existing directory. If not provided, the command interpreter will assume the current directory is the target directory.
When used with a destination-file name not already in use, cp allocates an unused inode, duplicates the data stored in the source file, and creates a new directory entry using the allocated inode's ID and the new destination-file name.
If the name is already listed as a regular file in the target directory, cp will use the existing file-name, inode-id pair and overwrite the existing file's data.
cp source-file-list directory
If the destination is a directory, one or more source file-names may be specified and all will copied into the specified directory.
User must have read permissions for the source-file[s] and write and execute permissions for the target-directory.
We will do an assignment where much of this will become clearer.
When cp is run with no options, the new file will be stored with the user and group id of the person issuing the command, the current permissions of the source-file as long as they don't conflict with the umask, and with new time-stamps.
Some options available with cp :
-p preserve. Preserves ownership, permissions, even if they conflict with the umask, and original file's timestamps.
-r or -R recursive. Copy all contents of a directory into another directory. The -r is required when copying directories.
-f force. Forces overwrite if target file exists but permissions marks file as unwritable.
-n noclobber. Prevents overwrite of an existing file. Note that this will not generate an error message.
-l hard-link. Create a hard link with new file-name rather than copying original file's data. Links will be examined in detail in a later modele.
-s symbolic-link. Create a symbolic link with new fine-name rather than copying original file's data.
See the man page on cp for a list of additional options.
mv source destination
mv original-file new-file
mv original-file-list directory.
mv original-directory new-directory
If performed on the same file-system, moving a file usually does not require the OS to actually access the file itself. Rather, the command modifies the source and target directories that list/will list the file of interest. Because of this, moving or renaming a directory works the same as moving or renaming a regular file.
User must have write and execute permissions for both the source directory and the target directory that lists/will list the file of interest. As long as exact filenames are specified, read access is not required for either source or destination directory.
If the target is an existing directory, then the original filename will be stored in the target directory whether the original file is a directory or regular file.
If the target is a regular file and the original filename represents a directory, the command will always fail.
Available options for mv :
-i interactive. Prompt user for permissions of source-file will overwrite existing target file. This may be default on Linux systems.
-f force. Perform action even without confirmation even if target already exists. If directory permissions limit the action of the mv command, -f will NOT override them.
-n no-clobber. Do not overwrite existing file.
The exception to this is if the destination of the move is on a different partition or drive of the system. Then the source files must be read and duplicated on the target system. However, no additional option is required for this.
rm file-list
rm -r directory
A file's existence and access is controlled in Unix by a structure called an inode table. Each file created is assigned to an unique inode within the inode table. The inode stores the id of file's owner, the size, permissions, where the files contents are stored on the storage device, and additional information. There is one inode per file and one file per inode. We will look at the inode in more detail later.
When a file is created, it is also linked to a name assigned by the user. This name is stored in a user chosen directory along with the id or index number of the inode associated with the file. Once the file is created, it is possible to assign several names to the file by linking a name to the associated inode number.
The inode of the file has a field called the link count. This indicates how many names have been associated or linked to the file of interest.
rmremoves the filename entry from the designated directory. The link count in the inode of the file whose name was removed is decremented by one. When the link count reaches zero, and only when it does, the operating system will mark both the inode and any storage space previously used by the file as available for other use.
Other options :
-i - interactive. Prompts user to confirm each remove. This is especially useful when using with filename meta-character to ensure you do not accidentally delete something.
-f - force.
-r - recursive. For use on directories. rmdir works only on empty directories. rm -r will recursively delete all files and sub-directories in the target directory. Permissions for the target directory and any sub-directories must have appropriate permissions, e.g read, write, and execute.
ln original_filename new_filename
ln -s original_filename new_filename
Linking provides a two methods for creating additional file names for an existing file.
The first type of line is called a hard link. Creating a hard link causes a new directory entry to be created where the new filename directly references the inode of an existing file. The link field in the inode of the file is increment by one. When either filename is referenced, the operating system 1st retrieves the inode index from the appropriate directory entry, then uses the inode information to access the file.
However, there are several restrictions on creating hard links. The file being linked to must be a regular file. The directory that will list the new name must be on the same partition (file system) as the original. The directory listing the original filename must be executable and the directory of the new target filename must be writable and executable.
Hard linking provides some advantages. Once the link exists, only the properties and permissions of the file itself control its access. Since the link is based on a direct reference to the file's inode, access via the linked name works even if original filename is moved or deleted. File count and file size quotas are not affected by hard linking.
Experiment : #( Log in and go to testdir. If you have not created testdir yet, review above. ) cd testdir #( Create a file using the touch command. Select a name not already in use. touch will create a file of zero bytes length.) touch myfile #( Run the ls with long listing and inode options on the file. ) ls -il myfile #( where myfile is the name of the file you created. Among other things, this will display the file's size and inode number. ) #( Next run the command : ) ln myfile newfile #( Re-run the ls command and options against the files. ) ls -il myfile newfile #( Note that the size and inode number are the same for both files. Review the man page on ls if you are not familiar with the fields displayed. ) #(For now stay logged in and in testdir. We will play with symbolic links next.) |
The advantage of a symbolic link is that it can be linked to any kind of file because all it does is store the stated path to actual file of interest.
And because the path itself is stored in the link file, you have a record of the target's location.
Because a symbolic link is a static recording of the location of the original file, it is possible to break the link by deleting or changing the location of the original file. It is also possible to block access by changing the permissions on the directories in the path to the original file.
The ability to break the link may be viewed as an advantage or a disadvantage. For example, it is common to use a symbolic link as the interface to the video driver directories. This allows the administrator to install new drivers in a different directory, modify the link to point to the new directory, test the drivers, and reset the link to the existing drivers if the test fails.
And because a symbolic link is a separate file with its own inode, each symbolic file counts against a user's file number and file size quota.
If you are linking to another user's files, you should always use symbolic links.
Experiment : #( Create a symbolic link )
ln -s myfile srfile #( The $PWD/myfile will provide an absolute path qualified location for myfile. ). echo $PWD/myfile #( Count the number of characters displayed by the previous command. Now run the ls command and options. ) ls -il myfile srfile safile
|
The $PWD is expanded before the ln command executes, so the full path and name if the original file is passed to ln. The same occurs for the echo so you can see what was passed to ln. Notice that the size and inode number are the different from the original file and different from each other. The size of the symbolic files should match the path and name of the original file given for each link command.
There is not much more to linking except learning when to best use it.