Using more with Other Commands
The more
command becomes even more powerful when combined with other Linux commands. In this step, you will learn how to use more
with commands like cat
, grep
, and others through pipes.
First, let's create a log file with various types of entries:
cat > ~/project/system.log << EOF
[2023-05-01 08:00:12] INFO: System startup completed
[2023-05-01 08:15:45] WARNING: High CPU usage detected (85%)
[2023-05-01 08:30:22] INFO: Backup process started
[2023-05-01 08:45:18] ERROR: Backup failed - insufficient disk space
[2023-05-01 09:00:33] INFO: Disk cleanup initiated
[2023-05-01 09:10:56] INFO: 2GB of temporary files removed
[2023-05-01 09:15:27] WARNING: Memory usage high (75%)
[2023-05-01 09:30:45] INFO: System update available
[2023-05-01 09:45:12] INFO: Update download started
[2023-05-01 10:00:39] ERROR: Update installation failed - connection lost
[2023-05-01 10:15:22] INFO: Retry update installation
[2023-05-01 10:30:08] INFO: Update completed successfully
[2023-05-01 10:45:51] WARNING: Network latency issues detected
[2023-05-01 11:00:14] INFO: System scan started
[2023-05-01 11:15:33] INFO: No malware detected
[2023-05-01 11:30:47] INFO: User john logged in
[2023-05-01 11:45:09] ERROR: Permission denied for user john to access /admin
[2023-05-01 12:00:25] INFO: User john logged out
EOF
Now, let's explore different ways to combine more
with other commands using pipes. A pipe (|
) takes the output of one command and uses it as the input for another command.
- Filter the log for WARNING and ERROR entries, then view with
more
:
grep -E "WARNING|ERROR" ~/project/system.log | more
This command searches for lines containing either "WARNING" or "ERROR" and then displays the results one page at a time using more
.
- Display the file with line numbers and view with
more
:
cat -n ~/project/system.log | more
The cat -n
command displays the file with line numbers, and then more
allows you to scroll through the output.
- View a specific portion of the file using
head
and more
:
head -n 10 ~/project/system.log | more
This displays only the first 10 lines of the file through more
.
- Start viewing the file from a specific line using the
+
option:
more +5 ~/project/system.log
This opens the file and starts displaying from line 5.
These examples demonstrate how the more
command can be combined with other commands to filter, format, and display text files in various ways. This flexibility makes it a valuable tool for examining and analyzing text data in Linux.