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
, ornano
to edit files. These editors allow you to modify the content of the file.vi and vim:vi
andvim
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
orvim 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
- To save without exiting, type:
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 pressY
to confirm saving, and press Enter to confirm the filename. - Exit Without Saving: To exit without saving changes, press:
Ctrl+X
, then pressN
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
- To save the file, press:
- Open a File: To open a file named
- Copying a File: Use the
cp
command to make copies of files.Example: To copy a file namedmyfile.txt
to a new file namedmyotherfile.txt
, use the command:cp myfile.txt myotherfile.txt
This will create a copy ofmyfile.txt
namedmyotherfile.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
mv
command moves files from one location to another.Example: To move a file namedmyfile.txt
to a new location, use the command:mv myfile.txt /path/to/new/location/
This will move themyfile.txt
file 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
rm
command deletes files.Example: To delete a file namedmyfile.txt
, use the command:rm myfile.txt
This will delete themyfile.txt
file.Options forrm
:-r
: Recursively delete directories and their contents.-f
: Force deletion without prompt.-i
: Prompt before each removal.
rm -rf mydir
Example: 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
.cat
command:cat filename
: Displays the entire contents of a file.
myfile.txt
, use the command:cat myfile.txt
head
command:head filename
: Displays the first 10 lines of a file by default.head -n number filename
: Displays the firstnumber
lines of a file.
myfile.txt
, use the command:head -n 5 myfile.txt
tail
command:tail filename
: Displays the last 10 lines of a file by default.tail -n number filename
: Displays the lastnumber
lines of a file.
myfile.txt
, use the command:tail -n 5 myfile.txt
more
command:more filename
: Displays file contents one screen at a time.
myfile.txt
one 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
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.