Linux Text Management Commands
Common text management commands in Linux:
cat: View, concatenate, and create filesgrep: Search for text contentecho: Output text contenttail: View the end of a filesed: Stream editor, used for text substitution and modificationvi: Text editor, used for creating and editing files
Detailed Introduction to Text Management Commands
cat(concatenate) command:- Used to view, concatenate, and create files
- Syntax:
cat [options] [file(s)] - Common options:
-n: Display line numbers-E: Display end-of-line characters
- Example: Merge file1.txt and file2.txt into combined_file.txt.
cat file1.txt file2.txt > combined_file.txt
grep(global regular expression print) command:- Used to search for text content
- Syntax:
grep [options] 'pattern' [file(s)] - Common options:
-i: Ignore case-r: Recursively search directories
- Example: Search for lines containing "error" (case-insensitive) in log_file.txt.
grep -i "error" log_file.txt
echocommand:- Used to output text content
- Syntax:
echo [options] [string] - Common options:
-n: Output without a trailing newline
- Example: Output "Hello, World!".
echo "Hello, World!"
tailcommand:- Used to view the end of a file
- Syntax:
tail [options] [file] - Common options:
-n: Display the last n lines-f: Continuously monitor file changes
- Example: View the last 5 lines of log_file.txt.
tail -n 5 log_file.txt
sed(stream editor) command:- Used for text substitution and modification
- Syntax:
sed [options] 'commands' [file(s)] - Common options:
-i: Modify the original file in place
- Example: Replace all occurrences of "old_string" with "new_string" in file.txt.
sed 's/old_string/new_string/g' file.txt
vi(visual editor) command:- Used for creating and editing files
- Basic operation modes:
- Command mode: Input commands
- Insert mode: Edit text
- Last line mode: Save and exit
- Example: Open file.txt for editing.
vi file.txt