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

BLP Unit 3.2

Uploaded by

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

BLP Unit 3.2

Uploaded by

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

K. K. Wagh Polytechnic, Nashik.

Program: Information Technology (IF)


Semester: II Scheme: K
Course: Linux Basics Course Code: 312001
Unit-3: File Management in Linux
Topic: File and Directory commands
Presented by:
Mrs. M. P. Nawarkar
Lecturer in Information Technology
File and Directory commands
Contents
Course Outcome:
CO-3: Manage files and directories in Linux operating
system.

Theory Learning Outcomes:


TLO3.2 Use absolute and relative pathnames.
TLO3.3 Execute file and directory commands.

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


File and Directory commands
Contents
Contents:
- Absolute pathname
- Relative pathname
- File commands
- Directory commands

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Absolute and Relative Pathname

- A path is a unique location to a file or a folder in a file system of an OS.


- A path to a file is a combination of / and alpha-numeric characters.
- Types of path: Absolute Pathname and Relative pathname
i) Absolute Pathname:
- An absolute path is defined as the specifying the location of a file or directory from the root
directory(/).
- In other words, we can say that an absolute path is a complete path from start of actual
file system from / directory.
- To write an absolute path-name:
- Start at the root directory ( / ) and work down.
- Write a slash ( / ) after every directory name
- For Example :
- i) $cat abc.txt - if the fie “abc.txt” exists in current directory.
- ii) $cat /home/kt/abc.txt - if this file is not present in working directory and is present
somewhere else like in /home/kt

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Absolute and Relative Pathname

Relative pathname

- Relative path is defined as the path related to the present working directory (pwd).
- It starts from current directory .
- Relative paths do not start with the root directory i.e. /
- These are usually shorter than absolute paths.
- For example, if you are currently in the home directory of "BLP" and want to access a file
named "example. txt" in a subdirectory called "documents", the relative path would be
"documents/example.txt".

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


File and Directory commands
Cat: Displaying and creating files

- The cat command in Linux is a versatile companion for various file-related operations,
allowing users to view, concatenate, create, copy, merge, and manipulate file contents.
- Syntax:
- cat [OPTION] [FILE]
Here,
- [OPTION] : represents various command-line options.
- [FILE] : the name of the file(s) to be processed.

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


File and Directory commands
Practical examples of cat command
1. How to Create a file and add content in Linux Using `cat` Command
- If we want to create a new file or overwrite an existing file with new content, use ‘cat’ with
the output redirection (`>`) operator
- Syntax: cat > newfile_name
- Example: cat > new_file.txt
- This will allow to type text directly into the terminal, and when you press Ctrl + D, the
entered text will be saved to new_file.txt

2. How to View the Content of a Single File in Linux


- The most basic use of ‘cat’ is to display the contents of a file on the terminal.
- This can be achieved by simply providing the filename as an argument:
- Syntax: cat file_name
- Example: cat new_file.txt
Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik
File and Directory commands
Practical examples of cat command
3. How to View the Content of Multiple Files in Linux
- Syntax: cat file_name1 file_name2
- Example: cat file1 file2

4. How to View Contents of a File preceding with Line Numbers in Linux


- Adding the -n option to cat introduces line numbers.
- Syntax: cat -n file_name
- Example: cat -n file2

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


File and Directory commands
Practical examples of cat command
5. How to Copy the Contents of One File to Another File in Linux
- ‘cat’ command can concatenate multiple files into a single file.
- It can be done using redirection (>) operator.
- Syntax: cat file1.txt file2.txt > merged_file.txt
- This command combines the content of file1.txt and file2.txt into a new file named
merged_file.txt.
6. How to Append the Contents of One File to the End of Another File
- If we want to add the content of one file to another, ‘cat’ can be used along with the
append (>>) operator:
- Syntax: cat file_name1 >> file_name2
- Example: cat file1 >> file2
- This will append the content of `file1` to the end of `file2`

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


File and Directory commands
Practical examples of cat command

7. How to Display Content in Reverse Order Using `tac` Command in Linux


- The ‘tac’ command is the reverse of ‘cat’ and is used to display the content of a file in
reverse order.
- Syntax: tac file_name
- Example: tac file2
- This command will print the content of ‘file2’ in reverse order, displaying the last line first,
followed by the second-to-last line, and so on.

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


cp: Copying file
- cp command is used for copying files from one location to another.
- Syntax : cp source_file destination
- This command creates a copy of the `source_file` at the specified `destination`.
- If the destination is a directory, the file is copied into that directory.
1. Copying Between Two Files in Linux:
- If the `cp` command contains two file names, it copies the contents of the first file to the
second file.
- If the second file doesn’t exist, it is created, and the content is copied into it.
- However, if the second file already exists, it is overwritten without warning.
- cp Src_file Dest_file
- If `Dest_file` does not exist, it is created.
- If `Dest_file` already exists, it is overwritten without any warning.
- Example 1: cp a.txt b.txt
- Initially, there is only one file (`a.txt`) in the directory.
- The `cp` command is used to copy the contents of `a.txt` to `b.txt`.
- After the command execution, both `a.txt` and the newly created `b.txt` coexist in the
directory. Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik
cp: Copying file

2. Copy files to a Directory in Linux


- When the cp command has one or more source file arguments and is followed by a
destination directory argument, it copies each source file to the destination directory with
the same name.
- Syntax: cp Src_file1 Src_file2 Src_file3 Dest_directory
- Example:
- Suppose we have to copy three files name “a.txt“, “b.txt” and “c.txt” to a directory name
“new”
- cp a.txt b.txt c.txt new/
- `ls` command can be used to display all the file in the “new” directory to confirm the
successful copy of file in that directory.

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


cp: Copying file

3. Copy a File in Linux Using `*` Option


- The star wildcard represents anything i.e., all files and directories.
- Suppose we have many texts documents in a directory and want to copy it to another
directory, it takes lots of time if we copy files 1 by 1 or command becomes too long if
specify all these file names as the argument, but by using * wildcard it becomes simple.
- Syntax: cp *.txt [Destination Directory or file]
- Example: cp *.txt Folder1

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


rm: deleting file

- rm stands for remove.


- rm command is used to remove files.
- Syntax: rm [OPTION]... FILE...
- For e.g. Let us consider 5 files having name a.txt, b.txt and so on till e.txt.
- $ ls
- a.txt b.txt c.txt d.txt e.txt
- Removing one file at a time : $ rm a.txt
- $ ls
- b.txt c.txt d.txt e.txt
- Removing more than one file at a time: $ rm b.txt c.txt
- $ ls
- d.txt e.txt

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


mv: renaming or moving file

- mv` stands for “move”.


- This command is used to rename file directories and move files from one location to
another within a file system.
- Two Distinct Functions of `mv` Command
1) Renaming a file or directory.
2) Moving a file or directory to another location
- Syntax:
mv [options(s)] [source_file_name(s)] [Destination_file_name]
Here,
source_file_name(s) = The name of the files that we want to rename or move.
Destination_file_name = The name of the new location or the name of the file.

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Examples of mv command

1. Rename a file in Linux Using `mv` Command


- Syntax:
mv [source_file_name] [Destination_file_name]
- For e.g. mv a1.txt a2.txt
2. Move a File in Linux Using `mv` Command
- Syntax: mv [source_file_name] [Destination_path]
- For e.g.
i) mv a1.txt blp1/
ii) mv a1.txt b1.txt blp1/

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


More: paging output

- more command is used to view the text files in the command prompt, displaying one
screen at a time in case the file is large.
- The more command also allows the user do scroll up and down through the page.
- When the output is large, we can use more command to see output one by one.
- Syntax:
more [file_name]

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Lp: printing a file

- lp stands for ‘Line printer’ which allows to print the files through the terminal.
- This command is also known as the printer management command in Linux.

- Syntax:
lp [options][filename]
- For e.g. lp –d abc.txt
- -d option is used to print the file using the default printer.

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Touch : create an empty file

- It is used to create a file without any content.


- The file created using the touch command is empty.
- This command can be used when the user doesn’t have data to store at the time of file
creation.
- Syntax:
touch [options] file_name
- The file which is created can be viewed by ls command.

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


File : knowing the file types

- File command is used to determine the type of a file.


- File type may be of human-readable(e.g. ‘ASCII text’) or MIME type(e.g. ‘text/plain;
charset=us-ascii’).
- Syntax:
file [option] [filename]
- Example:
i) File abc
Output: abc: ASCII text
ii) File * - (displays the all files’s file type.)
iii) File [a-f] * - (display the file type of files in specific range)

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Wc : counting lines

- wc stands for word count. It is mainly used for counting purpose.


- It is used to find out number of lines, word count, byte and characters count in the files
specified in the file arguments.
- By default it displays four-columnar output.
- First column shows number of lines present in a file specified.
- Second column shows number of words present in the file.
- Third column shows number of characters present in file.
- Fourth column itself is the file name which are given as argument.
- Syntax: wc [Option] [Filename]

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Wc : counting lines - example

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Wc : counting lines - options

1. Wc -l: This option prints the number of lines present in a file. With this option wc
command displays two-columnar output, 1st column shows number of lines present in a
file and 2nd itself represent the file name.
2. Wc – w: This option prints the number of words present in a file. With this option wc
command displays two-columnar output, 1st column shows number of words present in a
file and 2nd is the file name.
3. Wc -c: This option displays count of bytes present in a file. With this option it display
two-columnar output, 1st column shows number of bytes present in a file and 2nd is the
file name.
4. Wc -m: Using -m option ‘wc’ command displays count of characters from a file.
5. Wc -L: It can be used to print out the length of longest (number of characters) line in a file.

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Cmp: Comparing 2 files

- cmp command is used to compare the two files byte by byte and helps you to find out
whether the two files are identical or not.
- When cmp is used for comparison between two files, it reports the location of the first
mismatch to the screen if difference is found and if no difference is found i.e the files
compared are identical.
- cmp displays no message and simply returns the prompt if the files compared are identical.
- Syntax: cmp [option] File1 File2

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Cmp: Comparing 2 files - example

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Comm: what is common

- comm command compares two sorted files line by line and write to standard output.
- It writes the lines that are common and the lines that are unique.
- Syntax: comm [option] File1 File2
- comm produces three-column output where first column contains lines unique to FIile1
,second column contains lines unique to File2 and third column contains lines common to
both the files.
- comm command only works right if you are comparing two files which are already sorted.

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


Comm: what is common - example

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


diff: difference

- diff stands for difference.


- The diff command is used to compare the contents of two files line by line and display the
differences between them.
- The command provides a way to highlight changes, additions, and deletions in a clear and
readable format.
- diff command uses certain special symbols and instructions that are required to make two
files identical.
- It tells the instructions on how to change the first file to make it match the second file.
- Syntax: diff [options] file1 file2

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik


diff: difference

Options in result should be like this:


a - added the text to file
c – changes are made in the file
d – deletion operation is performed
< - Lines from the first file.
> - Lines from the second file.
- - - The three dashes separate the lines of file 1 and file 2.
Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik
Thank you

Presented by: Mrs. M. P. Nawarkar, K. K. Wagh Polytechnic, Nashik

You might also like