Modifying Network Configuration and Text Processing in Linux

Modifying Network Interface Configuraton

1. Update the Network Configuration File Edit the configuraton file for the target interface (e.g., ens33).

vim /etc/sysconfig/network-scripts/ifcfg-ens33

Adjust the following parameters:

NAME=enp0    # Corresponds to net.ifnames=0
DEVICE=enp0   # Corresponds to biosdevname=0

2. Rename the Configuration File

mv ifcfg-ens33 ifcfg-enp0

3. Adjust Kernel Naming Rules Edit the GRUB configuration file /etc/default/grub. Add the parameters net.ifnames=0 biosdevname=0 to the GRUB_CMDLINE_LINUX variable.

4. Regenerate the GRUB Configuration

grub2-mkconfig -o /boot/grub2/grub.cfg

Using Commands for Direct File Editing

Echo Command Applications Generate a sequence of numbers:

echo {1..10}

Append a single line to a file:

echo "newuser" >> /data/userlist.txt

Insert multiple lines into a file using a heredoc:

cat >> /data/userlist.txt <<END
newuser01
newuser02
newuser03
END

Filtering and Processing Text Content

Primary Text Processing Tools (The Three Musketeers)

  • grep: Primary tool for filtering and searching text.
  • sed: Stream editor for performing text transformations on lines.
  • awk: Programming language designed for pattern scanning and processing, excels at column-based operations.

Create a sample text file for demonstrations:

cat >> /data/sample.txt <<END
user01
admin01
client01
user02
admin02
client02
END

Example Filtering Tasks Using grep

Task 01: Find all lines containing "user"

grep "user" /data/sample.txt

Task 02: Show lines containing "admin" along with the preceding line

grep -B 1 "admin" /data/sample.txt

Task 03: Show lines containing "admin" along with the following line

grep -A 1 "admin" /data/sample.txt

Task 04: Show lines containing "admin" with one line of context before and after

grep -C 1 "admin" /data/sample.txt

Task 05: Count the occurrences of "admin" in the file

grep -c "admin" /data/sample.txt

Using sed for Filtering The p command prints lines, and -n suppresses automatic printing.

sed -n "/user/p" /data/sample.txt

For comparison, the equivalent awk command:

awk "/user/" /data/sample.txt

Modifying File Content with sed Replace all occurrences of "user" with "developer" in the file.

sed 's#user#developer#g' /data/sample.txt

To make the changes permanent in the file, use the -i (in-place) flag:

sed -i 's#user#developer#g' /data/sample.txt

Character Translation with the tr Command

The tr command performs character-by-character substitution.

Scenario 1: Replacement string is shorter than the target string.

echo wxyz | tr "wxyz" "123"

Scenario 2: Replacement string is longer than the target string.

echo wxyz | tr "wxy" "1234"

Scenario 3: Replacement string and target string are of equal length.

echo wxyz | tr "wxyz" "1234"

Understanding Linux File Attributes

Display detailed file information:

ls -li /etc/

Key attributes include:

  1. inode Number: Index node, a unique identifier pointing to the file's data on disk.
  2. File Type: Indicates the object type (e.g., regular file, directory, symbolic link).
  3. Permission Bits: Control read (r), write (w), and execute (x) access for the file's owner, group, and others.
  4. Hard Link Count: Number of directory entries referencing this inode.
  5. File Owner (User): The user who owns the file.
  6. File Group: The primary group asssociated with the file.
  7. File Size: Size in bytes.
  8. Timestamps: Access, modification, and change times.

Common Linux File Types

  • d: Directory.
  • -: Regular file.
    • Plain text files (e.g., /etc/hosts).
    • Binary executables (e.g., /bin/ls).
    • Data files (e.g., archives).
  • l: Symbolic link (soft link). Created with:
    ln -s /source/file.txt /path/to/link.txt
    
  • c / b: Character or block device files (e.g., /dev/sda, /dev/urandom).
  • p: Named pipe (FIFO).
  • s: Socket file.

Supplementary Command Utilities

Locating Command Executables Show the absolute path of a command:

which cat

Show command path, source, and manual page locations:

whereis cat

Fast File Path Lookup with locate The locate command searches a pre-built database for file paths.

locate filename

Update the search database:

updatedb

Note: The mlocate package may need to be installed.

yum install -y mlocate

Searching for Files with find Basic syntax:

find /path/to/search -type filetype -name "filename"

Find a specific configuration file:

find /etc -type f -name "ifcfg-enp0"

Find files with a partial name match using wildcards:

find /etc -type f -name "*enp0"

Tags: Linux Network Configuration sed grep tr

Posted on Mon, 11 May 2026 09:45:57 +0000 by yogadt