The first commands a novice learns
The basic file "list" command. It is all too easy
to underestimate the power of this humble command. For
example, using the
![]() | The ls command returns a non-zero exit status when attempting to list a non-existent file.
|
Example 15-1. Using ls to create a table of contents for burning a CDR disk
#!/bin/bash
# ex40.sh (burn-cd.sh)
# Script to automate burning a CDR.
SPEED=2 # May use higher speed if your hardware supports it.
IMAGEFILE=cdimage.iso
CONTENTSFILE=contents
DEVICE=cdrom
# DEVICE="0,0" For older versions of cdrecord
DEFAULTDIR=/opt # This is the directory containing the data to be burned.
# Make sure it exists.
# Exercise: Add a test for this.
# Uses Joerg Schilling's "cdrecord" package:
# http://www.fokus.fhg.de/usr/schilling/cdrecord.html
# If this script invoked as an ordinary user, may need to suid cdrecord
#+ chmod u+s /usr/bin/cdrecord, as root.
# Of course, this creates a security hole, though a relatively minor one.
if [ -z "$1" ]
then
IMAGE_DIRECTORY=$DEFAULTDIR
# Default directory, if not specified on command line.
else
IMAGE_DIRECTORY=$1
fi
# Create a "table of contents" file.
ls -lRF $IMAGE_DIRECTORY > $IMAGE_DIRECTORY/$CONTENTSFILE
# The "l" option gives a "long" file listing.
# The "R" option makes the listing recursive.
# The "F" option marks the file types (directories get a trailing /).
echo "Creating table of contents."
# Create an image file preparatory to burning it onto the CDR.
mkisofs -r -o $IMAGEFILE $IMAGE_DIRECTORY
echo "Creating ISO9660 file system image ($IMAGEFILE)."
# Burn the CDR.
echo "Burning the disk."
echo "Please be patient, this will take a while."
cdrecord -v -isosize speed=$SPEED dev=$DEVICE $IMAGEFILE
exit $? |
cat, an acronym for
concatenate,
lists a file to
# Uses of 'cat' cat filename # Lists the file. cat file.1 file.2 file.3 > file.123 # Combines three files into one. |
See also Example 15-28 and Example 15-24.
![]() |
In a pipe, it may be
more efficient to redirect
the
|
tac, is the inverse of cat, listing a file backwards from its end.
reverses each line of a file, and outputs to
|
This is the file copy command.
![]() | Particularly useful are the
|
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-20 and Example A-2.
![]() | When used in a non-interactive script,
mv takes the When a directory is moved to a preexisting directory, it becomes a subdirectory of the destination directory.
|
Delete (remove) a file or files. The
![]() | The rm command will, by itself, fail to remove filenames beginning with a dash. Why? Because rm sees a dash-prefixed filename as an option.
One clever workaround is to precede the filename with a " -- " (the end-of-options flag).
Another method to is to preface the filename to be removed
with a
|
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,
Changes the attributes of an existing file or directory (see Example 14-13).
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 444 filename # Makes "filename" read-only for all. # Modifying the file (for example, with a text editor) #+ not allowed for a user who does not own the file (except for root), #+ and even the file owner must force a file-save #+ if she modifies the file. # Same restrictions apply for deleting the file. |
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. chmod 111 directory-name # Gives everyone execute-only permission in a directory. # This means that you can execute and READ the files in that directory #+ (execute permission necessarily includes read permission #+ because you can't execute a file without being able to read it). # But you can't list the files or search for them with the "find" command. # These restrictions do not apply to root. chmod 000 directory-name # No permissions at all for that directory. # Can't read, write, or execute files in it. # Can't even list files in it or "cd" to it. # But, you can rename (mv) the directory #+ or delete it (rmdir) if it is empty. # You can even symlink to files in the directory, #+ but you can't read, write, or execute the symlinks. # These restrictions do not apply to root. |
Change file attributes. This is analogous to chmod above, but with different options and a different invocation syntax, and it works only on ext2/ext3 filesystems.
One particularly interesting chattr
option is
|
If a file has the
If a file has the
If a file has the
![]() | 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
The syntax of the command is a bit tricky. For example:
![]() | If a file named |
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 15-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 more detailed descriptions than do the man pages.
There have been various attempts at "automating" the writing of man pages. For a script that makes a tentative first step in that direction, see Example A-41.
| [1] | Dotfiles are files whose
names begin with a dot, such as
|