Introducción a UNIX

Instructor: Darío Mitnik


Basic Introduction to Unix commands

This is a very brief introduction to some useful Unix commands, including examples of how to use each command.
pwd ls mkdir rmdir
cd mv rm cp
vi cat man kill

pwd

This command reports the current directory path. Enter the command by itself:

  pwd

ls

This command will list the files stored in a directory. To see a brief, multi-column list of the files in the current directory, enter:

  ls 

To also see "dot" files (configuration files that begin with a period, such as .login ), enter:

  ls -a 

To see the file permissions, owners, and sizes of all files, enter:

  ls -la 

If the listing is long and scrolls off your screen before you can read it, combine ls with the less utility, for example:

  ls -la | less 

mkdir

This command will make a new subdirectory.

To create a subdirectory named mystuff in the current directory, enter:

  mkdir problemas

To create a subdirectory named bohr in the existing directory named problemas, enter:

  mkdir problemas/bohr 

Note: To make a subdirectory in a particular directory, you must have permission to write to that directory.

rmdir

This command will remove a subdirectory. To remove a subdirectory named problemas, enter:

  rmdir problemas 

Note: The directory you specify for removal must be empty. To clean it out, switch to the directory and use the ls and rm commands to inspect and delete files.

cd

This command changes your current directory location. By default, your Unix login session begins in your home directory.

To switch to a subdirectory (of the current directory) named problemas, enter:

  cd problemas

To switch to a directory named problemas/bohr, enter:

  cd problemas/bohr 

To move to the parent directory of the current directory, enter:

  cd .. 

To move to the root directory, enter:

  cd /

To return to your home directory, enter:

  cd

mv

This command will move a file. You can use mv not only to change the directory location of a file, but also to rename files. Unlike the cp command, mv will not preserve the original file.

Note: As with the cp command, you should always use  -i  to make sure you don't overwrite an existing file.

To rename a file named oldname in the current directory to the new name newname, enter:

  mv -i oldname newname 

To move a file named viejo1 from a subdirect named problemas/bohr to another subdirectory named problemas/schrodinger (both subdirectories of the current directory), enter:

  mv -i bohr/viejo1 schrodinger/.

If, in this last operation, you also wanted to give the file a new name, such as nuevo1, you would enter:

  mv -i bohr/viejo1 schrodinger/nuevo1 

rm

This command will remove (destroy) a file. You should enter this command with the  -i  option, so that you'll be asked to confirm each file deletion. To remove a file named junk, enter:

  rm -i junk

Note: Using rm will remove a file permanently, so be sure you really want to get rid of the file before you use rm.

cp

This command copies a file, preserving the original and creating an identical copy. If you already have a file with the new name, cp will overwrite and destroy the duplicate. For this reason, it's safest to always add  -i  after the cp command, to force the system to ask for your approval before it destroys any files. The general syntax for cp is:

  cp -i oldfile newfile 

To copy a file named problema1 in the directory problemas/bohr to your current directory, enter:

  cp -i problemas/bohr/problema1 . 

The  .  (period) indicates the current directory as destination, and the  -i  ensures that if there is another file named problema1 in the current directory, you will not overwrite it by accident.

To copy a file named oldfile in the current directory to the new name newfile in the problemas subdirectory of your home directory, enter:

  cp -i oldfile ~/problemas/newfile 

The  ~  character (tilde) is interpreted as the path of your home directory.

Note: You must have permission to read a file in order to copy it.

vi

This command starts the vi text editor. To edit a file named myfile in the current directory, enter:

  vi myfile 

The vi editor works fairly differently from other text editors. If you haven't used it before, you should probably look at a tutorial, such as the Knowledge Base document How do I use the vi text editor? Another helpful document for getting started with vi is A quick reference list of vi editor commands.

The very least you need to know to start using vi is that in order to enter text, you need to switch the program from command mode to insert mode by pressing  i . To navigate around the document with the cursor keys, you must switch back to command mode by pressing Esc. To execute any of the following commands, you must switch from command mode to ex mode by pressing  :  (the colon key): Enter  w  to save;  wq  to save and quit;  q!  to quit without saving.

cat

This command outputs the contents of a text file. You can use it to read brief files or to concatenate files together.

To append file1 onto the end of file2, enter:

  cat file1 >> file2 

To view the contents of a file named myfile, enter:

  cat myfile

Because cat displays text without pausing, its output may quickly scroll off your screen. Use the less command (described below) or an editor for reading longer text files.

man

This command displays the manual page for a particular command. If you are unsure how to use a command or want to find out all its options, you might want to try using man to view the manual page.

For example, to learn more about the ls command, enter:

  man ls 

To learn more about man, enter:

  man man 

If you aren't sure of the exact command name, you can use man with the -k option to help you find the command you need. To see one line summaries of each reference page that contains the keyword you specify, enter:

  man -k keyword 

Replace keyword in the above example with the keyword which you want to reference.

kill

Use this command as a last resort to destroy any jobs or programs that you suspended and are unable to restart. Use the jobs command to see a list of suspended jobs. To kill suspended job number three, for example, enter:

  kill %3 

Now check the jobs command again. If the job has not been cancelled, harsher measures may be necessary. Enter:

  kill -9 %3


Lecturas Auxiliares Recomendadas:


  • Introduction to UNIX - Indiana University.
  • Introduction to UNIX - Strathclyde University.