0% found this document useful (0 votes)
22 views47 pages

File Viewing Editing Linux Command.

File Viewing/Editing • cat — Concatenate and display file contents. • more — View file contents (paginates). • less — View file contents (paginates with navigation). • head — View the first few lines of a file. • tail — View the last few lines of a file. • vi or vim — Advanced text editor. • awk — Pattern scanning and processing. • sed — Stream editor for modifying files. • cut — Remove sections from lines of text. • sort — Sort lines of text. • uniq — Filter duplicate lines.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views47 pages

File Viewing Editing Linux Command.

File Viewing/Editing • cat — Concatenate and display file contents. • more — View file contents (paginates). • less — View file contents (paginates with navigation). • head — View the first few lines of a file. • tail — View the last few lines of a file. • vi or vim — Advanced text editor. • awk — Pattern scanning and processing. • sed — Stream editor for modifying files. • cut — Remove sections from lines of text. • sort — Sort lines of text. • uniq — Filter duplicate lines.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Day 2

File Viewing/Editing (10)


 cat — Concatenate and display file contents.

 more — View file contents (paginates).

 less — View file contents (paginates with navigation).

 head — View the first few lines of a file.

 tail — View the last few lines of a file.

 awk — Pattern scanning and processing.

 sed — Stream editor for modifying files.

 cut — Remove sections from lines of text.

 sort — Sort lines of text.

 uniq — Filter duplicate lines.

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 [Link]

2. View the contents of multiple files:


Command: cat [Link] [Link]

3. Create a new file:


Command: cat > [Link]
After entering the command, type the content and press Ctrl+D to save.
4. Append text to an existing file:
Command: cat >> [Link]
After entering the command, type the content and press Ctrl+D to save.

5. Display line numbers for a file:


Command: cat -n [Link]

6. Suppress repeated empty lines:


Command: cat -s [Link]
7. Display tabs and end-of-line characters:
Command: cat -T -E [Link]

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 [Link]

2. Start reading from a specific line:


Command: more +50 [Link]
Starts displaying content from line 50.
3. Squeeze multiple blank lines:
Command: more -s [Link]
4. Display content Ensures a clean display by refreshing the screen for every
new page:
Command: more -c [Link]

Note : Clears the screen before displaying new content for each page.

Summary of Difference:

more [Link]: Scrolls down, leaving old content on the screen.

more -c [Link]: Clears the screen before displaying the new content, offering a cleaner display.
5. View content with a prompt for invalid keys:
Command: more -d [Link]
6. Combine options to ignore form feed characters and clear the screen:
Command: more -l -p [Link]

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:

1. -N — Display line numbers.

2. -S — Do not wrap long lines; truncate them instead.

3. -X — Do not clear the screen after exiting.

4. -M — Display a more detailed prompt.

5. -i — Ignore case when searching.

6. -p [pattern] — Start by searching for the specified pattern.

7. +F — Enter follow mode (similar to tail -f).

8. +num — Start displaying from the specified line number.

Examples:

1. View a file interactively:


Command: less [Link]
2. Display line numbers:
Command: less -N [Link]

3. Prevent wrapping of long lines:


Command: less -S [Link]

The -S option in the less command disables line wrapping. If a line in [Link] 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 [Link]

Begins displaying content from line 100.

5. Search for a pattern:


Command: less -p "pattern" [Link]
Highlights and starts at the first occurrence of "pattern".

6. Follow the end of a file (like tail -f):


Command: less +F [Link]

7. Ignore case in searches:


Command: less -i [Link]
Allows case-insensitive searching with /pattern.

8. Combine options to display line numbers and prevent screen clearing:


Command: less -N -X [Link]

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:

1. -n [number] — Display the specified number of lines (e.g., -n 20 shows 20 lines).

2. -c [number] — Display the specified number of bytes (e.g., -c 50 shows the first 50 bytes).

3. -q — Suppress file name headers when displaying multiple files.

4. -v — Always display file name headers, even if only one file is provided.

Examples:

1. Display the first 10 lines of a file (default):


Command: head [Link]

2. Show the first 20 lines of a file:


Command: head -n 20 [Link]
3. Display the first 50 bytes of a file:
Command: head -c 50 [Link]

4. Use with multiple files, suppressing file name headers:


Command: head -q [Link] [Link]
5. Always display file name headers for multiple files:
Command: head -v [Link] [Link]

6. Combine options to display the first 20 lines and file name headers:
Command: head -n 20 -v [Link]
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).

3. -f — Follow a file as it grows, displaying new lines as they are appended.

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.

6. -q — Suppress file name headers when displaying multiple files.

7. -v — Always display file name headers, even if only one file is provided.

Examples:

1. Display the last 10 lines of a file (default):


Command: tail [Link]

2. Show the last 20 lines of a file:


Command: tail -n 20 [Link]
3. Display the last 50 bytes of a file:
Command: tail -c 50 [Link]

4. Continuously monitor a log file for new entries:


Command: tail -f /var/log/syslog

5. Monitor a growing file and terminate when a specific process ends:


Command: tail -f --pid=1234 [Link]

6. Combine options to display the last 15 lines and file name headers:
Command: tail -n 15 -v [Link]

7. Use --retry to continuously attempt accessing a file:


Command: tail --retry -f [Link]

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:

1. -F [delimiter] — Specify a field delimiter (default is whitespace).

2. -v [var=value] — Assign variables for use in the script.

3. -f [file] — Execute an awk program from a file.

4. --help — Display help information.

5. --version — Display the version of awk.

Examples:

1. Print all lines of a file:


Command: awk '{print}' [Link]

Output: Displays the entire content of the file.

2. Print the first field (column) of each line:


Command: awk '{print $1}' [Link]
Output: Displays the first column from the file. By default, awk uses spaces or tabs as the field
separator.

3. Print lines where the second field (salary)is greater than 100:
Command: awk '$2 > 100 {print}' [Link]

Output: Shows lines with the second column value exceeding 100.

4. Use a custom delimiter (e.g., comma):


Command: awk -F ',' '{print $1, $2}' [Link]

Output: Displays the first two columns from a CSV file.

5. Print lines containing a specific pattern:


Command: awk '/ERROR / {print}' [Link]
Output: Displays lines with the word "error". Case sensitive.

6. Calculate the sum of the second column:


Command: awk '{sum += $2} END {print sum}' [Link]

Output: Displays the total of all values in the second column.

7. Assign a variable and use it in the script:


Command: awk -v threshold=50 '$3 > threshold {print $1, $3}' [Link]

Output: Displays the first and third columns where the third column exceeds 50.

8. Execute an awk program from a file:


Command: awk -f [Link] [Link]
Output: Executes the commands written in the [Link] file on [Link].

9. Print line numbers with content:


Command: awk '{print NR, $0}' [Link]
Output: Displays each line prefixed by its line number.

10. Format output with text:


Command: awk '{print "Name:", $1, "Score:", $2}' [Link]

Output: Custom formatted output like Name: John Score: 85.


Command: sed
Definition:
The sed (stream editor) command is used for parsing and transforming text. It operates line by line on a
file or input stream, allowing text substitution, deletion, insertion, and more.

Pattern to Use:
sed [options] 'script' [file...]

Options:

1. -n — Suppress automatic printing; only print lines specified by the script.

2. -e [script] — Add the script to the commands to be executed.

3. -f [file] — Execute commands from a script file.

4. -i — Edit files in place without creating a backup.

5. --help — Display help information.

6. --version — Display the version of sed.

Examples:

1. Substitute a word:
Command: sed 's/old/new/' [Link]

Output: Replaces the first occurrence of "old" with "new" in each line.

2. Substitute globally:
Command: sed 's/old/new/g' [Link]
Output: Replaces all occurrences of "old" with "new" in each line.

3. Substitute only on specific lines:


Command: sed '2s/old/new/' [Link]

Output: Replaces "old" with "new" on the second line only.


4. Delete specific lines:
Command: sed '3d' [Link]

Output: Deletes the third line from the file.

5. Print only lines matching a pattern:


Command: sed -n '/pattern/p' [Link]

Output: Displays lines containing the word "pattern".

6. Insert text before a line:


Command: sed '3i\Inserted text' [Link]
Output: Adds "Inserted text" before the third line.

7. Append text after a line:


Command: sed '3a\Appended text' [Link]

Output: Adds "Appended text" after the third line.

8. Replace text and save to a new file:


Command: sed 's/old/new/g' [Link] > [Link]

Output: Saves the modified content to a new file.


9. Edit a file in place:
Command: sed -i 's/old/new/g' [Link]

Output: Modifies [Link] directly, replacing "old" with "new".

10. Remove blank lines:


Command: sed '/^$/d' [Link]

Output: Deletes all blank lines in the file.

11. Print lines within a range:


Command: sed -n '5,10p' [Link]
Output: Displays lines 5 through 10.

12. Replace text based on a regular expression:


Command: sed 's/[0-9]/#/g' [Link]

Output: Replaces all digits with the # symbol.

13. Change the delimiter:


Command: sed 's|/usr/bin|/usr/local/bin|g' [Link]
Output: Replaces /usr/bin with /usr/local/bin using | as the delimiter.

14. Combine multiple commands:


Command: sed -e 's/old/new/' -e '/pattern/d' [Link]
Output: Substitutes "old" with "new" and deletes lines containing "pattern".

15. Use a script file for transformations:


Command: sed -f [Link] [Link]

Output: Executes commands from [Link] on [Link].

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.

2. -c [list] — Select specific characters from each line.

3. -f [list] — Select specific fields (columns) from each line.

4. -d [delimiter] — Specify a custom delimiter (default is TAB).

5. --complement — Invert the selection, showing all but the specified fields.

6. --output-delimiter=[string] — Change the output delimiter (default is the input delimiter).

7. --help — Display help information.

8. --version — Display the version of cut.

Examples:

1. Extract specific bytes:


Command: cut -b 1-5 [Link]

Output: Displays the first 5 bytes from each line of the file.

2. Extract specific characters:


Command: cut -c 3-7 [Link]
Output: Displays characters from positions 3 to 7 of each line.

3. Extract specific fields (columns):


Command: cut -f 2 [Link]

Output: Displays the second field from each line.

Explanation:

o The -f 2 option extracts the second field (column) from each line.

o Since the fields are tab-separated, this command works as expected.


o For space-delimited files, you need to specify the delimiter using the -d option, like cut -
d ' ' -f 2 [Link].

4. Extract fields with a custom delimiter:


Command: cut -d ',' -f 1 [Link]

Output: Extracts the first column from a CSV file, assuming , is the delimiter.

5. Change the output delimiter:


Command: cut -d ',' -f 1,3 --output-delimiter='|' [Link]

Output: Extracts the first and third columns and separates them with |.
6. Extract multiple fields:
Command: cut -d ',' -f 1,3-5 [Link]

Output: Extracts the first, third, fourth, and fifth columns from the file.

7. Invert the selection:


Command: cut -d ',' --complement -f 2 [Link]

Output: Displays all fields except the second column.

8. Extract from standard input:


Command: echo "apple,banana,cherry" | cut -d ',' -f 2
Output: banana

9. Extract characters with a range:


Command: cut -c 1,5-7 [Link]

Output: Displays the first, fifth, sixth, and seventh characters from each line.

10. Use with piping:


Command: ls -l | cut -d ' ' -f 1

Output: Extracts the file permissions column from the ls -l output.

11. Combine with other tools:


Command: grep "pattern" [Link] | cut -c 1-10

Output: Extracts the first 10 characters of lines containing "pattern".

12. Handle fixed-width fields:


Command: cut -c 5-10 [Link]
Output: Displays a fixed-width segment from positions 5 to 10 in each line.

13. Remove unwanted fields:


Command: cut --complement -d ',' -f 3 [Link]

Output: Removes the third column from the CSV file.

14. Extract with an alternate delimiter:


Command: cut -d ':' -f 1 /etc/passwd
Output: Displays usernames from the /etc/passwd file, where : is the delimiter.

15. Extract fields and redirect output:


Command: cut -d ',' -f 1-2 [Link] > [Link]
Output: Saves the first two columns to a new file, [Link].

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:

1. -n — Sort numerically (default is lexicographically).

2. -r — Reverse the sorting order (descending).

3. -u — Output only the first occurrence of each line (remove duplicates).

4. -k [field] — Sort based on a specific field/column.

5. -t [delimiter] — Specify a custom delimiter (default is whitespace).

6. -f — Ignore case when sorting.

7. -M — Sort by month names (e.g., Jan, Feb, Mar).

8. -b — Ignore leading blanks.


9. -g — Sort according to general numerical value (float values).

10. -o [file] — Output sorted data to a file instead of standard output.

11. --reverse — Same as -r, reverses the sorting order.

12. --help — Display help information.

13. --version — Display the version of sort.

Examples:

1. Sort lines in ascending order (default):


Command: sort [Link]

Output: Sorts the lines of the file alphabetically in ascending order.

2. Sort in reverse (descending) order:


Command: sort -r [Link]
Output: Sorts the lines of the file in reverse order (Z-A, largest to smallest).

3. Sort numerically:
Command: sort -n [Link]

Output: Sorts lines in the file numerically (1, 2, 10, 20, ...).

4. Sort by a specific column/field:


Command: sort -k 2 [Link]
Output: Sorts lines based on the second field/column.

5. Sort and remove duplicates:


Command: sort -u [Link]
Output: Sorts and removes duplicate lines.

6. Sort by a specific field with a delimiter:


Command: sort -t ',' -k 2 [Link]

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.

8. Sort ignoring case:


Command: sort -f [Link]

Output: Sorts the file while ignoring case (e.g., "a" = "A").

9. Sort by month names:


Command: sort -M [Link]
Output: Sorts lines based on the month names (Jan, Feb, Mar, etc.).

10. Sort and output to a new file:


Command: sort [Link] -o sorted_file.txt

Output: Sorts the contents of [Link] and saves the result in sorted_file.txt.

11. Sort by general numerical values:


Command: sort -g floating_numbers.txt
Output: Sorts the file based on general numerical values, handling floating-point numbers
correctly.

12. Sort ignoring leading blanks:


Command: sort -b [Link]
Output: Sorts the lines while ignoring leading spaces or tabs.

13. Sort by a specific column in a text file with tabs as delimiters:


Command: sort -t$'\t' -k 2 [Link]

Output: Sorts the file based on the second tab-delimited column.

14. Sort and save the sorted output to a file:


Command: sort -r [Link] > sorted_file.txt
Output: Sorts in reverse order and redirects the output to sorted_file.txt.

15. Sort with numeric values in reverse order:


Command: sort -n -r [Link]

Output: Sorts the file in descending numerical order.

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:

1. -c — Prefix lines with the number of occurrences.

2. -d — Only show duplicate lines.

3. -u — Only show unique (non-duplicate) lines.

4. -i — Ignore case while comparing lines.

5. -w [N] — Compare only the first N characters of each line.

6. -f [N] — Skip the first N fields (columns) when comparing lines.

7. -s [N] — Skip the first N characters of each line before comparing.

8. -v — Invert the selection (show only lines that are not repeated).

9. -o [output_file] — Redirect the output to a specific file.

10. --all-repeated=separate — Show all repeated lines grouped together.

11. --help — Show help information about the uniq command.

Examples:

1. Remove adjacent duplicate lines:


Command: uniq [Link]
Output: Removes adjacent duplicates from [Link] and prints the unique lines.

2. Count occurrences of each line:


Command: uniq -c [Link]
Output: Displays the number of occurrences of each unique line in [Link].

3. Show only duplicate lines:


Command: uniq -d [Link]
Output: Displays only the lines that appear more than once.

4. Show only unique lines:


Command: uniq -u [Link]
Output: Displays only the lines that appear exactly once in the file.

5. Ignore case when comparing lines:


Command: uniq -i [Link]
Output: Removes duplicates while ignoring case differences (e.g., "apple" and "Apple" will be
treated as the same).

6. Skip the first N characters when comparing lines:


Command: uniq -s 5 [Link]
Output: Skips the first 5 characters of each line when comparing, helping to ignore certain
prefixes.

7. Compare only the first N characters of each line:


Command: uniq -w 5 [Link]
Output: Compares only the first 5 characters of each line when determining uniqueness.

8. Redirect output to a file:


Command: uniq [Link] > [Link]
Output: Removes duplicates from [Link] and saves the result in [Link].

9. Show lines that are not repeated (invert selection):


Command: uniq -v [Link]
Output: Displays only the lines that are unique in the file (not repeated).

10. Show all repeated lines grouped together:


Command: uniq --all-repeated=separate [Link]
Output: Groups and displays all repeated lines together.

11. Show only duplicates based on a specific field (column):


Command: uniq -f 1 [Link]
Output: Compares lines, skipping the first field and showing only duplicates based on
subsequent fields.

You might also like