You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
12 KiB

<html>
<head>
<title>Basic File and Shell Operations</title>
<style type="text/css">
#main {
width: 800px;
line-heigth: 1.1em;
}
</style>
</head>
<body>
<div id="main">
<h1>Basic File and Shell Operations</h1>
<p>In this tutorial, we'll briefly explain some basic file and shell
operations using the following commands:</p>
<h2>Commands</h2>
<ol>
<li>touch -- create a file</li>
<li>pwd -- print working directory</li>
<li>ls -- list files in current directory</li>
<li>cp -- copy a file</li>
<li>mv -- move a file</li>
<li>rm -- remove a file</li>
<li>file -- examine type of file</li>
<li>less -- read a file</li>
<li>mkdir -- create a directory</li>
<li>cd -- change directory</li>
<li>rmdir -- remove a directory</li>
<li>clear -- clear screen</li>
</ol>
<p>Please note that the above commands each have a man-page that will
describe in more detail the full possibilities of each command. In this
tutorial, we'll simply give a short overview, enough to get things done
on the command line. For more information on man-pages, see the <a
href="http://sdf.lonestar.org/index.cgi?tutorials/findinghelp"><i>Finding
Help From Within the Shell</i></a> tutorial.</p>
<h3>First some notes about:</h3>
<ul type="disk">
<li>Command options</li>
<li>File naming</li>
</ul>
<h3>1. Command options</h3>
<p>All of the commands above and many commands you'll discover and use
in the shell can be modified with the use of <i>options</i>. See the
man-pages for details about the available options for each command.
<i>Options</i> normally come in the form of <i>ls -l</i>, that is, you
type the command, space over once, type a dash and the letter for the
option you need to use.</p>
Something unique for the SDF server, the SDF server now offers both
commands with BSD style options and commands with GNU style options.
For example, <i>ls</i> provides users with a command
providing BSD style options. The command <i>gls</i>, which is prefixed
with a "g" designates the <i>ls</i> command providing GNU style options.
This is very noteworthy because most users are more aquainted with
binaries with GNU style options. The most popular commands are:
<i>gls --color=always</i> and <i>gdiff</i>.
<h3>2. File naming</h3>
<p>It's good form to name a file or directory without empty spaces in
the name. A file such as <i>my journal.txt</i> may be more cumbersome
to handle than a file called <i>my_journal.txt</i>. This is because
the shell wants to see the above <i>my journal.txt</i> as two separate
files: one that is called <i>my</i> and another that is called
<i>journal.txt</i>. If you happen upon a file with empty spaces in
its name, you can use the following methods to manipulate it:</p>
<ol>
<li>Use single or double quotes around the file:
<ul>
<li><code>rm "my file.txt"</code> or</li>
<li><code>cp 'my file.txt' my_file.txt</code>, for example.</li>
</ul>
</li>
<li>Use a backslash where there's an empty space:
<ul>
<li><code>mv my\ file.txt my_file.txt</code> or</li>
<li><code>cd backups/text\ pages/</code>.</li>
</ul>
</li>
<li>Use Tab-Completion: many shells support tab completion. This
means if you type the first two or three characters of a file name
and press your Tab button, unless you have multiple files with
similar beginning names, your shell may be able to finish the word
completion for you. Note: if you're not in the same directory as
the file you want to tab complete, you'll need to provide the path,
which tab-completion can help with also.</li>
</ol>
<p>Also, when naming a file using two or more words, the safest
choices you have to use are the underscore, the dash, and the period.
Examples are:</p>
<i>my_file.txt</i><br>
<i>my-file.txt</i><br>
<i>my.file.txt</i>
<p>Or, you can keep it as one word:</p>
<i>myfile.txt</i>
<p>Using other symbols, like the ampersand, may cause problems because
some of these symbols may mean other things to the shell. As always,
for more information you should read the man-pages for the shell of
your choice.</p>
<h3>Commands</h3>
<p>Note: in the examples below, the percent sign is used to denote the
command prompt and is not meant to be typed.</p>
<h3>touch and pwd : create a file and print the working directory</h3>
<p>To create a file without invoking a text editor or another program,
one simply has to <i>touch</i> it. For example, to create a file called
<i>orange.txt</i>, at the command prompt type:</p>
<code>% touch orange.txt</code>
<p>Nothing much to that! To see the file you created you have the
ability to list the file(s) and directories in the current working
directory. First, let's see which directory we are in. By default,
upon creating a ssh link or a telnet link to your shell account, you
will be in your home directory. To confirm this, at the command prompt
you can type:</p>
<code>% pwd</code>
<p>If your user name is georgette, you may get something like this:</p>
<code>/udd/g/georgette</code>
<p>Or if you're on your home computer, perhaps you'll see something like
this:</p>
<code>/home/georgette</code>
<h3>ls : list files in current directory</h3>
<p>Now to list the files in your current directory, type:</p>
<code>% ls</code>
<p>If you followed the tutorial and created the <i>orange.txt</i> file
with the <i>touch</i> command, you should see this file in what the
<i>ls</i> command yields. Next, try <i>ls</i> with the various
options below and see the difference in the kinds of information each
option provides:</p>
<code>% ls -l</code><br>
<code>% ls -hl</code><br>
<code>% ls -a</code><br>
<code>% ls -al</code><br>
<code>% ls -ahl</code><br>
<h3>cp : copy a file</h3>
<p>Copying a file is likewise very easy. The copy command serves two
important functions: to make a simple backup of the file in question
and to also rename a file while keeping the original.</p>
<p>Say you want to backup your <i>orange.txt</i> file to a
sub-directory (more about creating directories in a moment) called
<i>backups</i>. To do so, you would type the following:</p>
<code>% cp orange.txt backups/</code>
<p>The forward slash at the end of the word <i>backups</i> means that
this is a directory.</p>
<p>To use the <i>cp</i> command to change the name of the file without
destroying the original you would type the following:</p>
<code>% cp orange.txt papaya.txt</code>
<p>where <i>papaya.txt</i> is the new name of the file.</p>
<p>And to copy the original <i>orange.txt</i> file to the backup
directory and to change the name at the same time, you would type:</p>
<code>% cp orange.txt backups/papaya.txt</code>
<h3>mv : move or rename a file</h3>
<p>The <i>mv</i> command works similarly to the <i>cp</i> command but
with one vital difference. Moving a file means destroying the
original file name. Thus the following command:</p>
<code>% mv orange.txt papaya.txt</code>
<p>essentially replaces the <i>orange.txt</i> with the new
<i>papaya.txt</i> file.</p>
<p>You can keep the file name the same with the <i>mv</i> command by
moving the file to a separate directory. To do so, type the
following:</p>
<code>% mv orange.txt backups/</code>
<p>This would move the <i>orange.txt</i> file to the backups
directory. To move the file to the backups directory and to rename it
then, you'd type:</p>
<code>% mv orange.txt backups/papaya.txt</code>
<h3>rm : remove a file</h3>
<p>Removing a file is also very simple. The command to do so is
<i>rm</i>. To completely remove and destroy a file simply type:</p>
<code>% rm orange.txt</code>
<h4>short note on interactive use</h4>
<p>The commands for copying, moving and removing files if carelessly
used may wreak a bit of havoc for you. For these commands, you may
want to invoke the interactive option by typing:</p>
<code>% cp -i orange.txt backups/orange.txt</code><br>
<code>% mv -i orange.txt papaya.txt</code><br>
<code>% rm -i orange.txt</code>
<p>When the interactive option is called, you'll be prompted to answer
yes or no to each file you are asking to be removed. For
<i>cp -i</i> and <i>mv -i</i>, you'll be prompted if and only if the file you are
copying to or moving will overwrite another file.</p>
<h3>file : examine type of file</h3>
<p>The <i>file</i> command is useful to determine what type of file a
file is. In unix-like operating systems, a file's name is fairly
flexible and the file extension, e.g. the .txt appendage, is not
always necessary. So if someone sent you a file and you wanted to be
certain what type of file it was before you opened it, use the
<i>file</i> command like so:</p>
<code>% file name_of_file</code>
<p>The results for a text file would be something like this:</p>
<code>name_of_file: ASCII text</code>
<p>Say someone sent you an image file called <i>something.something</i>
in the PNG format and you wanted to be certain it was actually a PNG
file, simply type:</p>
<code>% file something.something</code>
<p>If the file is truly a PNG file, you should see something similar to
this:</p>
<code>something.something: PNG image data, 922 x 691, 8-bit/color RGBA,
non-interlaced</code>
<h3>less : read a file</h3>
<p>The <i>less</i> command is a type of pager available to view
and browse through text files without altering or opening the file
in a text editor. You're encouraged to read the man-page for this
command because it possesses many useful attributes such as
searching through text for key words or strings. Invoke it with
the name of the file you want to view:</p>
<code>% less orange.txt</code>
<p>If there is more text in the file than can fit on your computer
screen, press the space-bar to scroll down page by page. Oftentimes,
your Page Up and Page Down buttons on your keyboard will work and the
arrow keys normally allow you to go up and down through the file line
by line.</p>
<h3>mkdir : create a directory</h3>
<p>You create a directory by using the <i>mkdir</i> command. To create
the backups directory we used in earlier examples, type:</p>
<code>% mkdir backups</code>
<h3>cd : change directory</h3>
<p>The <i>cd</i> command is used to change directories. If we are in
our home directory and want to go to the newly created <i>backups</i>
sub-directory, we'd simply type:</p>
<code>% cd backups</code>
<p>To go back simply type:</p>
<code>% cd</code>
<p>Typing <i>cd</i> all by itself will always take you back to your
home directory which is useful if you're deep in another branch of the
directory tree. If you just want to go back up a level, type:</p>
<code>% cd ..</code>
<p>And of course, you can always type the full path to the directory you
want to change to:</p>
<code>% cd /usr/bin</code>
<p>To switch back to the previous working directory, type:</p>
<code>% cd -</code>
<h3>rmdir : remove a directory</h3>
<p>And to remove an empty directory, you use the the <i>rmdir</i>
command.</p>
<code>% rmdir backups</code>
<p>The <i>rmdir</i> will only work if the directory you want to remove
is empty of files. If a directory contains files in it and you are sure
you want to remove said directory along with all the files in it, you
actually have to go back to the <i>rm</i> command and type:</p>
<code>% rm -r name_of_directory</code>
<p>The <i>-r</i> is a command option telling the <i>rm</i> or remove
command to delete the directory and all contents including
subdirectories. It stands for <i>recursive</i>. Be very careful
about using this command! In fact, a better way to run this command
is by typing:</p>
<code>% rm -ir</code>
<p>This invokes the interactive use of the remove command which
prompts you to answer yes or no to each file and directory to be
possibly removed. Again, read the man-pages for more details.</p>
<h3>clear : clear screen</h3>
<p>Finally, to clear the screen type the following at the prompt:</p>
<code>% clear</code></br>
<hr />
<cite>$Id: file_operations.html,v 1.11 2009/11/28 10:39:54 rogerx Exp $</cite>
</div> <!-- main -->
</body>
</html>