Of course. Here is a robust and well-commented CLI script that uses standard Unix/Linux tools to achieve your goal.
This script will:
Accept a filename as an argument.
Check if the file exists.
Use the less command to allow you to scroll through the file.
Use the time command to measure how long you spend viewing the file.
Print the time duration after you quit the viewer.
The Script: scroll_and_time.sh
sh
#!/bin/sh
# -----------------------------------------------------------------------------
# scroll_and_time.sh
#
# A script to view a text file using a pager (less) and report the
# total time spent viewing the file upon exit.
#
# Usage: ./scroll_and_time.sh <path_to_file>
# -----------------------------------------------------------------------------
# --- 1. Validate Input ---
# Check if a filename was provided as an argument.
# If not, print a usage message to standard error and exit.
if [ -z "$1" ]; then
echo "Error: No file specified." >&2
echo "Usage: $0 <path_to_file>" >&2
exit 1
fi
FILENAME="$1"
# Check if the provided file exists and is a regular file.
if [ ! -f "$FILENAME" ]; then
echo "Error: File not found or is not a regular file: '$FILENAME'" >&2
exit 1
fi
# --- 2. Execute and Time the Viewer ---
echo "Starting viewing session for '$FILENAME'..."
echo "Navigate with arrow keys, PageUp/PageDown. Press 'q' to quit."
echo "----------------------------------------------------"
# A short pause to let the user read the instructions.
sleep 1
# Use the 'time' command to measure how long the 'less' command runs.
# The timer starts now and stops when you quit 'less' by pressing 'q'.
#
# - 'less' is a powerful pager that lets you scroll forward and backward.
# - The quotes around "$FILENAME" ensure it works with filenames
# containing spaces or special characters.
time less "$FILENAME"
echo "----------------------------------------------------"
echo "Viewing session for '$FILENAME' has ended."
echo "Total duration is shown above."
How to Use the Script
Save the Code: Copy the code above and save it into a file named scroll_and_time.sh.
Make it Executable: Open your terminal and run the following command to give the script execution permissions.
bash
chmod +x scroll_and_time.sh
Run the Script: Execute the script by passing the path to the text file you want to view as an argument.
Example:
Let's say you have a file named my_log_file.log. You would run:
bash
./scroll_and_time.sh my_log_file.log
How It Works
Shebang (#!/bin/sh): This line tells the system to execute the script using the POSIX shell (sh), which makes it highly portable across different systems (like Linux and macOS).
Input Validation:
if [ -z "$1" ]: This checks if the first command-line argument ($1) is empty (-z). If you didn't provide a filename, the script prints a helpful usage message and exits.
if [ ! -f "$FILENAME" ]: This checks if the provided argument is not (!) a regular file (-f). This prevents errors if you pass a directory or a non-existent file path.
time less "$FILENAME": This is the core of the script.
less: This is a standard terminal pager program. It displays the content of your file and lets you scroll up and down using your arrow keys, PageUp/PageDown, and other shortcuts. The program keeps running until you press q to quit.
time: This is a shell command that executes another command and, once it completes, reports the time it took to run. It measures the duration from the moment less starts until the moment you press q to exit it.
Understanding the time Output
When the script finishes, time will print something like this:
code
real 0m15.234s
user 0m0.002s
sys 0m0.005s
real: This is the "wall clock" time. It's the total time that passed from start to finish, and it's the duration you are most likely interested in. In this example, you spent 15.2 seconds viewing the file.
user: The amount of CPU time spent in user-mode code (outside the kernel). This will be very low for less since it's mostly waiting for your input.
sys: The amount of CPU time spent inside the kernel. This will also be very low.
User
Similar for Linux Mint.
Assistant
Excellent question! The great news is that the previous script will work perfectly on Linux Mint without any changes.
Linux Mint is a user-friendly distribution of Linux. At its core, it uses the same standard command-line environment (the shell, usually bash) and the same fundamental tools (time, less, echo, etc.) as other Linux systems and even macOS.
So, you can use the exact same script. For your convenience, here it is again, with comments confirming its compatibility.
The Universal Script (Works Perfectly on Linux Mint)
This script uses #!/bin/sh, the most basic and universal shell, ensuring it runs on virtually any Linux system, including Mint.
sh
#!/bin/sh
# -----------------------------------------------------------------------------
# scroll_and_time.sh
#
# This script is fully compatible with Linux Mint, Ubuntu, Debian,
# and most other Linux distributions.
#
# Usage: ./scroll_and_time.sh <path_to_file>
# -----------------------------------------------------------------------------
# --- 1. Validate Input ---
if [ -z "$1" ]; then
echo "Error: No file specified." >&2
echo "Usage: $0 <path_to_file>" >&2
exit 1
fi
FILENAME="$1"
if [ ! -f "$FILENAME" ]; then
echo "Error: File not found: '$FILENAME'" >&2
exit 1
fi
# --- 2. Execute and Time the Viewer ---
echo "Starting viewing session for '$FILENAME'..."
echo "Navigate with arrow keys. Press 'q' to quit."
echo "----------------------------------------------------"
sleep 1
# The 'time' command is a standard utility in Linux Mint.
# It will measure the execution time of the 'less' command.
time less "$FILENAME"
echo "----------------------------------------------------"
echo "Viewing session for '$FILENAME' has ended."
echo "Total duration is shown above as 'real' time."
How to Use on Linux Mint
The steps are identical:
Save the Code: Open your text editor in Linux Mint (like xed), paste the code, and save it as scroll_and_time.sh.
Make it Executable: Open your Terminal, navigate to where you saved the file, and run:
bash
chmod +x scroll_and_time.sh
Run it:
bash
./scroll_and_time.sh /var/log/syslog
(Here, /var/log/syslog is just an example of a text file that exists on your system).
Enhanced Version for Linux (Cleaner Output)
While the script above works perfectly, we can make a small improvement that takes advantage of the more advanced GNU time utility available on Linux systems like Mint. This version will give you a much cleaner, single-line output for the duration.
This enhanced script uses /usr/bin/time to print only the elapsed "wall clock" time in a human-readable format.
sh
#!/bin/bash
# -----------------------------------------------------------------------------
# scroll_and_time_enhanced.sh
#
# An enhanced version for Linux systems like Mint that provides
# cleaner, formatted time output.
#
# It uses bash and the GNU 'time' utility.
# -----------------------------------------------------------------------------
# --- 1. Validate Input ---
if [[ -z "$1" ]]; then
echo "Error: No file specified." >&2
echo "Usage: $0 <path_to_file>" >&2
exit 1
fi
FILENAME="$1"
if [[ ! -f "$FILENAME" ]]; then
echo "Error: File not found: '$FILENAME'" >&2
exit 1
fi
# --- 2. Execute and Time the Viewer ---
echo "Starting viewing session for '$FILENAME'..."
echo "Navigate with arrow keys. Press 'q' to quit."
echo "----------------------------------------------------"
sleep 1
# Use the GNU time command for formatted output.
# /usr/bin/time - Ensures we use the GNU version, not the shell built-in.
# -f "..." - Specifies the output format.
# %E - A placeholder for the elapsed real time (wall clock).
# The output will be a clean, single line like "Total viewing time: 0:15.23".
/usr/bin/time -f "\nTotal viewing time: %E" less "$FILENAME"
echo "----------------------------------------------------"
echo "Session ended."
Why this enhanced version is nice:
Instead of seeing this:
code
real 0m15.234s
user 0m0.002s
sys 0m0.005s
You will see just this clean output after you quit less:
code
Total viewing time: 0:15.23
----------------------------------------------------
Session ended.
Both scripts achieve the same core goal, but the enhanced version is a bit more user-friendly for a modern Linux desktop like Mint.