Sunday, January 3, 2010

Lesson 06 Working with the Filesystem Part02

Changing Directories
The shell prompt displays the last component of the directory name, for example, it displays student01 to represent /home/student01. To move from directory to directory on the system, use the cd command.

The cd command accepts one of two arguments:

An absolute or relative path name
A shortcut representing the directory to which you wish to change
A number of shortcuts are at your disposal for use with the cd command. A dot (.) and a double dot (..) are two shortcuts found in every directory. The . shortcut represents the current directory, whereas the .. shortcut represents the parent directory.

For example, if your current working directory is /home/student01, what would typing the following do?
cd ..
It would move you to the /home directory.

The tilde (~) is an abbreviation for “home directory.” Used by itself, it represents your own home directory. Used as a prefix to another user’s login ID, it represents that user’s home directory.
A dash (-) represents your previous working directory. It’s a handy shortcut to use to switch back and forth between two directories.
For example:









Listing Directory Contents

To list directory contents, use the ls command.

This is the syntax for the ls command:

ls [options] [files_or_directories]

Examples

The ls command used without any arguments will list the file and directory names in the current directory.
ls -a includes so-called hidden files and directories whose names begin with a dot (.).




ls lists another file or directory if given as an argument:




Use ls -l for a more detailed “long” listing:





ls -R recursively lists through subdirectories, listing their contents too.

The ls command has many other options. For a full listing, view the man page for the ls command. All options can be used in combination with other ls options.


Copying Files and Directories

To copy files and directories, use the cp command.

The cp command syntax is as follows:

cp [options] file destination

More than one file may be copied at a time if the destination is a directory:

cp [options] file1 file2 destination

The cp command must always be given at least two arguments. When two arguments are given, the first argument is interpreted as the source file and the second argument as the destination. These arguments can be specified as either an absolute or relative path name. If an existing directory is named, a copy of the source file is placed in that directory with the same name as the source. Otherwise, the destination is interpreted as a file name, and a copy of the source file is created with that destination name.

When more than two arguments are provided, all arguments except the last are interpreted as source files. The last argument is interpreted as a destination directory. Copies of the source files are placed, with their original names, in the destination directory.

These are some of the common options of the cp command:

-i Interactive – Asks before overwriting a file
-r Recursive – Recursively copies an entire directory tree
-p Preserve – Preserves permissions, ownership, and time stamps
-a Archive – Copies files and directories recursively (like -r) while preserving permissions (like -p)

Copying Files and Directories: The Destination

When you use the cp command to copy a single file to a destination, cp first checks whether the destination directory exists. If it does exist, cp places a copy of the source file, with the same name as the source file, into the destination directory. If the destination directory does not exist, the source file is assumed to be a new name and cp makes a copy of the source file with the destination name.

As an example, assume the current directory is ~/test and you want to make a copy of file01.txt in a subdirectory of your home directory, named backups.



Moving and Renaming Files and Directories

To move files, use the mv command.

The syntax for the mv command is as follows:

mv [options] file destination

More than one file can be moved at a time, but the destination must be a directory:

mv [options] file1 file2 destination

The mv command must always be given at least two arguments. Aside from a couple of switches, mv and cp function identically. The only difference is that cp results in matching identical files; with mv, the source disappears, leaving only the destination files.

Using Two Arguments

The first argument is taken as the source file. It may or may not be prepended with a relative or absolute path name. The second argument is interpreted as the destination. It may or may not be prepended with a relative or absolute path name.

If it names an existing directory, the source file is moved to that directory with the same name as the source. Otherwise, the destination is interpreted as a file name, and the source file is renamed, moved, or renamed and moved to that destination name.

Using More than Two Arguments

All arguments but the last are interpreted as source files. These may or may not be prepended with a relative or absolute path name.

The last argument is interpreted as a destination directory. It may or may not be prepended with a relative or absolute path name. The source files are moved, with their original file names, to the destination directory.

For example:




Creating and Removing Files


The touch command can be used to create or modify existing files. This command updates a file’s time stamps. For example, if the last time you accessed a file was at 10:02 p.m. and you touch the file at 10:45 p.m., the file will show its last access at 10:45 p.m. If you touch a file that does not exist, an empty file will be created.

To remove files, use the rm command.

This is the syntax for the rm command:

rm [options]

One or more files can be removed at a time. By default, rm will not remove directories. The -r option tells rm to remove files recursively, so it will delete directories and their contents.

To remove a directory and all files and subdirectories it contains, and suppressing and ignoring warnings about removing write-protected files and directories in the process, use rm -rf.

Note: Be very careful not to erase needed files with the -rf option! If necessary, the rm command can be made interactive by using the -i option.

Remember—you cannot undo the effects of the rm command. The only way you can restore files deleted this way is to restore from a backup.

Using rm: Examples

To remove a directory and all its contents, use this command:

rm -r directory

Use -r in combination with -i to recurse through the named directory and to query whether or not each file should be removed. The file will be removed if the user types y or Y:

rm -ri directory
rm: descend into directory ‘include’? Y
rm: descend into directory ‘include/files’ ? Y
rm: remove regular file ‘directory/files/file1.txt’ ? Y
…output truncated…

Use the -f option to suppress warnings about write-protected files. If the example above had used rm -rf instead of rm -ri, the directory tree would have been deleted with no further prompting.

Note

Be very careful using the rm -rf command. If run from the wrong directory, it can do extensive damage to the system, without prompting you for confirmation.

Creating and Removing Directories

To create directories, use the mkdir command. For example, to create a directory called work, use the following:

mkdir work


To remove an empty directory, use rmdir command. For example, to remove the work directory, use this command:

rmdir work

0 comments:

 
Home | About | Link | Link
Simple Proff Blogger Template Created By Herro | Inspiring By Busy Bee Woo Themes