Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 33

File Management Commands

ls COMMAND

The ls command lists all files in the directory that match the name. If
name is left blank, it will list all of the files in the directory.

Meta Characters:

Meta characters have special meaning in Unix. For example * and ?


are metacharacters. We use * to match 0 or more characters, a
question mark ? matches with single character.

-2- TCS INTERNAL


Creating a File

We can create a file using Various method

Using VI Editor
Touch
cat>TestFile
content

-3- TCS INTERNAL


Commands

Displaying Content of a File

Cat Command

cat command can be used to see the content of a file.Example to


see the content of above created file:

$ cat filename

Counting Words in a File


wc Command

wc command can be used to get a count of the total number of lines,


words, and characters contained in a file.

$ wc filename

-4- TCS INTERNAL


Copying Files

cp command

cp command used to copy a file

$cp file1 file2

This will copy the contents of File 1 to File 2.

Renaming Files
mv Command

mv command used to rename files.

mv file1 file2

This will rename the File.

-5- TCS INTERNAL


Deleting Files

rm Command

rm Command used to remove the files

$rm Filename

You can remove multiple files at a time by using

$ rm filename1 filename2 filename3

-6- TCS INTERNAL


Directory Structure - Diagram

A sample directory structure in UNIX is shown below

-7- TCS INTERNAL


Working with Directory
The directory in which the login was done for any user is called
home directory.

User can go to home directory anytime using the following


command:

$cd ~

~ indicates home directory.

cd Command

cd command is used to change between Directories

cd [directory]

cd.. Command

cd.. Command is used to go back by one directory


-8- TCS INTERNAL
Working with Directories

mkdir command

mkdir commands is used to create directories in UNIX.

$mkdir dirname

$mkdir docs pub (Under Current Directory)

The above command creates two directories at a time with name


docs and pub.

mv command

The mv (move) command can also be used to rename a directory.

The syntax is as follows:

$mv olddir newdir


-9- TCS INTERNAL
Removing Directories

rmdir command

Directories can be deleted using the rmdir command as follows:

$rmdir dirname

You can remove multiple directories at a time as follows:

$rmdir dirname1 dirname2 dirname3

- 10
TCS INTERNAL
-
File Access Permission – Introduction

• File ownership is an important component of UNIX that provides


a secure method for storing files.

• Every file in UNIX has the following attributes:

• Owner permissions: The owner's permissions determine what


actions the owner of the file can perform on the file.

• Group permissions: The group's permissions determine what


actions a user, who is a member of the group that a file belongs
to, can perform on the file.

• Other (world) permissions: The permissions for others indicate


what action all other users can perform on the file.

- 11
TCS INTERNAL
-
File Access Permission – cont…

The permissions are broken into groups of threes, and each position
in the group denotes a specific permission, in this order: read (r),
write (w), execute (x)

1.Read: Grants the capability to read ie. view the contents of the file.

1.Write: Grants the capability to modify, or remove the content of the


file.

1.Execute: User with execute permissions can run a file as a


program

- 12
TCS INTERNAL
-
File Access Permission - Diagram

- 13
TCS INTERNAL
-
Changing Permissions:

chmod command

chmod is the command used to change the permisson of a file.

Permission Details are shown below

- 14
TCS INTERNAL
-
Output Redirection

• The output from a command normally intended for standard


output can be easily diverted to a file instead. This capability is
known as output redirection:

• If the notation > file is appended to any command that normally


writes its output to standard output, the output of that command
will be written to file instead of your terminal:

• $ who > users

• Check following who command which would redirect complete


output of the command in users file.

- 15
TCS INTERNAL
-
Output Redirection cont…

• Append to the Same file

• You can use >> operator to append the output in an existing file
as follows:
• $ echo line 2 >> userss

- 16
TCS INTERNAL
-
Input Redirection

• Input Redirection:

• Just as the output of a command can be redirected to a file, so


can the input of a command be redirected from a file. As the
greater-than character > is used for output redirection, the less-
than character < is used to redirect the input of a command.

• $ wc -l < users

• Unix IO

- 17
TCS INTERNAL
-
Filters

• Filters in Unix are some programs that read some input and
perform a simple transformation on it, and write some
output to the standard output. The inputs taken are not
changed.

• wc – word count (line count, character count)


• grep, egrep – search files using regular expressions
• sort – sorts files by line (lexically or numerically)
• cut – select portions of a line
• uniq – Removes identical adjacent lines
• head, tail – displays first (last) n lines of a file .

- 18
- 18 - TCS INTERNAL
-
sort

• The filter sort reorders the lines of a file in ascending or


descending order.

• -k n : sort on the nth field of the line


• -tchar : use char as the field delimiter
• -n : sort numerically
• -r : reverse order sort
• -u : remove repeated lines
• -m list : merge sorted files in list

- 19
- 19 - TCS INTERNAL
-
Sort Cont…

- 20
- 20 - TCS INTERNAL
-
Sort cont…

- 21
- 21 - TCS INTERNAL
-
uniq

• The filter uniq displays the content of a file, removing all the
duplicate lines from it.

• If 2 files are specified, uniq reads from the first and writes to
the second.

• -u : lists only the lines that are unique


• -d : lists only the lines that are duplicates
• -c : counts the frequency of occurrences

- 22
- 22 - TCS INTERNAL
-
Uniq cont….

- 23
- 23 - TCS INTERNAL
-
cut

• The filter cut is a tool for extracting fields from files.

• -c : cuts vertically specified numbers of character


• -f : cuts vertically specified numbers of fields
• -d : specifies the field separator

$ cat file
learning UNIX
learning VI command
Did you learn Cut command
$ cut -c5 file
n
n
y

- 24
- 24 - TCS INTERNAL
-
cut

• The filter cut is a tool for extracting fields from files.

• -c : cuts vertically specified numbers of character


• -f : cuts vertically specified numbers of fields
• -d : specifies the field separator

$ cat file
learning UNIX
learning VI command
Did you learn Cut command
$ cut -c5 file
n
n
y

- 25
- 25 - TCS INTERNAL
-
cut
$ cut -c5,8 file
ng
ng
y

Above command prints the 5th and 8th character of each line of a file.

$ cut -d' ' -f2,3 file


UNIX
VI command
you learn

Above command prints 2nd and 3rd word of each line considering space as a
delimiter.

TCS INTERNAL
head

• Lists the beginning of a file to stdout.


• The default is 10 lines, but a different number can be specified.
• The command has a number of interesting options.

Syntax

• head -n filename
tail


Lists the (tail) end of a file to stdout.

The default is 10 lines, but this can be changed with the -n option

Syntax
tail -n filename

- 27
- 27 - TCS INTERNAL
-
grep
• A multi-purpose file search tool that uses Regular
Expressions.

• g-re-p : global - regular expression – print.

• grep command allows you to search one file or multiple


files for lines that contain a pattern

• grep works as a filter on stdout, as in a pipe. if no target


file(s) specified

Syntax

• grep pattern [file...]

- 28
- 28 - TCS INTERNAL
-
grep cont…

grep -i - Case in sensitive search using grep

grep -w - Checks for full words, not for sub-strings

grep -c - Counts the number of matches using grep

grep -l - The -l option is used to display only the file names


which matched the given pattern.

- 29
- 29 - TCS INTERNAL
-
pipe (|)

Pipe is
• A method of inter process communication (IPC).

• In shell the symbol '|' is used to represent pipe.

• Through pipe the output of one program in sent as an input


to another program .

For example

$ who | wc –l

- 30
- 30 - TCS INTERNAL
-
AWK

~$ awk 'pattern {action}' input-file

~$cat awk_file
Name,Marks,Max_Marks
Peter,200,1000
Sam,500,1000
Greg,1000
Abharam,800,1000
Henry,600,1000
Peter,400,1000
Example: Default behavior of awk
Print all the lines from a file. By default, awk prints all
lines of a file, so to print every line of above
created file use below command:
~$ awk '{print}' awk_file

TCS INTERNAL
~$ awk -F”,” {print $2,$3;}' awk_file
~$ awk '/Henry|Peter/' awk_file
BEGIN block Syntax

awk ‘BEGIN{awk initializing code}{actual AWK code}’ filename.txt


Example:
We can use even BEGIN block to initialize user defined variables in it.
Finding total marks of a student from below file

1|Latha|Third|Vikas|90|91
2|Neethu|Second|Meridian|92|94
3|Sethu|First|DAV|86|98
4|Theekshana|Second|DAV|97|86
5|Teju|First|Sangamithra|89|88
6|Theekshitha|Second|Sangamithra|99|100

~$ awk -F"|" 'BEGIN{s=0}{s=$5+$6;print s}' studentData


Output:
181
186
184
183
177
199

TCS INTERNAL
awk -F"|" 'BEGIN{s=0}{s=$5+$6;print $2,s}' studentData|sort -k2 -nr|head -3

END block which is executed after executing all the AWK code. This is the
last AWK code
going to execute.

Example:
Print some meaning full info after processing AWK code.
~$ awk -F"|" 'BEGIN{s=0}{s=$5+$6;print $2,s} END {print
"############################"}' studentData

Output:

Latha 181
Neethu 186
Sethu 184
Theekshana 183
Teju 177
Theekshitha 199
############################

TCS INTERNAL
THANK YOU

TCS INTERNAL

You might also like