Working with Files Text Editor

 

Working with Files

Text Editor

  • A text editor is used for editing text files.

  • Various text editors are:

    • Notepad++
    • Sublime Text
    • gEdit
    • Visual Studio Code etc..

Nano

Nano is an easy to use command line text editor for Unix and Linux-based operating systems.

Open file

To open a file with nano, pass the filename as an argument.

nano filename

Updating File

Add the text of the file in the middle of the editor.

Saving File

To save a file,

PRESS Ctrl+O
and
Enter()
.

Exit Nano

To exit from nano editor,

PRESS Ctrl+X
.

Viewing File Contents

To view file Contents:

cat filename

Filtering & Output Redirection

Filtering

We can filter the contents of a file using the following filter commands.

  • head

  • tail

  • grep

  • Used to print top N lines of a file.

  • By default, it will print the first 10 lines.

Syntax:

head [-N] filename

Example:

head -2 sentences.txt

tail

  • Used to print last N lines of a file.

  • By default it will print the last 10 lines.

Syntax:

tail [-N] filename

Example:

tail -2 sentences.txt

Counting

Word Count

wc
is used to find out number of lines, word count and characters count in the files.

Piping

  • Pipe is used to combine two or more commands

  • Output of one command is passed as an input to the command followed and so on.

Using ‘|’

Syntax:

command_1 | command_2 | command N

Example:

cat sentences.txt | head -2

Grep

Searches a file or files for lines that have a certain pattern.

Syntax:

cat filename | grep <pattern>

Example:

cat sentences.txt | grep "morning"

Example 1

Number of lines that contain the word morning in the given file.

Example 2

Occurrences of the word "morning" in the given file from the lines 10 to 15

Output Redirection

">" takes the standard output of the command and redirects it to the file.

Syntax:

command > filename

Example:

cat sentences.txt | head -2 > learnings.txt

Compressing & Uncompressing Files

  • File compression is a reduction in the number of bits needed to store the data of a file.

  • Files are stored in such a way that, it uses less disk space than all the individual files and directories combined
  • Advantages of compressing files are:
    • taking less disk space

    • easier and faster transmission
  • Commonly used file formats for the compressed files:

    • gzip

    • zip

    • tar

tar

We can use tar to compress files & directories

Compression

Syntax:

tar -czvf file-name.tar.gz path1 path2 ..

Example:

tar -czvf my_collection.tar.gz videos report.txt

Extract/ Uncompress

Syntax:

tar -xzvf filename.tar.gz -C path

Example:

tar -xzvf my_collection.tar.gz -C collections

zip

It is used to package all the files into one file with .zip extension

Syntax:

zip -r zipfile.zip file1 folder1 file2 ...

Example:

zip -r collections.zip videos report.txt

unzip

The unzip command extracts all files from the specified ZIP archive

Syntax:

unzip filename.zip -d path

Example:

unzip collections.zip -d new-folder

Summary

CommandDescription
headUsed to print top N lines of a file
tailUsed to print last N lines of a file
wcUsed to find out number of lines, word count and characters count in the files
grepSearches a file or files for lines that have a certain pattern
tarUsed to compress files & directories
zipUsed to package all the files into one file with .zip extension
unzipIt extracts all files from the specified ZIP archive

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form