0% found this document useful (0 votes)
15 views3 pages

Exp 3

Uploaded by

HODCSE RKCE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Exp 3

Uploaded by

HODCSE RKCE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT-3

Simulate UNIX commands like cp, ls, grep, etc.

1. The cp command:
cp - copy files and directories
This command is used to create a duplicate of a file, a set of files or a
directory The cp command copies both text and binary files
Syntax:
$cp source-file/directory destination-file/directory
Examples:
i) $cp file1 file2
ii) $cp dir1/file1 dir1/file2

2) The ls command:
ls - list directory contents
This command lists the contents in a directory.
Syntax:
$ls
Example:
$ls

3) The grep command:

Grep, short for “global regular expression print”, is a command used for
searching and matching text patterns in files contained in the regular
expressions.

Use the grep command whenever you need to search for specific text
patterns within files. It’s particularly useful for:

 Searching logs for error messages.


 Filtering the output of other commands.
 Finding configuration settings in system files.
 Extracting data from large text files.

To Find Matching Strings With grep

To search for a specific string in a file, use:

Sy: grep “search_string” filename

Ex: grep “error” /var/log/syslog


Searching for a String in More Than One File

To search for a specific string in multiple files, use a wildcard to specify the
files. You can also list multiple files explicitly:

Sy: grep “search_string” file1, file2, file3

Ex: : grep “search_string” /var/log/syslog//var/log/auth.log/

How to Count Number of Occurrences

To count the number of times a pattern appears, use the -c option:

Sy: grep -c "search_string" filename

Ex: grep -c "error" /var/log/syslog

How to Find Files with grep

To search for a pattern in multiple files, you can use wildcards:

Sy:grep "search_string" /path/to/files/*.log

Ex: grep "error" /var/log/*.log

How to Use Invert grep Search

To invert the search and display lines that do not match the pattern, use the
-v option:

Sy: grep -v "search_string" filename

Ex: grep -v "error" /var/log/syslog

Using grep with Regular Expressions (REGEX)

grep supports regular expressions, which allows for more complex


search patterns. For instance, to find lines that start with a pattern,
use the caret ^:

sy:grep "^pattern" filename

ex: grep "^error" /var/log/syslog

You might also like