Udgivet i

Search text files with grep

How to use the grep tool

The grep tool is built into the operating system, so you don’t need to install it.

The simplest syntax to find a string in a single file using the grep command is:
grep ‘searched-word’ filename

By default, the search with grep is case sensitive.

Let’s gradually add the most useful options to our search.

1. Make the search case-insensitive with the "-i" option:
grep -i 'searched-word' filename
2. Search recursively in all files in a given directory with the "-r" option:
grep -ir 'searched-word' '/directory'
3. Search whole words only with the "-w" option:
grep -irw 'searched-word' '/directory'
4. Print the line numbers in which the searched word was found with the "-n" option:
grep -irwn 'searched-word' '/directory'
5. Search for multiple words syntax:
grep -ir 'word1|word2|word3' '/directory'

Many other useful grep options can be found in the official Grep Manual.

Udgivet i

Find bad sectors

You can find the bad sector by running a smart long test;

# smartctl -t long /dev/sda

You can then check the status of the long test to see if its finished, and the LBA of the first error it encountered;

# smartctl -l selftest /dev/sda
smartctl 7.1 2019-12-30 r5022 [x86_64-linux-5.7.0-1-amd64] (local build)
Copyright (C) 2002-19, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF READ SMART DATA SECTION ===
SMART Self-test log structure revision number 1
Num  Test_Description    Status                  Remaining  LifeTime(hours)  LBA_of_first_error
# 1  Extended offline    Completed without error       00%     17711         12345
# 2  Short offline       Completed without error       00%     17709         -
# 3  Short captive       Interrupted (host reset)      10%       450         -
# 4  Short captive       Interrupted (host reset)      10%       228         -

You can then overwrite the bad sector using dd;

# dd if=/dev/zero of=/dev/sda seek=12345 count=1