1.Basic UNIX Commands
1.Basic UNIX Commands
basic commands
On a large system serving hundreds of users, someone has to be in
charge of the administration of the system. This person is known as the
system administrator.
The administrator is responsible for the management of the entire
setup. She allocates user accounts, maintains file systems, takes
backups, manages disk space, and performs several other important
functions. She is the person to be contacted in case of a genuine
problem.
If you own a workstation or PC that runs some flavor of UNIX, then you
are probably its administrator. You are then directly responsible for its
startup, shutdown, and maintenance. If you lose a file, it’s your job to
get it from a backup. If things don’t work properly, you have to try all
possible means to set them right before you decide to call a
maintenance person.
If you are not the administrator, you can use a UNIX machine only after
she has opened an account with a user ID and password for your use.
These authentication parameters are maintained in two separate files
on your system. You can’t simply sit down at any terminal and start
working unless you first log on to the system using a valid user-id–
password combination. The administrator uses a special user-id to log
on to the system: it is called root. The root user has near-absolute
powers. Some programs can only be run from this account—for
instance, the program that creates the user account itself.
Basic Structure of Command
Every command line command is a little computer program, even
commands as simple as clear.
Some simple commands like clear don’t require any options or
arguments. Options are usually preceded by a hyphen (-), and they
tweak the command’s behavior. Arguments can be names of files,
raw data, or other options that the command requires. A simple
command that has an argument is echo. The echo command prints
a phrase to the console.
If you want to see the last command, press the Up arrow key. You
can press Up and Down in order to scroll through the history of
commands that you’ve entered. If you want to re-execute a past
command, you can scroll to that command then press Enter. Try
getting back to the echo "Hello World!" command and execute it
again.
Computers are organized in a hierarchy of folders, where a
folder can contain many folders and files. People who use
Unix often refer to folders as directories, and these terms
are interchangeable. This directory hierarchy forms a tree,
like the diagram below. You can use the command line to
navigate these trees on your computer.
Hierarchical File structure
The home directory is another special directory that is
represented by a tilde (~). Your home directory contains
your personal files, like your photos, documents, and the
contents of your desktop. When you first open up your shell,
you usually start off in your home directory. Imagine tracing
all of the directories from your root directory to the directory
you’re currently in. This sequence of directories is called a
path.
Your shell starts in your home directory. Whatever directory
your shell is in, is called the working directory. Enter the
pwd command into your shell to print working directory.
You can change your working directory using the cd
command. If you use the cd command without any
arguments then your working directory is changed to your
home directory.
You can identify a specific file or folder by its path.
The root directory (/) contains all of the folders and
files on your computer.
Your home directory (~) is the directory where your
terminal always starts.
Use the cd command to change your working
directory.
The pwd command will print the working directory.
The ls command will list files and folders in a
directory.
Logging In and Out
Login:
When you first connect to a Unix system, you see a prompt such as the login
You should have your user ID (user identification) and password ready.
Type your user ID at the login prompt, then press ENTER. Your user ID is
case-sensitive, so be sure you type it exactly as your system administrator has
instructed.
Type your password at the password prompt, then press ENTER. Your
password is also case-sensitive.
If you provide the correct user ID and password, then you will be allowed to
enter into the system.
Logout:
When you finish your session, you need to log out of the system.
This is to ensure that nobody else accesses your files.
To log out
Press [ctrl-d]: Keep [Ctrl] pressed and then press d
Just type the logout command at the command prompt, and the system
will clean up everything and break the connection.
Type exit
System Information with date
and who
Date: To print the date and time information maintained by the UNIX system internal clock, one can use
the single command named date.
Who Command: UNIX can be used concurrently by multiple users, who command can be used to view the
List of Current Users.
In the second example :There are currently three users—romeo, henry, and steve—sharing the CPU. These
names are actually the user-ids they used to log in.
The output also includes your own user-id, (here csed/ romeo), which you entered at the login prompt to
gain entry to the system. The date and time of login are shown in three columns.
Basic commands to handle files
Handling Files: UNIX maintains all data in containers called files.
These files are assigned names, and a group of filenames are held
together in another separate file known as a directory.
Creating a File with echo: There are several ways to create a file.
Here we use the echo command with a special symbol (the >):
The cat command displays the contents of files, so let’s use it to view
the file that we just created:
Observe that we used both the echo and cat commands with an
additional word (date and foo). They are known as arguments. UNIX
commands are often used with arguments, and the variety of these
arguments makes these commands behave in numerous ways.
File Management
All data in Unix is organized into files. All files are
organized into directories.
These directories are organized into a tree-like structure
called the file system.
When you work with Unix, one way or another, you
spend most of your time working with files.
You need to understand how to create and remove files,
copy and rename them, create links to them, etc.
Creating a hidden file
- Create a file
- save it with . at the beginning
Touch old1
Ln old1 new1
Ln –s new1_soft old1
File System Types
Here, * works as meta character which matches with any character. If you
want to display all the files ending with just .doc, then you can use the
following command −
$ls *.doc
Creating Files
Once you are done with the program, follow these steps
−
Press the key esc to come out of the edit mode.
Press two keys Shift + ZZ together to come out of the file
completely.
• You will now have a file created with filename in the
current directory.
Editing Files
You can edit an existing file using the vi editor $ vi filename
Once the file is opened, you can come in the edit mode by
pressing the key i and then you can proceed by editing the file.
If you want to move here and there inside a file, then first you
need to come out of the edit mode by pressing the key Esc.
After this, you can use the following keys to move inside a file −
l key to move to the right side.
h key to move to the left side.
k key to move upside in the file.
j key to move downside in the file.
Using keys, you can position your cursor wherever you want to
edit.
Once you are positioned, then you can use the i key to come in
the edit mode. Once you are done with the editing in your file,
press Esc and finally two keys Shift + ZZ together to come out of
the file completely.
Display Content of a File
You can use the cat command to see the content of a file.
For example, to see the content of the above created file.
$ cat filename
This is unix file....I created it for the first time..... I'm going to save this
content in this file.
$
• You can display the line numbers by using the -b option along with
the cat command as follows
$ cat -b filename
1 This is unix file....I created it for the first time.....
2 I'm going to save this content in this file.
$
Counting Words in a File
You can use the wc command to get a count of the total number of lines, words, and characters
contained in a file. For example, to see the information about the file created above
$ wc filename
2 19 103 filename
$
Here is the detail of all the four columns:
First Column − Represents the total number of lines in the file.
Second Column − Represents the total number of words in the file.
Third Column − Represents the total number of bytes in the file. This is the actual size of
the file.
Fourth Column − Represents the file name.
You can give multiple files and get information about those files at a time. Following is simple
syntax
$ wc filename1 filename2 filename3
To find the number of lines alone
wc –l filename
Wc –w filename
Sorting the contents of a file
Copying Files
To make a copy of a file use the cp command. The basic syntax of
the command is:
You will now find one more file copyfile in your current directory. This
file will exactly be the same as the original file filename.
Renaming Files
To change the name of a file, use the mv command. Following is the
basic syntax:
The mv command will move the existing file completely into the new
file. In this case, you will find only newfile in your current directory.
Deleting Files
To delete an existing file, use the rm command. The basic syntax:
Here, dirname is the name of the directory that you want to change
to. For example, the command
cp needs two arguments and operates silently. If you run cat foo.sh now, cat will
also display the string date.
Displaying List of Filenames with ls: having two identical files, we
can produce a list of their names with the ls command:
one file and one directory, both in the current directory. ls will
now display both filenames, but if you follow it with -F (with
a space between ls and -F).
For example, to check the calendar, you need
to type the cal command as follows:
Change Password
All Unix systems require passwords to help ensure that
your files and data remain your own and that the system
itself is secure from hackers and crackers.
Following are the steps to change your password −
Step 1 − To start, type passwd at the command prompt.
Step 2 − Enter your old password, the one you're currently using.
Step 3 − Type in your new password. Always keep your password
complex enough so that nobody can guess it. But make sure, you
remember it.
Step 4 − You must verify the password by typing it again.
Change Password
Who are you?
While you're logged into the system, you might be
willing to know : Who am I?
The easiest way to find out "who you are" is to enter
the whoami command
$ whoami
amrood
$
Try it on your system. This command lists the account
name associated with the current login. You can try
who am i command as well to get information about
yourself.
Who is Logged in?
If you wants to know who is logged in to the computer at the same
time.
There are three commands available to get you this information,
based on how much you wish to know about the other users: users,
who, and w.
System Shutdown
The most consistent way to shut down a Unix system properly via the
command line is to use one of the following commands −
Assignment 1 Navigating the
Command Line Exercises