Day-6: Linux Commands: ACLs, Grep, Find, Locate

Day-6: Linux Commands: ACLs, Grep, Find, Locate

What is ACL?

ACL stands for Access Control List and we use ACLs to grant permission to specific users or groups for the files and directories. ACLs are very useful when there are multiple users or groups which require different level of access to same file or directory.

setfacl Command

We use setfacl command to set Access Control List (ACL) for file or directory.

Syntax for setfacl is:

setfacl <option> <action> filename

Common Options which we use with setfacl are:

  • -m : We use -m option to add or modify specific ACL

  • -x : We use -x option to remove specific ACL

  • -b : If we want to remove all ACL Entries, than we use -b option

  • -R: If we want to apply ACLs to subdirectories and files within the directory, than we use -R option to apply ACL recursively

Example 1: Grant Read Permission to a User

This grants Read( r ) permission to user student for file1.txt

Example 2: Grant Read and Write Permissions to a Group

This grants Read ( r ) and Write (w) permission to group student for file1.txt

Example 3: Apply Permissions Recursively

This grants permission of Read and Write to user student for /home/umar directory and it’s contents

Example 4: Remove a Specific ACL

This removes the ACLs for group student for file1.txt

getfacl Command

The getfacl command fetches the information and display the ACLs for files and directories.

Sytanx is:

getfacl <file/directory>

Example 1: View ACL for a File

Example 2: View ACL for a Directory

Linux Commands: find and grep

The find and grep commands are essential tools in Linux for locating files and searching text within files. These commands are highly versatile and can be used together for powerful search operations

1. The find Command

The “find” command in Linux is a powerful utility used to search for files and directories within a filesystem. It allows users to locate files and perform actions on them based on a variety of criteria, such as name, size, permissions, modification date, and more..

Basic Syntax:

find [path] [expression] [actions]

  • path: Specifies the directory or path to search. Use . for the current directory or / for the root directory.

  • expression: Defines the search criteria (e.g., file name, size, permissions).

  • actions: Specifies what to do with the matching files (e.g., print, delete).

Common Options and Expressions:

  • -name: Search for files by name (case-sensitive).

  • -iname: Search for files by name (case-insensitive).

  • -type: Search by type (e.g., f for files, d for directories).

  • -size: Search for files based on size (e.g., +1M for files larger than 1MB).

  • -mtime: Search for files modified a certain number of days ago (e.g. “-mtime -5” for files modified in the last 5 days).

  • -exec: Execute a command on the found files.

Commonly Used Options

  • Find Files by Name:

    • It searches for file name file1.txt in /home/umar directory and it’s subdirectories

      For Case Senstive , we use use option -iname as shown below:

  • Find Files by Type:

    It searches all the files in directory /home/umar

  • Find Files by Size:

    It shows all the files which are less than 5KB Size.

    This shows the file which is larger than 10MB

  • Find Files Modified Recently:

    This searches for files in /etc directory which are modified in last 3 days. Similarly we can also check in hours as shown below:

    Above we can see the list of files in /etc directory which are modified in last 3 hours

  • Find Files and Execute Commands:

    This will find and delete all .log files in /tmp

  • Find Files by Permissions:

    This will search for all the files which have permission 644 in /etc.

    In above Lab, we can see that we have search all the files with permission 644 and files ending on .conf extension.

2. The grep Command

The grep command in Linux is a powerful tool used for searching text or patterns within files. It scans files or input streams line by line for a specified pattern, and displays matching lines.

Basic Syntax

grep [options] pattern [file...]

  • pattern: The string or regular expression you want to search for.

  • file: The file(s) to search in. If omitted, grep reads from standard input.

Common Options:

  • -i: Perform a case-insensitive search.

  • -r: Search recursively in directories.

  • -v: Invert the match to find lines that do not match the pattern.

  • -n: Show line numbers in output.

  • -l: Show only file names containing the match

Examples:

1. Search for a Word in a File:

This searches for word “error” in the /home/umar/file2.txt

We will use option -i in case if we want to search for case insensitive word as shown below:

3. Recursive Search in a Directory:

With help of this , it will search word ErroR recursively in all files and folders as shown below:

4. Display Line Numbers:

This searches for word error in /home/umar.file2.txt and display the line numbers.

5. Invert the Match:

This will display all the lines in /home/umar/file2.txt that does not contain the word error.

3. Combining find and grep

The find and grep command can be combine for powerful searches such as searching for specific text within files that match certain criteria.This is especially useful when you need to search for a pattern in files across a directory tree.

Basic Syntax

find [path] [options] -exec grep [grep_options] "pattern" {} +

  • find: Locates files based on specified criteria (e.g., name, size, permissions).

  • grep: Searches within the files located by find for a specific pattern.

Example

1.Find Files and Search for Text:

Explanation of the Components

  1. /home/umar: Directory to search.

  2. -type f: Restricts the search to files.

  3. -name "*.txt": Searches only for .txt files.

  4. -exec grep "error" {}: Executes the grep command on each matching file.

  5. +: Indicates that grep should process multiple files at once for better performance.

2.Search with Line Numbers:

Find all “.txt” files in /home/umar and search for the word "error" with line numbers.

Conclusion

Combining find and grep enables efficient searching of patterns within files across directories. Whether you’re a system administrator, developer, or analyst, mastering this combination is essential for managing large datasets or logs effectively.

4. locate command

The locate command is a powerful and efficient utility for quickly finding files or directories in Linux. It is faster than the find command because it searches a prebuilt database rather than scanning the filesystem in real time.

Syntax

locate [option] <filename>

1.Search for a File:

This will search for any file name with file2

2.Wildcard Search:

This will search for all the files with .log extension.

Refreshing the Database:

The locate command relies on a database that must be updated to include new files. You can update the database using the updatedb command

If you are not root user, than try sudo updatedb