• Aucun résultat trouvé

Manipulating Files and Directories

Dans le document URLs Referenced in This Book (Page 99-102)

Part II: The Bioinformatics Workstation

Chapter 4. Files and Directories in Unix

4.2 Commands for Working with Directories and Files

4.2.3 Manipulating Files and Directories

Of course, just as with the stacks of papers on your desk, you periodically need to do some housekeeping on your files and directories to keep everything neat and tidy. Unix provides commands for moving, copying, and deleting files, as well as creating and removing directories.

4.2.3.1 Copying files and directories with cp

Usage: cp -[options] source destination

The cp command makes a copy of a source file at a destination. If the destination is a directory, the source can be multiple files, copies of which are placed in the destination directory. Frequently used options are -R and -r. Both copy recursively; that is, they copy the source directory and all its subdirectories to the destination. The -R option prevents cp from following symbolic links; only the link itself is copied. The -r option allows cp to follow symbolic links and copy all files it finds. This can cause problems if the symbolic links happen to form a circular path through the filesystem.

Normally, new files created by cp get their file ownership and permissions from your shell settings. However, the POSIX version of cp provides an -a option that attempts to maintain the original file attributes.

4.2.3.2 Moving and renaming files and directories with mv

Usage: mv source destination

The mv command simply moves or renames source to destination. Files and directories can both be either source or destination. If both source and destination are files or both are directories, the result of mv is essentially that the file or directory is renamed. If the destination is a directory, and the intention is to move already existing files or

directories under that directory in the hierarchy, the directory must exist before the mv command is given. Otherwise the destination is created as a regular file, or the operation is treated as a renaming of a directory. One problem that can occur if mv isn't used carefully is when source represents a file list, and destination is a preexisting single file.

When this happens, each member of source is renamed to destination and then promptly overwritten, leaving only the last file of the list intact. At this point, it's time to look for your system administrator and hope there is a recent backup.

4.2.3.3 Creating new links to files and directories with ln

Usage: ln -[options] source destination

The ln command establishes a link between files or directories at different locations in the directory tree. While creating a link creates the appearance of a new file in the destination location, no data is actually copied. Instead, what's created is a new pointer in the filesystem index that allows the source file to be found at more than one location "on the map."

The most commonly used option, -s, creates a symbolic link (or symlink) to a file or directory, as in the following example:

% ln -s perl5.005_03 perl

This allows you to type in just the word perl rather than remembering the entire version nomenclature for the current version of Perl.

Another common use of the ln command is to create a link to a newly compiled binary executable file in a directory in the system path, e.g., /usr/local/bin. Doing this allows you to run the program without addressing it by its full pathname.

4.2.3.4 Creating and removing directories with mkdir and rmdir

Usage: mkdir -[options] dirname Usage: rmdir -[options] dirname

Safari | Developing Bioinformatics Computer Skills -> 4.2 Commands for Working with Directories and Files

http://safari.oreilly.com/main.asp?bookname=bioskills&snode=46 (5 of 7) [6/2/2002 8:54:50 AM]

New directories can be created with the mkdir command, which has only two command-line options.

mkdir -p creates a directory and any intermediate components of the path that are missing. For instance, if user jambeck decides to create a directory mustelidae/weasels in his home directory, but the intermediate directory mustelidae doesn't exist, mkdir -p creates the intermediate directory and its subdirectory weasels.

mkdir -m mode creates a directory with the specified file-permission mode.

rmdir removes a directory if it's empty. With the -p option, rmdir removes all the empty directories in a given path.

If user jambeck decides to remove the directory mustelidae/weasels, and directory mustelidae is empty except for directory weasels, rmdir -p ~/mustelidae/weasels removes both weasels and its parent directory mustelidae.

4.2.3.5 Removing files with rm

Usage: rm -[options] files

The rm command removes files and directories. Here are its common options:

-f

Forces the removal of files without prompting. You still can't remove files you don't own, but the write permissions on files you do own are ignored. For example, rm -f a* deletes all files starting with the letter a, but doesn't delete any subdirectories.

-i

Prompts you with rm: remove filename? Files are removed only if you begin your answer with a y or Y.

-r

(recursive option) Removes all directories and subdirectories in the list of files. Symbolic links aren't traversed; only the symlink itself is removed.

-v

(verbose option) Echoes the names of all files/directories that are removed.

While rm is a fairly simple command, there are a few instances in which it can cause serious problems for the careless user.

The command rm * removes all files in a directory. Unless you have the files set as read-only or have the interactive flag set, you will delete everything in the directory. Of course this isn't as bad as using the command rm -r * or rm -rf *, the last of which overrides any read-only file modes, traverses down through your directories and deletes everything in your current directory or below.

Occasionally you will find that you create odd files in your directories. For instance, you might have a file named -myfile where the - is part of the filename. Try deleting it, and you will get an error message concerning the fact that rm doesn't have a -m option. Your shell program interprets the -m as a command flag, not part of the filename. The solution to this problem is trivial but not always instantly apparent: simply provide a more complete path to the file, such as rm ./-myfile or rm /home/jambeck/-myfile. Similar solutions are needed if you accidently create a file with a space in the name.

Delivered for Maurice ling Swap Option Available: 7/15/2002

Last updated on 10/30/2001 Developing Bioinformatics Computer Skills, © 2002 O'Reilly

< BACK Make Note | Bookmark CONTINUE >

Safari | Developing Bioinformatics Computer Skills -> 4.2 Commands for Working with Directories and Files

http://safari.oreilly.com/main.asp?bookname=bioskills&snode=46 (6 of 7) [6/2/2002 8:54:50 AM]

Index terms contained in this section

cd command (Unix) command prompts cp command (Unix) directories, in Unix

commands for working with finding

manipulating files, in Unix

commands for working with finding

manipulating find command (Unix) home directories Linux

files and directories, working with ln command (Unix)

ls command (Unix) interpreting output of mkdir command (Unix) mv command (Unix)

print working directory (pwd) command prompts (command)

pwd (print working directory) command rm command (Unix)

rmdir command (Unix) symlink (symbolic links) whereis command (Unix) which command (Unix)

© 2002, O'Reilly & Associates, Inc.

Safari | Developing Bioinformatics Computer Skills -> 4.2 Commands for Working with Directories and Files

http://safari.oreilly.com/main.asp?bookname=bioskills&snode=46 (7 of 7) [6/2/2002 8:54:50 AM]

Show TOC | Frames My Desktop | Account | Log Out | Subscription | Help

Programming > Developing Bioinformatics Computer Skills > 4. Files and Directories in Unix > 4.3 Working in a Multiuser Environment

See All Titles

< BACK Make Note | Bookmark CONTINUE >

158127045003020048038218232180015152050067001135112006120215207095115008093054028077150

Dans le document URLs Referenced in This Book (Page 99-102)