UNIX is an operating system which was first developed in the 1960s, and has been under constant development ever since. By operating system, we mean the suite of programs which make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops.

UNIX systems also have a graphical user interface (GUI) similar to Microsoft Windows which provides an easy to use environment. However, knowledge of UNIX is required for operations which aren’t covered by a graphical program, or for when there is no windows interface available, for example, in a telnet session

The UNIX operating system is made up of three parts; the kernel, the shell and the programs.

UNIX kernel

The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the filestore and communications in response to system calls.

As an illustration of the way that the shell and the kernel work together, suppose a user types rm testfile (which has the effect of removing the file testfile). The shell searches the filestore for the file containing the program rm, and then requests the kernel, through system calls, to execute the program rm on testfile. When the process rm testfile has finished running, the shell then returns the UNIX prompt % to the user, indicating that it is waiting for further commands.

UNIX shell

The shell acts as an interface between the user and the kernel. When a user logs in, the login program checks the username and password, and then starts another program called the shell. The shell is a command line interpreter (CLI). It interprets the commands the user types in and arranges for them to be carried out. The commands are themselves programs: when they terminate, the shell gives the user another prompt (% on our systems).

The adept user can customize his/her own shell, and users can use different shells on the same machine. Staff and students in the school have the tcsh shell by default.

The tcsh shell has certain features to help the user inputting commands.

Filename Completion – By typing part of the name of a command, filename or directory and pressing the [Tab] key, the tcsh shell will complete the rest of the name automatically. If the shell finds more than one name beginning with those letters you have typed, it will beep, prompting you to type a few more letters before pressing the tab key again.

History – The shell keeps a list of the commands you have typed in. If you need to repeat a command, use the cursor keys to scroll up and down the list or type history for a list of previous commands.

UNIX Files and processes

Everything in UNIX is either a file or a process. A process is an executing program identified by a unique PID (process identifier). A file is a collection of data. They are created by users using text editors, running compilers etc.

Examples of files:

  • a document (report, essay etc.)
  • the text of a program written in some high-level programming language
  • instructions comprehensible directly to the machine and incomprehensible to a casual user, for example, a collection of binary digits (an executable or binary file);
  • a directory, containing information about its contents, which may be a mixture of other directories (subdirectories) and ordinary files.
  • Note: UNIX is case-sensitive

Basic UNIX Commands

Cal

The cal command prints a calendar to standard output. You can specify a month (using the number for the month) and a year, or just a year to print a calendar for an entire year. If you do not specify anything, a calendar for the current month is printed.

To show this month’s calendar, enter:

cal

To show a twelve-month calendar for 2009, enter:

cal 2009

To show a calendar for just the month of June 2009, enter:

cal 6 2009

date

The date command displays the current day, date, time, and year.

Examples

  1. To display current date and time, enter : date
  2. To set the date and time, enter : date 0217142590. For a system using CST as its time zone, this sets the date and time to Sat Feb 17 14:25:00 CST 1990. Note: You must have root authority to change the date and time.
  3. 3 To display the date and time in a specified format, enter: date +"%r %a %d %h %y (Julian Date: %j)"
  4. This displays the date shown in Example 2 as: 02:25:03 PM Fri 17 Feb 90 (Julian Date: 048)

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 are not 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

less and more

These commands allow you to display the contents of a file, which is useful if you want to scroll through a file that is too long to fit within one screen. Two simple ways to do this are to use the more and less commands. These commands are similar in that you can read, but not edit a file using this command.

Using more, press the return key to view the next line of text, the spacebar to read the next page of text, q to quit, and / to search.

You can use the same commands in less, but in addition you can use the arrow keys to navigate by line, and u to move up a page. A useful option is -N, which numbers the line in the files.

To use more or less, type more or less and the name of the file you want to read. Type any option you want to use before the filename

To read the contents of a file named textfile in the current directory, enter:

less textfile

The less utility is often used for reading the output of other commands. For example, to read the output of the ls command one screen at a time, enter:

ls -la | less

In both examples, you could substitute more for less with similar results. To exit either less or more, press q . To exit less after viewing the file, press q .

Note: Do not use less or more with executables (binary files), such as output files produced by compilers. Doing so will display garbage and may lock up your terminal

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 myfiles, enter:

cd myfiles

To switch to a directory named /home/dvader/empire_docs, enter:

cd /home/dvader/empire_docs

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

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 meeting1 in the directory /home/dvader/notes to your current directory, enter:

cp -i /home/dvader/notes/meeting1 .

The  .  (period) indicates the current directory as destination, and the  -i  ensures that if there is another file named meeting1 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 mystuff subdirectory of your home directory, enter:

cp -i oldfile ~/mystuff/newfile

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

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 do not 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 hw1 from a subdirectory named newhw to another subdirectory named oldhw (both subdirectories of the current directory), enter:

mv -i newhw/hw1 oldhw

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

mv -i newhw/hw1 oldhw/firsthw

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 delete a file before you use rm.

To remove a non-empty subdirectory, rm accepts the  -r  option. On most systems this will prompt you to confirm the removal of each file. This behavior can be prevented by adding the  -f  option. To remove an entire subdirectory named oldstuff and all of its contents, enter:

rm -rf oldstuff

Note: Using this command will cause rm to descend into each subdirectory within the specified subdirectory and remove all files without prompting you. Use this command with caution, as it is very easy to accidently delete important files. As a precaution, use the ls command to list the files within the subdirectory you wish to remove. To browse through a subdirectory named oldstuff, enter:

ls -R oldstuff | less

rmdir

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

rmdir oldstuff

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.

mkdir

This command will make a new subdirectory.

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

mkdir mystuff

To create a subdirectory named morestuff in the existing directory named /tmp, enter:

mkdir /tmp/morestuff

 

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

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

df

This command reports file system disk usage (i.e., the amount of space taken up on mounted file systems). For each mounted file system, df reports the file system device, the number of blocks used, the number of blocks available, and the directory where the file system is mounted.

To find out how much disk space is used on each file system, enter the following command:

df

If the df command is not configured to show blocks in kilobytes by default, you can issue the following command:

df –k

du

This command reports disk usage (i.e., the amount of space taken up by a group of files). The du command descends all subdirectories from the directory in which you enter the command, reporting the size of their contents, and finally reporting a total size for all the files it finds.

To find out how much disk space your files take up, switch to your home directory with the cd command, and enter:

du

The numbers reported are the sizes of the files; on different systems, these sizes will be in units of either 512 byte blocks or kilobytes. To learn which is the case, use the man command, described below. On most systems, du -k will give sizes in kilobytes.

pwd

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

Pwd

chmod

This command changes the permission information associated with a file. Every file (including directories, which Unix treats as files) on a Unix system is stored with records indicating who has permission to read, write, or execute the file, abbreviated as r, w, and x. If you want to change who can read a file you use the command chmod. You must then specify who you are changing permission for and what permission you are changing it to. Who and the permission are connected by either a ‘+’ (to add a permission), a ‘-’ (to remove a permission), or an ‘=’ (to assign a permission and remove other permissions). Who can be either ‘u’ for the user, ‘g’ for the group the user belongs to (all users belong to at least one group), ‘o’ for everyone else, and ‘a’ all. The common permissions include r (read), w (write), and x (execute – for directories and executable files). Here are some examples of how to change permissions on the file foo.html:

Add read permission for all

chmod a+r foo.html

Add read and write permissions for the group:

chmod g+rw foo.html

remove execute permission for others:

chmod o-x foo.html

To allow yourself to execute a file that you own named myfile, enter:

chmod u+x myfile

To allow anyone who has access to the directory in which myfile is stored to read or execute myfile, enter:

chmod o+rx myfile

You can view the permission settings of a file using the ls command, described below.

Note: Be careful with the chmod command. If you tamper with the directory permissions of your home directory, for example, you could lock yourself out or allow others unrestricted access to your account and its contents.

Related Posts

  1. Basic Unix Commands – Part 2
  2. UNIX Command “find” – Tutorial
  3. Commands Options and Modes in UNIX VI Editor
  4. UNIX Command Tips for Developers
  5. Migrating Oracle Database from HP UNIX to LINUX

Tags: , , , ,

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>