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, ornanoto edit files. These editors allow you to modify the content of the file.vi and vim:viandvimare 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.txtorvim myfile.txt - Insert Mode: To enter insert mode (to start editing), press:
i - Save and Exit: To save changes and exit, press
Escto leave insert mode, then type::wqand press Enter - Exit Without Saving: To exit without saving changes, press
Escto leave insert mode, then type::q!and press Enter - Other Useful Commands:
- To save without exiting, type:
:wand press Enter - To undo the last change, type:
u - To delete a line, type:
dd
- To save without exiting, type:
nanois 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:
nanostarts in insert mode, so you can start typing immediately. - Save and Exit: To save changes and exit, press:
Ctrl+X, then pressYto confirm saving, and press Enter to confirm the filename. - Exit Without Saving: To exit without saving changes, press:
Ctrl+X, then pressNto 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
- To save the file, press:
- Open a File: To open a file named
- Copying a File: Use the
cpcommand to make copies of files.Example: To copy a file namedmyfile.txtto a new file namedmyotherfile.txt, use the command:cp myfile.txt myotherfile.txtThis will create a copy ofmyfile.txtnamedmyotherfile.txt.Options forcp:-r: Recursively copy directories and their contents.-i: Prompt before overwrite.-v: Verbose mode, showing files as they are copied.
cp -r mydir newdir - Moving a File: The
mvcommand moves files from one location to another.Example: To move a file namedmyfile.txtto a new location, use the command:mv myfile.txt /path/to/new/location/This will move themyfile.txtfile to the specified location.Options formv:-i: Prompt before overwrite.-v: Verbose mode, showing files as they are moved.-f: Force move, overwriting without prompt.
mv -i myfile.txt /path/to/new/location/ - Deleting a File: The
rmcommand deletes files.Example: To delete a file namedmyfile.txt, use the command:rm myfile.txtThis will delete themyfile.txtfile.Options forrm:-r: Recursively delete directories and their contents.-f: Force deletion without prompt.-i: Prompt before each removal.
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, andless.catcommand:cat filename: Displays the entire contents of a file.
myfile.txt, use the command:cat myfile.txtheadcommand:head filename: Displays the first 10 lines of a file by default.head -n number filename: Displays the firstnumberlines of a file.
myfile.txt, use the command:head -n 5 myfile.txttailcommand:tail filename: Displays the last 10 lines of a file by default.tail -n number filename: Displays the lastnumberlines of a file.
myfile.txt, use the command:tail -n 5 myfile.txtmorecommand:more filename: Displays file contents one screen at a time.
myfile.txtone screen at a time, use the command:more myfile.txt
less command:
less filename: Similar tomore, 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
Spaceto go to the next page. - Use
bto go back a page. - Use
qto 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
chmodcommand 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.txtThis 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
chmodcommand 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.txtThis 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.