File Creation, Manipulation, and Permissions

File Creation

Creating files in RHEL 9 can be done using various methods. The most straightforward method is using the touch command. This command allows you to create an empty file.

Example: To create a file named myfile.txt, use the command: touch myfile.txt This will create an empty file called myfile.txt.

You can also create and write to a file using the cat command with redirection:

Example: To create a file named myfile.txt and write to it, use the command: cat > myfile.txt Type your text, and when done, press Ctrl+D to save and exit.

File Manipulation

Manipulating files involves editing, copying, moving, and deleting files. Here are some common file manipulation tasks:

  • Editing a File: You can use text editors like vi, vim, or nano to edit files. These editors allow you to modify the content of the file.vi and vim:vi and vim are powerful text editors available in most UNIX-based systems. Here’s how you can perform basic operations:
    • Open a File: To open a file named myfile.txt, use the command: vi myfile.txt or vim myfile.txt
    • Insert Mode: To enter insert mode (to start editing), press: i
    • Save and Exit: To save changes and exit, press Esc to leave insert mode, then type: :wq and press Enter
    • Exit Without Saving: To exit without saving changes, press Esc to leave insert mode, then type: :q! and press Enter
    • Other Useful Commands:
      • To save without exiting, type: :w and press Enter
      • To undo the last change, type: u
      • To delete a line, type: dd
    nano:nano is a simpler text editor that’s more user-friendly for beginners. Here’s how you can perform basic operations:
    • Open a File: To open a file named myfile.txt, use the command: nano myfile.txt
    • Insert Mode: nano starts in insert mode, so you can start typing immediately.
    • Save and Exit: To save changes and exit, press: Ctrl+X, then press Y to confirm saving, and press Enter to confirm the filename.
    • Exit Without Saving: To exit without saving changes, press: Ctrl+X, then press N to discard changes.
    • Other Useful Commands:
      • To save the file, press: Ctrl+O
      • To cut a line, press: Ctrl+K
      • To paste a line, press: Ctrl+U
      • To search within the file, press: Ctrl+W
  • Copying a File: Use the cp command to make copies of files.Example: To copy a file named myfile.txt to a new file named myotherfile.txt, use the command: cp myfile.txt myotherfile.txt This will create a copy of myfile.txt named myotherfile.txt.Options for cp:
    • -r: Recursively copy directories and their contents.
    • -i: Prompt before overwrite.
    • -v: Verbose mode, showing files as they are copied.
    Example: To copy a directory and its contents, use the command: cp -r mydir newdir
  • Moving a File: The mv command moves files from one location to another.Example: To move a file named myfile.txt to a new location, use the command: mv myfile.txt /path/to/new/location/ This will move the myfile.txt file to the specified location.Options for mv:
    • -i: Prompt before overwrite.
    • -v: Verbose mode, showing files as they are moved.
    • -f: Force move, overwriting without prompt.
    Example: To move a file and prompt before overwriting, use the command: mv -i myfile.txt /path/to/new/location/
  • Deleting a File: The rm command deletes files.Example: To delete a file named myfile.txt, use the command: rm myfile.txt This will delete the myfile.txt file.Options for rm:
    • -r: Recursively delete directories and their contents.
    • -f: Force deletion without prompt.
    • -i: Prompt before each removal.
    Example: To delete a directory and its contents without prompting, use the command: rm -rf mydirExample: To delete a file with a prompt before each removal, use the command: rm -i myfile.txt
  • Viewing File Contents: You can view the contents of a file using commands like cat, head, tail, more, and less.cat command:
    • cat filename: Displays the entire contents of a file.
    Example: To view the contents of myfile.txt, use the command: cat myfile.txthead command:
    • head filename: Displays the first 10 lines of a file by default.
    • head -n number filename: Displays the first number lines of a file.
    Example: To view the first 5 lines of myfile.txt, use the command: head -n 5 myfile.txttail command:
    • tail filename: Displays the last 10 lines of a file by default.
    • tail -n number filename: Displays the last number lines of a file.
    Example: To view the last 5 lines of myfile.txt, use the command: tail -n 5 myfile.txtmore command:
    • more filename: Displays file contents one screen at a time.
    Example: To view the contents of myfile.txt one screen at a time, use the command: more myfile.txt

less command:

  • less filename: Similar to more, but with more advanced navigation.

Example: To view the contents of myfile.txt with advanced navigation, use the command: less myfile.txt

Navigation in less:

  • Use Space to go to the next page.
  • Use b to go back a page.
  • Use q to quit.

File Permissions

In RHEL 9, file permissions are crucial for security and proper system functioning. Each file has three types of permissions for three types of entities:

  • Read (r): Permission to read the file.
  • Write (w): Permission to modify the file.
  • Execute (x): Permission to run the file as a program.

These permissions are set for three different entities:

  • Owner (u): The user who owns the file.
  • Group (g): The group that owns the file.
  • Others (o): All other users.

Numeric Values for Permissions

Permissions can also be set using numeric values where:

  • Read (r) is represented by the number 4
  • Write (w) is represented by the number 2
  • Execute (x) is represented by the number 1

To set permissions, you sum these values for each entity. For example:

  • Read + Write + Execute equals 4 + 2 + 1, which equals 7

Setting Permissions

You can set permissions using either numeric values or symbolic notation:

  • Numeric Method: The chmod command followed by three numbers representing the permissions for the owner, group, and others, respectively.Example: To give the owner full permissions (read, write, execute), group read and execute permissions, and others read-only permission, use the command: chmod 754 myfile.txt This sets the owner’s permissions to read, write, and execute (7), the group’s permissions to read and execute (5), and others’ permissions to read (4).
  • Symbolic Method: The chmod command followed by a combination of letters representing the entities and permissions.Example: To give the owner read, write, and execute permissions, group read and execute permissions, and others read-only permissions, use the command: chmod u+rwx,g+rx,o+r myfile.txt This sets the owner’s permissions to read, write, and execute (u+rwx), the group’s permissions to read and execute (g+rx), and others’ permissions to read (o+r).

Understanding numeric values simplifies permission management, especially in scenarios where complex permissions are needed. Practice setting permissions using both methods to solidify your understanding.

Share the Post:

Leave a Reply

Your email address will not be published. Required fields are marked *

Join Our Newsletter

Delivering Exceptional Learning Experiences with Amazing Online Courses

Join Our Global Community of Instructors and Learners Today!