You are on page 1of 6

CSBP 315 – Operating Systems Fundamentals

Lab 3 – Basic Unix Commands Part 2


Objective:
Practice common UNIX commands.

Procedure:
1. Open your Unix shell and try these commands:
 Create a new file and add some text in it
v cat > filename

 View a file
v cat /etc/passwd
v more /etc/passwd
v more filename

 Copy file1, making file2


v cp file1 file2

 Rename file1 as file2


mv file1 file2

 Delete file1 as file2


rm file Delete file
rm -i file Double-check first

 Counts the lines, words, characters in file


wc file

 Output can be redirected to a file with’>’

1
ls > dir.txt
cal 2020 > year2020
cal 2 2020 > Feb2020

 Output can be appended to a file with ‘>>‘


cat file1 >> file2

 Concatenate two files


cat f1 f2 > fs

Tutorial 1 (Using cp command):

cp file1 file2 is the command which makes a copy of file1 in the current
working directory and calls it file2

What we are going to do now, is to take a file stored in an open access


area of the file system, and use the cp command to copy it to your ‘Lab2’
directory.

First, cd to your ‘Lab2’ directory.

% cd /Lab2
Then at the UNIX prompt, type,

% cp /home/Admin/Desktop/Text.doc .

Note: Don't forget the dot . at the end. Remember, in UNIX, the dot means
the current directory.

The above command means copy the file science.txt to the current
directory, keeping the name the same.

Task :
1. Create two directories in your home directory with name D1 and D2
2. Create a text file by using a text editor and save it by name file1( By
default file will be saved in your home directory.
3. Using the information given in the above tutorial and using cp
command make the file1 available in D1 and D2.

2
3
Tutorial 2 (Using mv command):
mv (move)

mv file1 file2 moves (or renames) file1 to file2

To move a file from one place to another, use the mv command. This has
the effect of moving rather than copying the file, so you end up with only
one file rather than two.

It can also be used to rename a file, by moving the file to the same
directory, but giving it a different name.

We are now going to move the file file1.txt to your Ex1 directory.

First, change directories to your D2 directory (can you remember how?).


Then, inside the D2 directory, type

% mv file1.txt Ex1/.

Type ls and ls Ex1 to see if it has worked.

Tutorial 3:

4
rm (remove), rmdir (remove directory)

To delete (remove) a file, use the rm command. As an example, we are


going to create a copy of the science.txt file then delete it.

Inside your Lab2 directory, type

% cp Text.doc tempfile.txt
% ls
% rm tempfile.txt
% ls

You can use the rmdir command to remove a directory (make sure it is
empty first). Try to remove the backups directory. You will not be able to
since UNIX will not let you remove a non-empty directory.

Task
Create a directory called tempstuff using mkdir , then remove it using the
rmdir command.

clear (clear screen)

Before you start the next section, you may like to clear the terminal
window of the previous commands so the output of the following
commands can be clearly understood.

At the prompt, type

% clear

This will clear all text and leave you with the % prompt at the top of the
window.

cat

The command cat can be used to display the contents of a file on the
screen. Type:

% cat file1.txt

5
Source: http://www.ee.surrey.ac.uk/Teaching/Unix/index.html

You might also like