UNIX file handling commands diff, regex, access permission

UNIX file handling commands diff, regex, access permission

UNIX is a powerful operating system that provides various commands for file handling, including diff, regex, and managing access permissions. Here's an overview of these commands:

  1. diff: The diff command is used to compare the contents of two files line by line and display the differences between them. It is commonly used to compare program source code, configuration files, or any text-based files. The basic syntax is as follows:

     diff file1 file2
    

    The output of the diff command shows the added or deleted lines between the two files. Additionally, you can use various options to customize the output and control the behavior of the command.

  2. Regular Expressions (Regex): Regular expressions are powerful patterns used for searching and manipulating text in UNIX. There are several UNIX tools that support regular expressions, such as grep, sed, and awk. Here are a few examples:

    • grep command: It is used to search for a specific pattern in a file or standard input. For example, to search for the word "example" in a file named file.txt, you can use the following command:

        grep "example" file.txt
      
    • sed command: It is used for text manipulation and transformation. You can use regular expressions with sed to perform search and replace operations. For example, to replace all occurrences of "foo" with "bar" in a file named file.txt, you can use the following command:

        sed 's/foo/bar/g' file.txt
      
    • awk command: It is a versatile text processing tool that supports regular expressions for pattern matching. You can use awk to extract specific fields from a file, perform calculations, and more. For example, to print all lines containing the word "example" in a file named file.txt, you can use the following command:

        awk '/example/ {print}' file.txt
      
  3. Access Permissions: UNIX-based systems use a permission system to control access to files and directories. The chmod command is used to change the permissions of files and directories. Here are some common examples:

    • Change permissions using symbolic mode:

        chmod u+r file.txt   # Add read permission for the owner
        chmod go-w file.txt  # Remove write permission for the group and others
        chmod a+x script.sh # Add execute permission for all (owner, group, others)
      
    • Change permissions using octal mode:

        chmod 644 file.txt  # Set read and write for owner, read for group and others
        chmod 755 script.sh # Set read, write, and execute for owner, and read and execute for group and others
      

The ls -l command can be used to view the current permissions of files and directories in long listing format.

These commands provide essential functionality for handling files, comparing differences, working with regular expressions, and managing access permissions in UNIX-like systems.