0% found this document useful (0 votes)
46 views

Exercise 1.listing Files and Directories: Command Meaning Ls Ls - A Mkdir CD Directory CD CD CD .

This document provides instructions on basic Linux commands for file management, file permissions, and searching files. It covers commands like ls, mkdir, cp, mv, rm, cat, less, head, tail, grep, and chmod. The key points are: 1) ls lists files and directories, ls -a shows hidden files, and mkdir makes a new directory. 2) cp copies files, mv moves/renames files, and rm removes files. 3) cat displays a file, less displays a file page by page, head shows the first lines, and tail shows the last lines. 4) grep searches files for text, and chmod changes file permissions.

Uploaded by

uvs sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Exercise 1.listing Files and Directories: Command Meaning Ls Ls - A Mkdir CD Directory CD CD CD .

This document provides instructions on basic Linux commands for file management, file permissions, and searching files. It covers commands like ls, mkdir, cp, mv, rm, cat, less, head, tail, grep, and chmod. The key points are: 1) ls lists files and directories, ls -a shows hidden files, and mkdir makes a new directory. 2) cp copies files, mv moves/renames files, and rm removes files. 3) cat displays a file, less displays a file page by page, head shows the first lines, and tail shows the last lines. 4) grep searches files for text, and chmod changes file permissions.

Uploaded by

uvs sahu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Exercise 1.

Listing files and directories

Command Meaning

ls list files and directories

ls -a list all files and directories

mkdir make a directory

cd directory change to named directory

cd change to home-directory

cd ~ change to home-directory

cd .. change to parent directory

pwd display the path of the current directory

Exercise 2. Copying Files, Moving files

Exercise 2a

Create a backup of your science.txt file by copying it to a file called science.bak

Exercise 2b

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

cp (copy)

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

mv file1 file2 moves (or renames) file1 to file2

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 unixstuff directory, type

% cp science.txt 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.

Exercise 3. Displaying the contents of a file on the screen

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 (concatenate)

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

% cat science.txt
As you can see, the file is longer than than the size of the window, so it scrolls past making
it unreadable.

less

The command less writes the contents of a file onto the screen a page at a time. Type

% less science.txt

Press the [space-bar] if you want to see another page, and type [q] if you want to quit
reading. As you can see, less is used in preference to cat for long files.

head

The head command writes the first ten lines of a file to the screen.

First clear the screen then type

% head science.txt

Then type

% head -5 science.txt

What difference did the -5 do to the head command?

tail

The tail command writes the last ten lines of a file to the screen.

Clear the screen and type

% tail science.txt

Q. How can you view the last 15 lines of the file?

Exercise 4. grep Command-

grep is one of many standard UNIX utilities. It searches files for specified words or patterns.
First clear the screen, then type
% grep science science.txt

As you can see, grep has printed out each line containg the word science.

Or has it ????

Try typing

% grep Science science.txt

The grep command is case sensitive; it distinguishes between Science and science.

To ignore upper/lower case distinctions, use the -i option, i.e. type

% grep -i science science.txt

To search for a phrase or pattern, you must enclose it in single quotes (the apostrophe
symbol). For example to search for spinning top, type

% grep -i 'spinning top' science.txt

Some of the other options of grep are:

-v display those lines that do NOT match


-n precede each matching line with the line number
-c print only the total count of matched lines

Try some of them and see the different results. Don't forget, you can use more than one
option at a time. For example, the number of lines without the words science or Science is

% grep -ivc science science.txt

Command Meaning

cp file1 file2 copy file1 and call it file2

mv file1 file2 move or rename file1 to file2

rm file remove a file


rmdir directory remove a directory

cat file display a file

less file display a file a page at a time

head file display the first few lines of a file

tail file display the last few lines of a file

grep 'keyword' file search a file for keywords

wc file count number of lines/words/characters in file

Exercise 5 a.Redirecting the Output

Using the above method, create another file called list2 containing the following fruit:
orange, plum, mango, grapefruit. Read the contents of list2

We use the > symbol to redirect the output of a command. For example, to create a file
called list1 containing a list of fruit, type

% cat > list1

Then type in the names of some fruit. Press [Return] after each one.

pear
banana
apple
^D {this means press [Ctrl] and [d] to stop}

What happens is the cat command reads the standard input (the keyboard) and the >
redirects the output, which normally goes to the screen, into a file called list1

To read the contents of the file, type

% cat list1

Appending to a file
The form >> appends standard output to a file. So to add more items to the file list1, type

% cat >> list1

Then type in the names of more fruit

peach
grape
orange
^D (Control D to stop)

To read the contents of the file, type

% cat list1

You should now have two files. One contains six fruit, the other contains four fruit.

We will now use the cat command to join (concatenate) list1 and list2 into a new file called
biglist. Type

% cat list1 list2 > biglist

Exercise 5b.Using Pipes

Using pipes, display all lines of list1 and list2 containing the letter 'p', and sort the result.

Answer

% cat list1 list2 | grep p | sort

Wildcards

The * wildcard

The character * is called a wildcard, and will match against none or more character(s) in a
file (or directory) name. For example, in your unixstuff directory, type

% ls list*
This will list all files in the current directory starting with list....

Try typing

% ls *list

This will list all files in the current directory ending with ....list

The ? wildcard

The character ? will match exactly one character.

So ?ouse will match files like house and mouse, but not grouse.
Try typing

% ls ?list

Filename conventions

We should note here that a directory is merely a special type of file. So the rules and
conventions for naming files apply also to directories.

In naming files, characters with special meanings such as / * & % , should be avoided.
Also, avoid using spaces within names. The safest way to name a file is to use only
alphanumeric characters, that is, letters and numbers, together with _ (underscore) and .
(dot).

Good filenames Bad filenames

project.txt project

my_big_program.c my big program.c

fred_dave.doc fred & dave.doc

File names conventionally start with a lower-case letter, and may end with a dot followed by
a group of letters indicating the contents of the file. For example, all files consisting of C
code may be named with the ending .c, for example, prog1.c . Then in order to list all files
containing C code in your home directory, you need only type ls *.c in that directory.

* match any number of characters

? match one character

man command read the online manual page for a command

6.1 File system security (access rights)

Exercise 6a

Try changing access permissions on the file science.txt and on the directory backups

Use ls -l to check that the permissions have changed.

In your unixstuff directory, type

% ls -l (l for long listing!)

You will see that you now get lots of details about the contents of your directory, similar to
the example below.

Each file (and directory) has associated access rights, which may be found by typing ls -l.
Also, ls -lg gives additional information as to which group owns the file (beng95 in the
following example):
-rwxrw-r-- 1 ee51ab beng95 2450 Sept29 11:52 file1

In the left-hand column is a 10 symbol string consisting of the symbols d, r, w, x, -, and,


occasionally, s or S. If d is present, it will be at the left hand end of the string, and indicates
a directory: otherwise - will be the starting symbol of the string.

The 9 remaining symbols indicate the permissions, or access rights, and are taken as three
groups of 3.

 The left group of 3 gives the file permissions for the user that owns the file (or directory)
(ee51ab in the above example);
 the middle group gives the permissions for the group of people to whom the file (or directory)
belongs (eebeng95 in the above example);
 the rightmost group gives the permissions for all others.

The symbols r, w, etc., have slightly different meanings depending on whether they refer to
a simple file or to a directory.

Access rights on files.

 r (or -), indicates read permission (or otherwise), that is, the presence or absence of
permission to read and copy the file
 w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to
change a file
 x (or -), indicates execution permission (or otherwise), that is, the permission to execute a
file, where appropriate

Access rights on directories.

 r allows users to list files in the directory;


 w means that users may delete files from the directory or move files into it;
 x means the right to access files in the directory. This implies that you may read files in the
directory provided you have read permission on the individual files.

So, in order to read a file, you must have execute permission on the directory containing
that file, and hence on any directory containing that directory as a subdirectory, and so on,
up the tree.

Some examples
-rwxrwxrwx a file that everyone can read, write and execute (and delete).

a file that only the owner can read and write - no-one else

-rw------- can read or write and no-one has execution rights (e.g. your

mailbox file).

6.2 Changing access rights

chmod (changing a file mode)

Only the owner of a file can use chmod to change the permissions of a file. The options of
chmod are as follows

Symbol Meaning

u user

g group

o other

a all

r read

w write (and delete)

x execute (and access directory)

+ add permission

- take away permission

For example, to remove read write and execute permissions on the file biglist for the group
and others, type

% chmod go-rwx biglist

This will leave the other permissions unaffected.


To give read and write permissions on the file biglist to all,

% chmod a+rw biglist

You might also like