213 lines
10 KiB

====== Basic File and Shell Operations ======
In this tutorial, we'll briefly explain some basic file and shell operations using the following commands:
===== Commands =====
- touch -- create a file
- pwd -- print working directory
- ls -- list files in current directory
- cp -- copy a file
- mv -- move a file
- rm -- remove a file
- file -- examine type of file
- less -- read a file
- mkdir -- create a directory
- cd -- change directory
- rmdir -- remove a directory
- clear -- clear screen
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 [[http://sdf.lonestar.org/index.cgi?tutorials/findinghelp|//Finding Help From Within the Shell//]] tutorial.
==== First some notes about: ====
* Command options
* File naming
==== 1. Command options ====
All of the commands above and many commands you'll discover and use in the shell can be modified with the use of //options//. See the man-pages for details about the available options for each command. //Options// normally come in the form of //ls -l//, that is, you type the command, space over once, type a dash and the letter for the option you need to use.
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, //ls// provides users with a command providing BSD style options. The command //gls//, which is prefixed with a "g" designates the //ls// 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: //gls --color=always// and //gdiff//.
==== 2. File naming ====
It's good form to name a file or directory without empty spaces in the name. A file such as //my journal.txt// may be more cumbersome to handle than a file called //my_journal.txt//. This is because the shell wants to see the above //my journal.txt// as two separate files: one that is called //my// and another that is called //journal.txt//. If you happen upon a file with empty spaces in its name, you can use the following methods to manipulate it:
- Use single or double quotes around the file:
* "rm "my file.txt"" or
* "cp 'my file.txt' my_file.txt", for example.
- Use a backslash where there's an empty space:
* "mv my\ file.txt my_file.txt" or
* "cd backups/text\ pages/".
- 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.
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:
//my_file.txt//\\ //my-file.txt//\\ //my.file.txt//
Or, you can keep it as one word:
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.
==== Commands ====
Note: in the examples below, the percent sign is used to denote the command prompt and is not meant to be typed.
==== touch and pwd : create a file and print the working directory ====
To create a file without invoking a text editor or another program, one simply has to //touch// it. For example, to create a file called //orange.txt//, at the command prompt type:
"% touch orange.txt"
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:
"% pwd"
If your user name is georgette, you may get something like this:
Or if you're on your home computer, perhaps you'll see something like this:
==== ls : list files in current directory ====
Now to list the files in your current directory, type:
"% ls"
If you followed the tutorial and created the //orange.txt// file with the //touch// command, you should see this file in what the //ls// command yields. Next, try //ls// with the various options below and see the difference in the kinds of information each option provides:
"% ls -l"\\ "% ls -hl"\\ "% ls -a"\\ "% ls -al"\\ "% ls -ahl"\\
==== cp : copy a file ====
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.
Say you want to backup your //orange.txt// file to a sub-directory (more about creating directories in a moment) called //backups//. To do so, you would type the following:
"% cp orange.txt backups/"
The forward slash at the end of the word //backups// means that this is a directory.
To use the //cp// command to change the name of the file without destroying the original you would type the following:
"% cp orange.txt papaya.txt"
where //papaya.txt// is the new name of the file.
And to copy the original //orange.txt// file to the backup directory and to change the name at the same time, you would type:
"% cp orange.txt backups/papaya.txt"
==== mv : move or rename a file ====
The //mv// command works similarly to the //cp// command but with one vital difference. Moving a file means destroying the original file name. Thus the following command:
"% mv orange.txt papaya.txt"
essentially replaces the //orange.txt// with the new //papaya.txt// file.
You can keep the file name the same with the //mv// command by moving the file to a separate directory. To do so, type the following:
"% mv orange.txt backups/"
This would move the //orange.txt// file to the backups directory. To move the file to the backups directory and to rename it then, you'd type:
"% mv orange.txt backups/papaya.txt"
==== rm : remove a file ====
Removing a file is also very simple. The command to do so is //rm//. To completely remove and destroy a file simply type:
"% rm orange.txt"
=== short note on interactive use ===
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:
"% cp -i orange.txt backups/orange.txt"\\ "% mv -i orange.txt papaya.txt"\\ "% rm -i orange.txt"
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 //cp -i// and //mv -i//, you'll be prompted if and only if the file you are copying to or moving will overwrite another file.
==== file : examine type of file ====
The //file// 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 //file// command like so:
"% file name_of_file"
The results for a text file would be something like this:
"name_of_file: ASCII text"
Say someone sent you an image file called //something.something// in the PNG format and you wanted to be certain it was actually a PNG file, simply type:
"% file something.something"
If the file is truly a PNG file, you should see something similar to this:
"something.something: PNG image data, 922 x 691, 8-bit/color RGBA, non-interlaced"
==== less : read a file ====
The //less// 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:
"% less orange.txt"
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.
==== mkdir : create a directory ====
You create a directory by using the //mkdir// command. To create the backups directory we used in earlier examples, type:
"% mkdir backups"
==== cd : change directory ====
The //cd// command is used to change directories. If we are in our home directory and want to go to the newly created //backups// sub-directory, we'd simply type:
"% cd backups"
To go back simply type:
"% cd"
Typing //cd// 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:
"% cd .."
And of course, you can always type the full path to the directory you want to change to:
"% cd /usr/bin"
To switch back to the previous working directory, type:
"% cd -"
==== rmdir : remove a directory ====
And to remove an empty directory, you use the the //rmdir// command.
"% rmdir backups"
The //rmdir// 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 //rm// command and type:
"% rm -r name_of_directory"
The //-r// is a command option telling the //rm// or remove command to delete the directory and all contents including subdirectories. It stands for //recursive//. Be very careful about using this command! In fact, a better way to run this command is by typing:
"% rm -ir"
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.
==== clear : clear screen ====
Finally, to clear the screen type the following at the prompt:
"% clear"
$Id: file_operations.html,v 1.11 2009/11/28 10:39:54 rogerx Exp $