Sitemap

🐧Linux Commands — Working with Files (English & Sinhala)

9 min read3 days ago

--

Part 17 — Linux Command Line

Press enter or click to view image in full size

Working with Files: An Overview

To manage our data effectively, it is important to understand how files and directories work in Linux. In our previous lessons, we learned how to interact with the Linux system using the command line. Now, we are going to explore another important part of Linux: working with files.

In Linux, we can use the command line to view file contents, create new files, update existing files, rename files and directories, move data between locations, and remove files that are no longer needed.

These operations are essential when working with Linux servers, managing application configurations, handling log files, and performing everyday system administration tasks.

Before we start exploring individual commands, let’s understand the basic operations we can perform on files.

Viewing Files

One of the most common tasks when working with Linux is viewing the contents of a file.

For example, imagine that we have a file named notes.txt. We may want to read its contents, check the latest entries, or search for specific information.

Linux provides several commands for viewing files. Although these commands may appear similar, each one is designed for a particular purpose.

1. The cat Command

The cat command is commonly used to display the contents of a file directly in the terminal.

For example, if we have a file named notes.txt, we can display its contents using:

cat notes.txt

The output may look like this:

Welcome to Linux.
We are learning file operations.
The command line is powerful.

The cat command displays the entire file without providing interactive scrolling.

Therefore, it is particularly useful when working with short files. We can also display the contents of multiple files:

cat notes.txt commands.txt

This command displays the contents of notes.txt, followed by the contents of commands.txt.

2. The tac Command

Have you ever wanted to read a file starting from the last line instead of the first?

The tac command allows us to display the contents of a file in reverse line order.

For example:

tac notes.txt

Output:

The command line is powerful.
We are learning file operations.
Welcome to Linux.

Notice that the contents of the file have not changed. Only the order in which the lines are displayed has changed.

This can be useful when examining files where the most recent information appears at the bottom.

3. The less Command

What happens when we need to read a file containing hundreds or thousands of lines?

Using cat may not be convenient because the entire file is printed directly into the terminal. We may lose the ability to easily navigate through the content.

This is where the less command becomes useful.

The less command is a paging program that allows us to view a file one screen at a time.

For example:

less notes.txt

When the file opens, we can navigate through its contents using the keyboard.

Space — Move forward one screen

b-Move backward one screen

Enter-Move forward one line

↑ / ↓ -Move up or down

g-Go to the beginning of the file

G-Go to the end of the file

q-Quit the viewer

We can also search for specific patterns within the file.

To search forward:

/ Linux

To search backward:

? Linux

After entering a search pattern, press Enter. We can use n to move to the next matching result and N to move to the previous matching result.

An older command named more also provides paging functionality. However, less offers more navigation and search capabilities.

This is why we often hear the phrase:

“Less is more.”

4. The tail Command

Sometimes, we do not need to read an entire file. We only want to see the latest information.

For example, Linux log files often contain a large amount of information, and the most recent entries are usually added at the end.

The tail command displays the last 10 lines of a file by default.

tail notes.txt

If we want to display the last 15 lines instead, we can use:

tail -n 15 notes.txt

We can also use the shorter form:

tail -15 notes.txt

Both commands display the last 15 lines of the file.

Another useful feature of tail is its ability to monitor a file as new content is added.

For example:

tail -f application.log

This command allows us to continuously observe new entries added to the log file.

This is particularly useful when troubleshooting applications or monitoring services on Linux servers.

To stop monitoring, press Ctrl + C.

5. The head Command

The head command performs the opposite operation of tail.

Instead of displaying the last lines, it displays the first 10 lines of a file by default.

For example:

head notes.txt

If we want to display only the first 5 lines:

head -n 5 notes.txt

This can be useful when checking the beginning of a configuration file, reading the header of a dataset, or quickly understanding what a file contains.

Connecting These Commands Together

Now that we understand these commands individually, let’s imagine that we are working with a Linux server and need to inspect an application log file.

We can use different commands depending on what we want to investigate.

Display the entire file— cat application.log

Display the file in reverse line order— tac application.log

Read the file interactively— less application.log

Display the first 10 lines— head application.log

Display the last 10 lines— tail application.log

Display the last 20 lines— tail -n 20 application.log

Monitor new log entries— tail -f application.log

Notice that these commands do not perform the same operation. Each command provides a different way of accessing the information stored in a file.

Understanding when to use each command is more important than simply memorizing its syntax.

The Most Important Lesson

In this lesson, we explored five fundamental Linux commands for viewing files: cat, tac, less, tail, and head.

We learned that:

  • cat displays the contents of a file.
  • tac displays the lines in reverse order.
  • less provides interactive navigation through a file.
  • tail displays the last lines of a file.
  • head displays the first lines of a file.

These commands are especially useful when working with configuration files, application logs, datasets, and other text-based information.

The goal is not simply to learn which command to type. It is to understand how these commands help us interact with and manage the Linux file system effectively.

Sinhala Translation

Files සමඟ වැඩ කිරීම: හැඳින්වීම

අපේ data නිවැරදිව සහ කාර්යක්ෂමව manage කරගැනීමට Linux වල files සහ directories ක්‍රියා කරන ආකාරය තේරුම්ගැනීම වැදගත් වෙනවා.

Get Sandali Sithumani’s stories in your inbox

Join Medium for free to get updates from this writer.

අපි කලින් පාඩම්වලදී Linux system එක සමඟ command line එක හරහා interact කරන ආකාරය ඉගෙනගත්තා. දැන් අපි Linux වල තවත් වැදගත් කොටසක් වන files සමඟ වැඩ කිරීම ගැන ඉගෙනගමු.

Linux වලදී command line එක භාවිතා කරලා file එකක contents බලන්න, අලුත් files create කරන්න, පවතින files update කරන්න, files සහ directories rename කරන්න, data වෙනත් locations වලට move කරන්න සහ අවශ්‍ය නොවන files remove කරන්න පුළුවන්.

මේ operations Linux servers manage කරන විට, application configuration files සමඟ වැඩ කරන විට, log files පරීක්ෂා කරන විට සහ සාමාන්‍ය system administration tasks සිදු කරන විට ඉතාමත් ප්‍රයෝජනවත් වෙනවා.

Individual commands ඉගෙනගන්න කලින්, files සමඟ අපිට සිදු කරන්න පුළුවන් මූලික operations ගැන අවබෝධයක් ලබාගමු.

Files වල Contents බලන්නේ කොහොමද?

Linux වල වැඩ කරන විට නිතරම සිදු කරන task එකක් තමයි file එකක contents බලන එක.

උදාහරණයක් විදිහට, අපිට notes.txt කියලා file එකක් තියෙනවා කියලා හිතමු. අපිට ඒ file එකේ contents කියවන්න, අලුත් entries බලන්න හෝ විශේෂ information එකක් හොයාගන්න අවශ්‍ය වෙන්න පුළුවන්.

Linux වල files view කරන්න commands කිහිපයක් තිබෙනවා. මේ commands එකිනෙකට සමාන ලෙස පෙනුණත්, ඒවා භාවිතා කරන අවස්ථා වෙනස් වෙනවා.

1. cat Command එක

cat command එක භාවිතා කරන්නේ file එකක contents කෙළින්ම terminal එකේ display කරගැනීමටයි.

උදාහරණයක් ලෙස, notes.txt කියන file එකේ contents බලන්න:

cat notes.txt

Output එක මෙවැනි ආකාරයකට පෙනෙන්න පුළුවන්:

Welcome to Linux.
We are learning file operations.
The command line is powerful.

cat command එක file එකේ සම්පූර්ණ contents එක terminal එකේ display කරනවා.

නමුත් මෙහිදී අපිට file එක ඇතුළේ scroll කරමින් contents navigate කරන්න interactive පහසුකමක් ලැබෙන්නේ නැහැ.

ඒ නිසා කෙටි files වල contents බලන්න cat command එක ඉතාමත් ප්‍රයෝජනවත් වෙනවා.

Files කිහිපයක contents එකට display කරන්නත් පුළුවන්:

cat notes.txt commands.txt

මෙහිදී මුලින් notes.txt file එකේ contents සහ ඉන්පසුව commands.txt file එකේ contents display වෙනවා.

2. tac Command එක

File එකක contents පළමු line එකේ සිට කියවනවා වෙනුවට අවසාන line එකේ සිට කියවන්න අවශ්‍ය වුණොත් මොකද කරන්නේ?

ඒ සඳහා අපිට tac command එක භාවිතා කරන්න පුළුවන්.

උදාහරණයක් ලෙස:

tac notes.txt

Output:

The command line is powerful.
We are learning file operations.
Welcome to Linux.

මෙහිදී වැදගත් දෙයක් තමයි file එකේ original contents වෙනස් වෙන්නේ නැති බව.

වෙනස් වෙන්නේ lines display කරන order එක පමණයි.

File එකක අවසානයේ තිබෙන information මුලින් බලන්න අවශ්‍ය අවස්ථාවලදී tac command එක ප්‍රයෝජනවත් වෙනවා.

3. less Command එක

දැන් හිතන්න අපිට lines සිය ගණනක් හෝ දහස් ගණනක් තිබෙන file එකක් කියවන්න අවශ්‍යයි කියලා.

එවැනි අවස්ථාවක cat command එක භාවිතා කිරීම පහසු නොවෙන්න පුළුවන්. මොකද file එකේ සම්පූර්ණ contents එක terminal එකට print වෙන නිසා අවශ්‍ය information එක navigate කරමින් කියවීම අපහසු වෙන්න පුළුවන්.

මේ සඳහා less command එක භාවිතා කරන්න පුළුවන්.

less කියන්නේ file එක screen එකෙන් screen එකට කියවීමට ඉඩ ලබාදෙන paging program එකක්.

උදාහරණයක් ලෙස:

less notes.txt

File එක open වුණාට පස්සේ keyboard එක භාවිතා කරලා contents navigate කරන්න පුළුවන්.

Space — ඊළඟ screen එකට යන්න

b — පෙර screen එකට යන්න

Enter — ඊළඟ line එකට යන්න

↑ / ↓ — ඉහළට හෝ පහළට යන්න

g — File එකේ ආරම්භයට යන්න

G — File එකේ අවසානයට යන්න

q — File viewer එකෙන් exit වෙන්න

File එක ඇතුළේ specific pattern එකක් search කරන්නත් පුළුවන්.

Forward direction එකට search කරන්න:

/ Linux

Backward direction එකට search කරන්න:

? Linux

Search pattern එක type කරලා Enter press කළාට පස්සේ, n භාවිතා කරලා ඊළඟ matching result එකටත් N භාවිතා කරලා පෙර matching result එකටත් යන්න පුළුවන්.

more කියලා පැරණි command එකක්ද paging සඳහා භාවිතා වෙනවා. නමුත් less command එකේ navigation සහ search capabilities වැඩියි.

ඒ නිසා අපිට නිතරම අහන්න ලැබෙන phrase එකක් තමයි:

“Less is more.”

4. tail Command එක

සමහර අවස්ථාවලදී අපිට file එකක සම්පූර්ණ contents කියවන්න අවශ්‍ය වෙන්නේ නැහැ. අපිට අවශ්‍ය වෙන්නේ එහි අලුත්ම information ටික පමණයි.

උදාහරණයක් විදිහට, Linux log files වල විශාල information ප්‍රමාණයක් තිබෙන්න පුළුවන්. අලුත්ම log entries සාමාන්‍යයෙන් file එකේ අවසානයට එකතු වෙනවා.

tail command එක භාවිතා කරන්නේ file එකක අවසාන lines display කිරීමටයි.

By default, tail command එක අවසාන lines 10 display කරනවා.

tail notes.txt

අවසාන lines 15 display කරන්න අවශ්‍ය නම්:

tail -n 15 notes.txt

ඒ වෙනුවට කෙටි syntax එකක් භාවිතා කරන්නත් පුළුවන්:

tail -15 notes.txt

Commands දෙකෙන්ම file එකේ අවසාන lines 15 display වෙනවා.

tail command එකේ තවත් ප්‍රයෝජනවත් feature එකක් තමයි file එකට අලුත් contents එකතු වෙන විට ඒවා continuously monitor කිරීම.

උදාහරණයක් ලෙස:

tail -f application.log

මෙම command එක භාවිතා කළාම log file එකට අලුත් entries එකතු වෙන විට ඒවා terminal එකේ continuously display වෙනවා.

Linux server එකක application එකක් troubleshoot කරන විට හෝ service එකක් monitor කරන විට මේ command එක ඉතාමත් ප්‍රයෝජනවත් වෙනවා.

Monitoring එක නතර කිරීමට:

Ctrl + C

භාවිතා කරන්න පුළුවන්.

5. head Command එක

head command එක tail command එකේ opposite operation එක සිදු කරනවා.

tail command එක file එකේ අවසාන lines display කරන විට, head command එක file එකේ පළමු lines display කරනවා.

By default, head command එක පළමු lines 10 display කරනවා.

උදාහරණයක් ලෙස:

head notes.txt

File එකේ පළමු lines 5 පමණක් display කරන්න අවශ්‍ය නම්:

head -n 5 notes.txt

Configuration file එකක ආරම්භය පරීක්ෂා කිරීමට, dataset එකක header එක කියවීමට හෝ file එකක contents ගැන ඉක්මන් අවබෝධයක් ලබාගැනීමට මෙම command එක ප්‍රයෝජනවත් වෙනවා.

මේ Commands එකට සම්බන්ධ කරගන්නේ කොහොමද?

දැන් අපි මේ commands එකින් එක ඉගෙනගත්තා. අපි Linux server එකක application log file එකක් පරීක්ෂා කරනවා කියලා හිතමු.

අපිට අවශ්‍ය information එක අනුව විවිධ commands භාවිතා කරන්න පුළුවන්.

සම්පූර්ණ file එක display කිරීම — cat application.log

File එක reverse line order එකට display කිරීම — tac application.log

File එක interactive විදිහට කියවීම — less application.log

පළමු lines 10 display කිරීම — head application.log

අවසාන lines 10 display කිරීම — tail application.log

අවසාන lines 20 display කිරීම — tail -n 20 application.log

අලුත් log entries monitor කිරීම — tail -f application.log

මෙහිදී අපිට පේනවා මේ commands සියල්ල එකම operation එක සිදු කරන්නේ නැති බව.

File එකක තිබෙන information access කරගැනීමට සහ කියවීමට විවිධ ක්‍රම ලබාදීම තමයි මේ commands වල ප්‍රධාන අරමුණ.

ඒ නිසා command එකක syntax එක මතක තබාගැනීමට වඩා, අවශ්‍යතාවය අනුව නිවැරදි command එක තෝරාගැනීමට ඉගෙනගැනීම වැදගත් වෙනවා.

මේ පාඩමේ වැදගත්ම Lesson එක

මෙම පාඩමේදී අපි files view කිරීම සඳහා භාවිතා කරන මූලික Linux commands පහක් ඉගෙනගත්තා.

  • cat — File එකක contents display කරනවා.
  • tac — File එකේ lines reverse order එකට display කරනවා.
  • less — File එකක් interactive විදිහට navigate කරමින් කියවීමට ඉඩ ලබාදෙනවා.
  • tail — File එකක අවසාන lines display කරනවා.
  • head — File එකක පළමු lines display කරනවා.

මෙම commands configuration files, application logs, datasets සහ වෙනත් text-based information සමඟ වැඩ කරන විට ඉතාමත් ප්‍රයෝජනවත් වෙනවා.

මෙහි අරමුණ command එකක් type කරන ආකාරය පමණක් ඉගෙනගැනීම නෙමෙයි. Linux file system එක සමඟ කාර්යක්ෂමව interact වෙලා data manage කරගන්න මේ commands උපකාරී වෙන්නේ කොහොමද කියන එක තේරුම්ගැනීමයි.

--

--