Unix Commands

Category : Unix | Sub Category : Unix | By Prasad Bonam Last updated: 2024-03-02 03:59:11 Viewed : 104


Unix commands are powerful tools for interacting with the Unix operating system and its variants like Linux. Here are explanations for some advanced Unix commands:

  1. grep: This command is used to search text or files for lines that match a specific pattern. It is incredibly powerful when combined with regular expressions. For example:

    bash
    grep "pattern" file.txt
  2. find: Used to search for files in a directory hierarchy. It allows you to search based on various criteria like name, type, size, etc.

    bash
    find /path/to/search -name "filename"
  3. awk: A versatile programming language mainly used for pattern scanning and processing. It is often used for data extraction and reporting.

    bash
    awk `{print $2}` file.txt
  4. sed: Stands for stream editor. It is used for filtering and transforming text, typically used in conjunction with piping.

    bash
    sed `s/old_pattern/new_pattern/g` file.txt
  5. xargs: Used to build and execute command lines from standard input. It is often used with find, grep, and other commands for efficient command execution.

    bash
    find . -type f -name "*.txt" | xargs grep "pattern"
  6. rsync: A powerful utility for syncing files and directories between two locations. It is commonly used for backups and mirroring.

    bash
    rsync -avz source/ destination/
  7. wget: Used for downloading files from the internet via HTTP, HTTPS, and FTP protocols.

    bash
    wget http://example.com/file.txt
  8. curl: Another tool for transferring data from or to a server. It supports various protocols and can be used for testing APIs, downloading files, etc.

    bash
    curl -O http://example.com/file.txt
  9. tar: Stands for tape archive. It is used for archiving files and directories into a single file (often called a "tarball") and optionally compressing it.

    bash
    tar -cvzf archive.tar.gz /path/to/directory
  10. chmod: Used to change permissions of files and directories.

    bash
    chmod 755 file.sh

These commands, along with many others, form the backbone of Unix-like operating systems, offering administrators and users powerful tools for managing and manipulating files, processes, and system configurations.

awk `{print $2}` file.txt with example

Let`s say you have a file called data.txt with the following content:

Name Age Gender John 25 Male Alice 30 Female Bob 22 Male

If you want to extract only the second column (which contains ages), you can use the awk command like this:

bash
awk `{print $2}` data.txt

Output:

Age 25 30 22

Explanation:

  • awk is invoked with the single quotes enclosing {print $2}. This is an awk program that tells awk to print the second field of each line.
  • file.txt is provided as input to awk.
  • For each line of file.txt, awk splits the line into fields based on whitespace (by default). $2 refers to the second field in each line.
  • awk then prints the second field of each line, resulting in the output you see above.


sed `s/old_pattern/new_pattern/g` file.tx with example:

The sed command is used for text manipulation and stands for "stream editor." Lets consider an example with a file called data.txt containing the following text:

vbnet
Hello world This is a sample text. Replace old_pattern with new_pattern.

If you want to replace occurrences of "sample" with "example" in this file, you can use sed like this:

bash
sed  `s/sample/example/g` data.txt

Output:

vbnet
Hello world This is a example text. Replace old_pattern with new_pattern.

Explanation:

  • sed is invoked with the single quotes enclosing the s/sample/example/g command.
  • s indicates a substitution.
  • sample is the old pattern that you want to replace.
  • example is the new pattern that you want to replace it with.
  • g stands for global, meaning it will replace all occurrences in each line. If you omit g, sed will only replace the first occurrence in each line.

So, in this example, sed replaces all occurrences of "sample" with "example" in the data.txt file and prints the modified text to the standard output.


Search
Sub-Categories
Related Articles

Leave a Comment: