|
||
12.1. Basic CommandsThe first commands a novice learns
|
tac, is the inverse of cat, listing a file backwards from its end.
reverses each line of a file, and outputs to stdout. This does not have the same effect as tac, as it preserves the order of the lines, but flips each one around.
bash$ cat file1.txt This is line 1. This is line 2. bash$ tac file1.txt This is line 2. This is line 1. bash$ rev file1.txt .1 enil si sihT .2 enil si sihT
This is the file copy command. cp file1 file2 copies file1 to file2, overwriting file2 if it already exists (see Example 12-6).
Particularly useful are the -a archive flag (for copying an entire directory tree) and the -r and -R recursive flags.
This is the file move command. It is equivalent to a combination of cp and rm. It may be used to move multiple files to a directory, or even to rename a directory. For some examples of using mv in a script, see Example 9-18 and Example A-2.
When used in a non-interactive script, mv takes the -f (force) option to bypass user input.
When a directory is moved to a preexisting directory, it becomes a subdirectory of the destination directory.
bash$ mv source_directory target_directory bash$ ls -lF target_directory total 1 drwxrwxr-x 2 bozo bozo 1024 May 28 19:20 source_directory/
Delete (remove) a file or files. The -f option forces removal of even readonly files, and is useful for bypassing user input in a script.
The rm command will, by itself, fail to remove filenames beginning with a dash.
bash$ rm -badname rm: invalid option -- b Try `rm --help' for more information.
One way to accomplish this is to preface the filename to be removed with a dot-slash .
bash$ rm ./-badname
bash$ rm -- -badname
When used with the recursive flag -r, this command removes files all the way down the directory tree from the current directory. A careless rm -rf * can wipe out a big chunk of a directory structure.
Remove directory. The directory must be empty of all files -- including "invisible" dotfiles [1] -- for this command to succeed.
Make directory, creates a new directory. For example, mkdir -p project/programs/December creates the named directory. The -p option automatically creates any necessary parent directories.
Changes the attributes of an existing file (see Example 11-12).
chmod +x filename # Makes "filename" executable for all users. chmod u+s filename # Sets "suid" bit on "filename" permissions. # An ordinary user may execute "filename" with same privileges as the file's owner. # (This does not apply to shell scripts.)
chmod 644 filename # Makes "filename" readable/writable to owner, readable to # others # (octal mode).
chmod 1777 directory-name # Gives everyone read, write, and execute permission in directory, # however also sets the "sticky bit". # This means that only the owner of the directory, # owner of the file, and, of course, root # can delete any particular file in that directory.
Change file attributes. This is analogous to chmod above, but with different options and a different invocation syntax, and it works only on an ext2 filesystem.
One particularly interesting chattr option is i. A chattr +i filename marks the file as immutable. The file cannot be modified, linked to, or deleted , not even by root. This file attribute can be set or removed only by root. In a similar fashion, the a option marks the file as append only.
root# chattr +i file1.txt root# rm file1.txt rm: remove write-protected regular file `file1.txt'? y rm: cannot remove `file1.txt': Operation not permitted
If a file has the s (secure) attribute set, then when it is deleted its block is zeroed out on the disk.
If a file has the u (undelete) attribute set, then when it is deleted, its contents can still be retrieved (undeleted).
If a file has the c (compress) attribute set, then it will automatically be compressed on writes to disk, and uncompressed on reads.
The file attributes set with chattr do not show in a file listing (ls -l).
Creates links to pre-existings files. A "link" is a reference to a file, an alternate name for it. The ln command permits referencing the linked file by more than one name and is a superior alternative to aliasing (see Example 4-6).
The ln creates only a reference, a pointer to the file only a few bytes in size.
The ln command is most often used with the -s, symbolic or "soft" link flag. Advantages of using the -s flag are that it permits linking across file systems or to directories.
The syntax of the command is a bit tricky. For example: ln -s oldfile newfile links the previously existing oldfile to the newly created link, newfile.
If a file named newfile has previously existed, an error message will result.
Which type of link to use?
As John Macdonald explains it:
Both of these [types of links] provide a certain measure of dual reference -- if you edit the contents of the file using any name, your changes will affect both the original name and either a hard or soft new name. The differences between them occurs when you work at a higher level. The advantage of a hard link is that the new name is totally independent of the old name -- if you remove or rename the old name, that does not affect the hard link, which continues to point to the data while it would leave a soft link hanging pointing to the old name which is no longer there. The advantage of a soft link is that it can refer to a different file system (since it is just a reference to a file name, not to actual data). And, unlike a hard link, a symbolic link can refer to a directory.
Links give the ability to invoke a script (or any other type of executable) with multiple names, and having that script behave according to how it was invoked.
Example 12-2. Hello or Good-bye
#!/bin/bash # hello.sh: Saying "hello" or "goodbye" #+ depending on how script is invoked. # Make a link in current working directory ($PWD) to this script: # ln -s hello.sh goodbye # Now, try invoking this script both ways: # ./hello.sh # ./goodbye HELLO_CALL=65 GOODBYE_CALL=66 if [ 0ドル = "./goodbye" ] then echo "Good-bye!" # Some other goodbye-type commands, as appropriate. exit $GOODBYE_CALL fi echo "Hello!" # Some other hello-type commands, as appropriate. exit $HELLO_CALL
These commands access the manual and information pages on system commands and installed utilities. When available, the info pages usually contain a more detailed description than do the man pages.
Dotfiles are files whose names begin with a dot, such as ~/.Xdefaults. Such filenames do not appear in a normal ls listing (although an ls -a will show them), and they cannot be deleted by an accidental rm -rf *. Dotfiles are generally used as setup and configuration files in a user's home directory.