Many, if not most of the UNIX files you will encounter will be generated by an application of some kind. To edit these files, you will need the application. You can still copy them, move them about and delete them. Many of the following commands work better on ASCII text files. A surprising number of UNIX applications like Xfig, ApplixWare and NExS store their files in ASCII format.
Indicate the file type of foo. The capability of this
command varies from UNIX to UNIX. Under Linux, it is effective,
identifying text, executables, application files and graphic
formats.
Aside from text editors, there are three UNIX utilities for reading
text files --- cat, more and less. The
command cat dumps the file text straight to your terminal.
If you are in an xterm with a scrollbar, you can scroll
back and read all of it. The command more reads the text
screen by screen, waiting for you to hit Enter at the end of each
one.
Print foo.txt to the terminal,
Print foo.txt to the terminal, pausing at the end of
each screen.
The command less is not always installed on UNIX, but as
far as I know, all Linux distributions include it.
Use less to read the file foo.txt.
less scrolls up and down, using the cursor keys
as well as ``u'' and ``d''. To do keyword searches, hit ``/'',
followed by the string of characters you want to search for. Hit
Enter. To exit, hit ``q''.
There are an awful lot of text editors available to UNIX. Some of these are applications in themselves, with full GUI support, syntax highlighting. At least one text editor has a built-in web browser, a minesweeper game, and an online psychoanalyst that you can feed the built-in Zippy the Pinhead quotes to. You can even edit text with it!
All of the following editors run from the command line, although some of them have GUI versions. With one horrible exception, all of the following editors provide on-screen help, so you don't have to know how it works, to run it. They are listed in descending order of user friendliness.
This is the text editor used by the mail reader Pine. Pine
used to be installed on most Linux system. It is not so
common now. Pico was actually written to write email messages,
so it wraps text for you. This is unacceptable for most command
text, so you must turn the feature off using the -w
switch. Pico displays a command menu at the bottom of the
screen. You can also type pico -? to get a list of
command line options.
Emulates a bunch of other editors including Emacs and Wordstar. It provides a command menu at the top of the screen.
Joes Own Editor. This is installed with most Linux systems, and it tells you how to get help at the top of the screen.
Emacs is the standard UNIX text editor, originally written by Richard Stallman. On newer systems, including Linux, EMACS comes up as a GUI editor, with menus accessible using the mouse. The command line version is a bit awkward to figure out. Most of the help for Emacs is in the info pages, and you should read these carefully before launching it. To get help, hit [CTRL]h. To exit Emacs, hit [CTRL]x, then [CTRL]c. There are several editors based on Emacs, including Jove (Jonathan's Own Version of Emacs), and Xemacs, the editor with the features listed above and which has an excellent GUI interface. New users will probably want to find something a little more friendly for the command line.
This is the horrible exception. Vi is an excellent editor once
you have learned it, especially if you touch type.
Unfortunately, It has no intuitive qualities. If you
have somehow gotten into vi, hit the Escape key a couple of
time, then type q! ("que bang"). Most of this HOWTO was
typed up with vi.
If you are running Linux, your vi is a symbolic link either to
vim or to elvis. Probably, it's vim. Try typing vi
or vim by itself, without the filename. You should get
a useful information message. From vim, you can type
:help to get into their help system.
Copy the file foo to foobar. If
foobar is a directory, foo will copied into
it, and named foo.
Same as above, except that if you already have a file called
foo, or foobar/foo, you will be prompted
before it is overwritten.
Copy a whole bunch of files whose names start with foo,
into the directory foobar. If foobar is not a
directory, or it doesn't exist, the command will return an error.
Move the file foo to foobar. If
foobar is a directory, foo will moved into it,
and named foo.
Same as above, except that if you already have a file called
foo, or foobar/foo, you will be prompted
before it is overwritten.
Delete the file foo.
Delete all files whose names start with foo. The
-i switch causes rm to prompt you before
deleting each file.
Permissions determine who can access your files. There are three
entites you can provide access to, the owner (you), members of your
group (see /etc/group), and everyone.
If you list a directory, the first column (on Linux anyway) will be
the file permissions, shown as something like -rw-r-----.
This particular code shows that the owner can read from, write to,
or delete the file (rw-). Members of the owners's group
can read the file, but cannot write to or delete it, and everybody
else is refused access.
If you want to change the permissions, you must use the command
chmod, as follows...
Command Permissions from command ls
------- ---------------------------
chmod 600 foo -rw-------
chmod 640 foo -rw-r-----
chmod 660 foo -rw-rw----
chmod 644 foo -rw-r--r--
chmod 666 foo -rw-rw-rw-
chmod 755 foo -rxwr-xr-x
The execute permissions are only required if the file is an
executable of some kind. You need not worry about this unless you
are writing shell scripts.
The tool for file searching in UNIX is find. This allows
you to search filesystems for files or directories conforming to a
list of specified characteristics. The structure of the find
command is as follows...
find foobar -mtime -10 -size +1000 -ls
This command searches the directory foobar for files
modifed sometime in the last ten days, and occupying more than 1000
disk blocks. When it finds such a file, it lists it to the screen.
There are a lot of switches for find, and you can use any
number of them...
Modify time, in days. -10 means less than ten days old. +10 means more than ten days old. 10 means exactly ten days old.
Size, in disk blocks. +500 means more than 500 blocks. -500 means less than 500 blocks.
Do wildcard searches for filenames. Unfortuntately, we can't just use the asterisk character. It must be ``escaped'' with a backslash, eg.
foo\*bar
.
Select files by the type of entity they are. The switch
-type f will select only files. The switch
-type d will select only directories.
Print the pathname of any file conforming to the list.
Detailed listing of any file conforming to the list.
The command grep and its relatives fgrep and
egrep are pattern searchers. You feed them a pattern and
some text, and they spit out the lines that match the pattern.
Here are some examples...
Search text files in the current directory for the pattern ``Howard''. Print out any lines that contain the pattern. Note that I have supplied an extension, to limit the search a little. Otherwise, grep would try to read any subdirectories it found, and this would cause an error.
Do the same search as above, except that now the pattern matching is case insensitive. The characters ``howard'', ``HOWARD'', ``hOwArD'' would all match, and the text lines containing them would be printed.
Do the same case insensitive search as above, except now, only print the filenames. Don't print the actual text.
You can feed data to grep using a pipe. In this case, we are doing a detailed directory listing, and using grep to filter the results. Only directory lines containing the characters ``Mar'' somewhere, will be printed to the terminal. Effectively, this lists all files saved or updated in March. Unfortunately, it will also print any files with ``Mar'' in the filename, username or group. Oh well.
Here is how to search through a file system for files containing a text string...
find . -type f -exec fgrep -li howard {} \;
The above line searches through the directory tree for files. Each
file is processed by executing the command fgrep, with the switches
-li. The string ``howard'' is searched for. Each file
containing the string somewhere, will be listed to the terminal.
The -exec switch allows you to use standard UNIX commands
with your find search. The -exec sequence is terminated
with a semicolon. A number of characters must be ``escaped'' in
the command line with a backslash. These include the semicolon, as
shown above, the asterisk (*), useful for filename substitution, and
the brackets.
You can easily generate and use a listing of everything you have stored on CD-ROM and DVD. This will also work on floppies for those among you who still have floppy drives.
We need to do two things here. First, we must create a listing of the files on the disk. Then, we need a procedure for searching through this listing.
The first thing to do is create a directory where your CD-ROM
listings will be stored. On my system this is
/home/howard/disks/cdroms. Create this directory, then
move to it.
$ mkdir /home/howard/disks/cdroms
$ cd /home/howard/disks/cdroms
To catalogue an individual CD-ROM, you must mount it. On current Linux distributions, this happens automatically when you insert the disk into your machine.
If your disk does not mount, you will have to read up on your
mount command and your /etc/fstab file. These
are outside the scope of this HOWTO.
Once your disk is mounted, there should be a file path leading
to it. On my Fedora system, the disk is mounted under
/media. I want to list files, and I want to go down into
all the subdirectories and list their contents too. Let's assume
my disk is called ``Downloads201112".
$ ls -lR /media/Downloads201112 > Downloads201112
The results of the listing are being redirected from the
standard output (my terminal) to a file called
Downloads201112.
When I have done this for each of my CD-ROMs, I have a directory with a complete file listing for each of my CD-ROMs. Each file on the CD-ROMs is listed as a complete pathname including the mount point of my CD-ROM.
Note how the directory /home/howard/disks can be broken
into subdirectories. I would probably want to store my photos
in a directory deparate from cdroms.
Now, weeks, months or years later, I want to search for something
that reads the Kodak Photo CD-ROMs. The files on my photo CD have
the extension .pcd, so I figure that a program that
reads them will include this string. I am sitting in my home
directory.
$ grep -ri pcd disks
The system will respond by searching the directories under
disks, and printing out every line of each of my
CD-ROM listings that contain the sequence pcd in any
combination of upper and lower case (the -i switch). These
will be printed with the filename, so I will see what CD-ROM these
were found on.
Now all I have to do is mount the CD-ROM and take a look at the file. Since the complete pathname is being displayed, I can easily copy and paste directly from the output.