Understanding Linux Commands: File Viewing and Manipulation

Partial Command Line Explanation:

  • cat: cat file.txt

    • Display the content of file.txt.
  • cp: cp file.txt newfile.txt

    • Copy the contents of file.txt to newfile.txt.
  • grep: grep "pattern" file.txt

    • Search for "pattern" in file.txt.
  • sed: sed 's/old_text/new_text/' file.txt

    • Replace occurrences of "old_text" with "new_text" in file.txt.
  • awk: awk '{print $1, $2}' file.txt

    • Print the first and second columns of file.txt.
  • head: head file.txt

    • Display the initial lines of file.txt.
  • tail: tail -n 10 file.txt

    • Display the last 10 lines of file.txt.

cat (Concatenate)

  • Usage: cat [OPTIONS] filename

  • Function: It's primarily used to display the contents of a file on the terminal.

  • Examples:

    • Display file content: cat file.txt

    • Concatenate multiple files and display: cat file1.txt file2.txt

    • Output file content to a new file: cat file.txt > newfile.txt

1. Display File Content:

cat file.txt

This command simply displays the contents of the file named file.txt onto the terminal.

2. Concatenate Multiple Files and Display:

cat file1.txt file2.txt

By specifying multiple filenames as arguments, cat concatenates them in the order they are listed and displays their combined content on the terminal.

3. Output File Content to a New File:

cat file.txt > newfile.txt

This command redirects the output of cat file.txt (contents of file.txt) to a new file named newfile.txt. If newfile.txt doesn't exist, it will be created; if it does, it will be overwritten.

Move file/folder to other folder

mv filename foldername
mv newfilefile.txt devops

For create number of files & directory

touch file_{1..10}.txt
mkdir new_folder{1..10}

Copy & Move:

cp (Copy)

  • Function: Copies files or directories from one location to another.

      cp file.txt newfile.txt # Copy file.txt to newfile.txt
    
      cp -r directory/ newdir/ # Copy directory recursively to a new directory
    

File copy or move command

cp -r filename devops
mv folderName devops

Remove all txt file

rm file_*.txt  (remove all txt file)

Remove all new folder file

rm -r new*

grep (Global Regular Expression Print):

  • Usage: grep [OPTIONS] pattern filename

Function: Search for specific patterns or strings in files.

Example:

grep "search_string" file.txt # Search for "search_string" in file.txt
grep test file.txt
grep test file.txt file1.txt file2.txt

awk (Pattern Scanning and Processing Language)

The awk command is a powerful tool used for pattern scanning and text processing. It's incredibly versatile for data extraction, manipulation, and reporting.

Further Explanation:

  • awk is exceptionally versatile. It can perform various actions like printing, arithmetic operations, data manipulation, etc., based on the provided pattern.

  • $1, $2, etc., are fields that represent columns in a line, separated by spaces or a specified delimiter.

  • {print $1, $2} tells awk to print the first and second columns of the matched lines.

Example Scenario:

Let's assume file.txt contains the following lines:

apple 20 30
banana 40 50
orange 60 70

If you run the awk command provided:

awk '/an/ {print $1, $2}' file.txt

It will match lines containing 'an' (like 'banana' and 'orange' in this case) and print their first and second columns:

banana 40
orange 60
vi file.txt
cat file.txt
awk '/an/ {print $1, $2}' file.txt
awk '/pp/ {print $1, $2 ,$3}' file.txt
awk '/ap/ {print $1}' file.txt
awk '/a/ {print $1, $2 ,$3}' file.txt
awk '/an/ {print $1, $2 ,$3}' file.txt

This is just a simple example; awk offers extensive capabilities for text processing and is widely used for more complex data manipulation tasks in scripting and data analysis.

date
date | awk '{printf "%s %s %s\n", $1, $2, $3}'

head and tail (Display Beginning and End of Files)

head Command:

  • Function: Displays the beginning of a file.

Usage:

head file.txt: Displays the first few lines (default 10) of file.txt.
head -n N file.txt: Displays the first N lines of file.txt.
head files.txt
head -n 4 files.txt

tail Command:

Function: Displays the end of a file.

Usage:

tail file.txt: Displays the last few lines (default 10) of file.txt.
tail -n N file.txt: Displays the last N lines of file.txt.
tail files.txt
tail -n 5 files.txt