File Viewing Editing Linux command.
File Viewing Editing Linux command.
Command: cat
Definition:
The cat command in Linux is used to concatenate and display the contents of files.
It can also be used to create, view, and append files.
Pattern to Use:
cat [options] [file...]
Options:
1. -n — Number all output lines.
2. -b — Number only non-empty output lines.
3. -s — Suppress repeated empty output lines.
4. -E — Display $ at the end of each line.
5. -T — Display tabs as ^I.
6. -A — Display all non-printing characters, including newlines, tabs, etc.
Examples:
1. Display the contents of a file:
Command: cat file.txt
Command: more
Definition:
The more command in Linux is used to view the content of a file one screen at a
time, making it useful for reading large files interactively.
Pattern to Use:
more [options] [file...]
Options:
1. -d — Display a prompt with usage instructions when a wrong key is pressed.
2. -f — Count logical lines instead of screen lines (does not fold long lines).
3. -l — Ignore form feed (Ctrl+L) characters.
4. -p — Clear the screen before displaying content.
5. -c — Display content one screen at a time without scrolling.
6. -s — Squeeze multiple blank lines into a single blank line.
7. +num — Start displaying from the specified line number.
Examples:
1. View a file one screen at a time:
Command: more file.txt
Note : Clears the screen before displaying new content for each page.
Summary of Difference:
more -c large.txt: Clears the screen before displaying the new content, offering a cleaner display.
5. View content with a prompt for invalid keys:
Command: more -d file.txt
6. Combine options to ignore form feed characters and clear the screen:
Command: more -l -p file.txt
Command: less
Definition:
The less command in Linux is a file pager used to view file content one screen at a time. Unlike more, it
allows backward navigation and provides more advanced features for searching and navigating files
interactively.
Pattern to Use:
less [options] [file...]
Options:
Examples:
The -S option in the less command disables line wrapping. If a line in file.txt is too long to fit the
width of the terminal, it will not wrap to the next line. Instead, you can scroll horizontally to view
the rest of the line.}
4. Start viewing from a specific line:
Command: less +100 file.txt
Command: head
Definition:
The head command in Linux is used to display the first few lines of a file or standard input. By default, it
shows the first 10 lines but can be customized with options.
Pattern to Use:
head [options] [file...]
Options:
2. -c [number] — Display the specified number of bytes (e.g., -c 50 shows the first 50 bytes).
4. -v — Always display file name headers, even if only one file is provided.
Examples:
6. Combine options to display the first 20 lines and file name headers:
Command: head -n 20 -v file.txt
Command: tail
Definition:
The tail command in Linux is used to display the last few lines of a file or standard input. By default, it
shows the last 10 lines but can be customized with options.
Pattern to Use:
tail [options] [file...]
Options:
1. -n [number] — Display the specified number of lines (e.g., -n 15 shows the last 15 lines).
2. -c [number] — Display the specified number of bytes (e.g., -c 100 shows the last 100 bytes).
4. --pid [pid] — Terminate tailing when the specified process ID (PID) exits.
5. --retry — Keep trying to open a file, useful if the file doesn’t yet exist or has been temporarily
moved.
7. -v — Always display file name headers, even if only one file is provided.
Examples:
6. Combine options to display the last 15 lines and file name headers:
Command: tail -n 15 -v file.txt
Command: awk
Definition:
The awk command is a powerful text-processing tool used to search, manipulate, and format text within
files or input streams. It operates on a per-line basis, splitting data into fields based on delimiters and
performing specified actions.
Pattern to Use:
awk [options] 'pattern {action}' [file...]
Options:
Examples:
3. Print lines where the second field (salary)is greater than 100:
Command: awk '$2 > 100 {print}' file.txt
Output: Shows lines with the second column value exceeding 100.
Output: Displays the first and third columns where the third column exceeds 50.
Pattern to Use:
sed [options] 'script' [file...]
Options:
Examples:
1. Substitute a word:
Command: sed 's/old/new/' file.txt
Output: Replaces the first occurrence of "old" with "new" in each line.
2. Substitute globally:
Command: sed 's/old/new/g' file.txt
Output: Replaces all occurrences of "old" with "new" in each line.
Command: cut
Definition:
The cut command in Linux is used to extract sections of text from each line of a file or standard input. It
can cut text by bytes, characters, or fields using a delimiter.
Pattern to Use:
cut [options] [file...]
Options:
1. -b [list] — Select specific bytes from each line.
5. --complement — Invert the selection, showing all but the specified fields.
Examples:
Output: Displays the first 5 bytes from each line of the file.
Explanation:
o The -f 2 option extracts the second field (column) from each line.
Output: Extracts the first column from a CSV file, assuming , is the delimiter.
Output: Extracts the first and third columns and separates them with |.
6. Extract multiple fields:
Command: cut -d ',' -f 1,3-5 file.csv
Output: Extracts the first, third, fourth, and fifth columns from the file.
Output: Displays the first, fifth, sixth, and seventh characters from each line.
Command: sort
Definition:
The sort command in Linux is used to sort lines of text in a file or input. It can sort in ascending or
descending order and supports different sorting criteria like numerical, alphabetical, and more.
Pattern to Use:
sort [options] [file...]
Options:
Examples:
3. Sort numerically:
Command: sort -n numbers.txt
Output: Sorts lines in the file numerically (1, 2, 10, 20, ...).
Output: Sorts by the second column in a CSV file, using , as the delimiter.
7. Sort with a custom delimiter:
Command: sort -t ':' -k 1 file_delimited.txt
Output: Sorts the file based on the first field with : as the delimiter.
Output: Sorts the file while ignoring case (e.g., "a" = "A").
Output: Sorts the contents of file.txt and saves the result in sorted_file.txt.
Command: uniq
Definition:
The uniq command in Linux is used to filter out repeated lines from a file or input. It is typically used
after sorting a file or output to remove consecutive duplicate lines. By default, it only removes adjacent
duplicates, and often used with sort to ensure that duplicates across the file are removed.
Pattern to Use:
uniq [options] [input_file] [output_file]
Options:
8. -v — Invert the selection (show only lines that are not repeated).
Examples: