Unix Tutorial


What is Unix?

Getting started

Hardware

Software

Basic Commands

vi Commands

 

Files

File Commands

Different types of file

File Redirection

Permissions

Pipelines / Filters

 

Directory

Directory Structure

Directory Command

Shell

Different types of shell

Compiling Program

Networking

Commands

Reference

Reference Commands

online help : Manpages

Useful links/Books

 

Search Engine

Files Commands


Directories are used to organize your files.Here is the list of directory commands :

cp || mv|| rm || cat || more/less|| lpr || du || find || file || chmod || comress/uncompress|| ps||tar || tail || gzip/gunzip || bzip2/bunzip2


cp Command:

  • There are two ways you will want to use this command.
    1. You will want to copy a file from someone else's directory (like a data set for an assignment) to your directory so you can use the file. The syntax for the command is: cp 'path to the file you want/filename' 'path to where you want the file copied/filename'

For example: you want to copy the file assignment1 from your TA's home directory to your directory and call it homework1.

2.To copy a single file in the current directory: type

%cp notes sect3.txt

This makes a copy of the file notes in the file sect3.txt.

In case you are not sure if you have the file then you can use this:

To copy a file using the -i option: type

%cp -i notes part2.txt

This makes a copy of the file notes in the file part2.txt. If this file exists the prompt part2.txt: File exists is displayed. The file is not copied.

top of the page


mv command :(Make Directory)

The command mkdir creates a new directory. You will find it useful to make a new directory for each new assignment you have.
We will now make a subdirectory in your home directory to hold the files you will be creating and using in the course of this tutorial. To make a subdirectory called csc1000 in your current working directory type % mkdir csc1000 .To see the directory you have just created, type % ls
Here are few of the examples you can try to rename a file:type

% mv junk precious

This renames the file junk as the file precious.

To be prompted when using the mv command:type

% mv -i draft1.txt part1.txt

remove part1.tx?n

This command prompts you to confirm that you really want to move the file draft1.txt to the file part1.txt; a file with that name already exists. Entering n cancels this action: a y confirms it.

To move a file to another directory and rename it:type

% mv jewel casket/amethyst

This moves the file jewel to the filename amethyst in the subdirectory casket.

top of the page


rm Command

The command rm my_file deletes my_file. The form rm -i my_file asks if you really want to remove the file my_file before it proceeds.Here are the few examples

To remove a single file:type

% rm help.txt

This removes the file help.txt from the current directory.

To remove several files:type

% rm artwork/figure[2-7]

This removes files figure2 through to figure7 from the subdirectory artwork.

To remove files interactively: type

%rm -i *.fm This will prompt you to confirm that you want to remove a file from the current directory whenever that file has the suffix .fm. Answering y will delete the file. The file is not deleted if any other response is given.

top of the page


cat Command

The cat command is useful for displaying short files of a few lines. To display longer files use an editor or pager. To display the contents of a file use the command:

%cat filename

For example to display a file README, type

%cat README

This displays the contents of the file README.

To display line numbers type

%cat -b myprog.c

This displays the contents of myprog.c and numbers the lines. Blank lines are not numbered.

top of the page


more/less Command

The command more my_file displays the text of my_file one page at a time.To display a file use the command:

% more filename

This will display the first screenful of the file. The prompt --More--(nn%) is given in the bottom left-hand corner of the screen. You can now use one of the following commands

space bar : Display next screenful of text.

<RETURN> : Display next line of text.

q : Exit from more: this can be done at any time.

d : Scroll forwards about half a screenful of text.

b : Skip backwards one screenful of text.

h : Display a list of commands (help).

less has better features than more and can be customised to suit your particular needs. Many of its commands are the same as those of the text editor vi. To display a file use the command type:

%less filename

This will display the first screenful of the file. A prompt with the filename is given in the bottom left-hand corner of the screen. You can now move around the file using the less commands.

Summary of less commands

Commands marked with * may be preceded by a number, N. Notes in parentheses indicate the behavior if N is given.

h H : Display a complete command summary

q :q :Q ZZ : Exit.

e ^E j ^N CR * : Forward one line (or N lines).

y ^Y k ^K ^P * :Backward one line (or N lines).

f ^F ^V SPACE * : Forward one window (or N lines).

b ^B ESC-v * : Backward one window (or N lines).

z * : Forward one window (set window to N).

w * : Backward one window (set window to N).

d ^D * : Forward one half-window (set half-window to N).

u ^U * : Backward one half-window (set half-window to N).

r ^R ^L : Repaint screen.

R : Repaint screen, discarding buffered input.

/pattern * : Search forward for (N-th) matching line.

?pattern * : Search backward for (N-th) matching line.

NOTE: search commands may be modified by one or more of:

! search for NON-matching lines.

* search multiple files.

@ start search at first file (for /) or last file (for ?).

top of the page


lpr Command

The command lpr print_file sends print_file to the default printer.

To print a single file:type

% lpr memo

This prints the file memo to your default printer.

To print several files:type

%lpr may june july

This prints the files may, june and july to your default printer.

To cancel a print request use the command:type

% lprm job_number

This removes the print job from the print queue. You cannot remove another user's print job from the print queue; only the ones that belong to you.

top of the page


du Command

This command is used to find out how much disk space is used.

df (disk free) command is used to find out how much space is available.Type

%du

top of the page


find command

To locate a file in the file system, use the find command.

find pathname -name filename -print The pathname defines the directory to start from. Each subdirectory of this directory will be searched. The -print option must be used to display results. You can define the filename using wildcards. If these are used, the filename must be placed in 'quotes'. Examples

To find a single file below the current directory:type

% find . -name mtg_jan92 -print This displays the pathname to the file mtg_jan92 starting from the current directory. If the file is not found nothing is displayed.

To find a file below your home directory:type

% find ~/ -name README -print

This displays the pathname to every file with the name README in your home directory or its subdirectories.

To find several files below the current directory: type

%find . -name '*.fm' -print

This displays the pathname to any file with the extension .fm which exists below the current directory. To find a directory:type

% find /usr/local -name gnu -type d -print

This searches to see if there is a subdirectory gnu in the directory /usr/local.

top of the page


file Command

The file command examines the content of a file and reports what type of file it is. To use the command type:

% file filename

Here are the few examples:

1. To check the identity of a single file:type

% file ReportA

ReportA: Frame Maker document

This checks the identity of the file ReportA and reports that this file has been created using the Frame Maker application.

To check the identity of several files:type

% file*

external.c:c program text

external.h: c program text

mc: sparc demand paged dynamically linked executable

mcXThelp: executable shell script

mcdemo: executable shell script

mchelp: executable shell script

mchome: executable shell script

mctools: executable shell script

This checks the identity of every file in the current directory. Notice how it reports whether a file is an executable binary or a shell script.

top of the page


chmod Command

The command chmod refers to changing the mode of a files permissions. Permissions are set for "owner, group and other" users. The available permissions for these users are read, write and execute. Each of these user's have a numeric permission equivalent of; r = 4, w = 2, x = 1.

To assign more than one value to any of the users simply add the numbers together i.e., chmod 756 will set the permissions of a file to owner = rwx, group = r-x, other = rw-.

top of the page

Unix has a built-in program which gives us the capability of compressing huge files into a smaller size files.For doing the reverse type uncompress.Type

%ls -l list

-rw------- 1 taylor 106020 Oct 10 13:24 list

%compress list

%ls -l list.z

-rw------- 1 taylor 44130 Oct 10 13:25 list.z

%uncompress list

%ls -l list

-rw------- 1 taylor 106020 Oct 13:25 list

To compress several files:type

% compress sect[1-8].fm

This compresses files sect1.fm through sect8.fm.

uncompress command

To recover compressed files:type

% uncompress sect[1-4].fm.Z

This will decompress the compressed files sect1.fm.Z through sect4.fm.Z.

top of the page


ps Command

The ps command lists the processes that are running right now on your system. Even at rest, Unix systems run quite a few processes.

top of the page


tail Command

To display the last 10 lines of a text file type

%tail fred

This displays the last 10 lines of the file fred which is in the current directory.

To display the last 50 characters in a file type

%tail -50c help.txt

This displays the last 50 characters in the file help.txt which is in the current directory.

top of the page


tar Command

The tar command can be used to turn several files into a single tarfile. tar can also be used to bundle up whole directories before transferring them to another system. tar stands for Tape Archive.It was originally designed for tape backups, but it is used to create a tar file anywhere on the filesystem. tar creates one "tar file" (also known as a "tarball") out of several files and directories. A tar file isn't compressed, it's just a heap of files assembled together in "one container". So, the tar file will take up the same amount of space as all the individual files combined, plus a little extra. A tar file can be compressed by using gzip or bzip2.

Here are few of the examples of tar command:

tar -xvf example.tar

Extract the contents of example.tar and display the files as they are extracted.

tar -cvf name_of_tar_file.tar name_of _directory

What ever files are there in name_of_directory folder is tared into single file with extension .tar and here the name is name_of_tar_file.Some times you would like to tar few of the files in the specific directory or folder.The way to tar few files only is:

tar cf name_tar.tar abc.cpp ddd.txt lmn.cpp

tar -cf backup.tar /home/ftp/pub

Create a tar file named backup.tar from the contents of the directory /home/ftp/pub

tar -tvf example.tar

list contents of example.tar to the screen

top of the page


gzip/gunzip

This command gzip is the original Unix ZIP format. It's common to first "tar" files and then compress them afterwards using gzip. These files are normally given the extentions .tar.gz to show that they are tar archives zipped up with gzip. You may also see the extension, .tgz. An archive that is compressed with gzip is compatible with WinZip and PkZip. So, a file zipped up on a UNIX box can be unzipped with a Windows box.

Here are some examples:

  • To compress a file using gzip, execute the following command:
  • % gzip filename.tar (where filename.tar is the name of the file you wish to compress)

    The result of this operation is a file called filename.tar.gz. By default, gzip will delete the filename.tar file.

  • To decompress a file using gzip, execute the following command:

    %gzip -d filename.tar.gz

    The result of this operation is a file called filename.tar. By default, gzip will delete the filename.tar.gz file. You can also decompress the file using the command: gunzip filename.tar.gz This is the same as using the gzip -d command. There are many options that you can use with gzip.

top of the page


bzip2/bunzip2

bzip2 and bunzip2 are file compression and decompression utilities. The bzip2 and bunzip2 utilities are newer than gzip and gunzip and are rapidly gaining popularity. The bzip2 utility is capable of greater compression ratios than gzip. Therefore, a bzip2 file can be 10-20% smaller than a gzip version of the same file. Usually, files that have been compressed by bzip2 will have a .bz2 extension. The utilities work very similar to gzip and gunzip.

Here are few of examples.

To compress a file using bzip2, execute the following command:

%bzip2 filename.tar (where filename.tar is the name of the file you wish to compress)

The result of this operation is a file called filename.tar.bz2. By default, bzip2 will delete the filename.tar file.

To decompress a file using gzip, execute the following command:

%bunzip2 -d filename.tar.bz2

The result of this operation is a file called filename.tar. By default, bunzip2 will delete the filename.tar.bz2 file.

top of the page