Week Broader Topic Topics Tools To Be Covered: Linux System Administration
Week Broader Topic Topics Tools To Be Covered: Linux System Administration
Topics to be covered:
Week Broader Topic Topics Tools to
be
covered
1. 1. Working with Files & Directories: Linux
file types
2.
3. 2. Find and Locate command: Working
with Files & Directories
4.
5. 3. (Changing Password) cp, rm, mv, mkdir,
rmdir: Working with Files & Directories
6.
7. 4.File Display Commands: cat, less, more,
3 Working with head, tail in Working on Files & Linux
Files & Directories in Linux Terminal
Directories 8.
9. 5. Redirection, Files and directory
permissions (chmod): Working on Files &
Directories in Linux
6. File ownership commands (chown,
chgrp) in Working on Files & Directories
in Linux
10.
11.
12.
13.
14.
15.
1
Linux System Administration Week 3
To view the contents of a file, you can use commands like `cat`, `less`, or `head` to display its
contents on the terminal. File searching can be done using tools such as `grep` and `find` to
locate specific files based on their content, name, or other attributes.
Linux file systems also support various special files and file types. Symbolic links provide
shortcuts to other files or directories, while device files represent hardware devices connected
to the system. Pipes enable inter-process communication, allowing the output of one process
to be connected to the input of another. Sockets facilitate network communication between
processes. These specialized file types contribute to the flexibility and power of the Linux
operating system.
In Linux, files and directories can have various types, each denoting different characteristics
and permissions. Here are some common file types you may encounter when working with
files and directories in Linux:
- Directories are denoted by the 'd' in the file type column when listing files (`ls -l`).
2
Linux System Administration Week 3
- Device files represent devices connected to the system, such as hard drives, USB devices,
or terminals.
- Block device files (Type: `b`) are used for random access devices, like hard drives.
- Character device files (Type: `c`) are used for sequential access devices, like terminals or
printers.
- The '/dev' directory is normally where device files are kept.
Understanding the different file types in Linux is important for managing and interacting with
files and directories effectively. You can identify the file type by using commands like `ls -l` to
list files and their properties or `file ` to determine the file type of a specific file.
1. `find` Command:
- The `find` command allows you to search for files and directories based on various criteria,
such as file name, type, size, modification time, and more.
- Syntax: `find [path] [expression]`
- The `[path]` specifies the starting directory for the search (default is the current directory).
- The `[expression]` consists of various options and tests used to refine the search.
- Examples:
- `find /home/user -name "*.txt"`: Search for files with the ".txt" extension in the
/home/user directory and its subdirectories.
- `find /var/log -size +1M -type f`: Find files larger than 1MB in the /var/log directory.
3
Linux System Administration Week 3
- `find /etc -mtime -7`: Locate files modified within the last 7 days in the /etc directory.
- The `find` command is powerful and flexible, allowing you to combine multiple search
criteria and perform complex operations on the found files, such as executing commands or
applying further actions using the `-exec` option.
2. `locate` Command:
- The `locate` command provides a quick way to search for files and directories using a pre-
built database called the "locate database."
- Syntax: `locate [options] [pattern]`
- The `[pattern]` represents the search pattern, which can be a partial file name or a regular
expression.
- The `locate` command searches the database for matching entries and displays the
corresponding file paths.
- Examples:
- `locate myfile.txt`: Search for files or directories containing "myfile.txt" in their names.
- `locate -i image.jpg`: Case-insensitive search for files or directories containing "image.jpg"
in their names.
- `locate /var/log/syslog`: Locate the specific file "/var/log/syslog" in the locate database.
- To ensure accurate results, the `locate` command relies on a periodically updated database.
You can update the database using the `updatedb` command (typically run as a cron job).
- Note that the `locate` command is faster than `find` as it searches an indexed database.
However, it may not always provide real-time results, as it relies on the database's last update.
3. (Changing Password) cp, rm, mv, mkdir, rmdir: Working with Files &
Directories
1. Changing Password (passwd):
- The `passwd` command allows users to change their password in Linux.
- To change your own password, simply type `passwd` in the terminal and follow the prompts
to enter your current password and set a new password.
- If you have administrative privileges, you can change the password for another user by
typing `passwd username` (replace "username" with the actual username).
4
Linux System Administration Week 3
- To copy a directory and its contents recursively, use the `-r` flag: `cp -r source_directory
destination_directory`.
- By default, `cp` does not preserve file attributes such as permissions and timestamps. Use
the `-p` flag to preserve them.
- To move a file or directory to a new location, use the following syntax: `mv source
destination`.
- To rename a file or directory, simply provide the new name as the destination.
- If the destination is an existing directory, the source file or directory will be moved into it.
Otherwise, the source file or directory will be renamed.
It's important to exercise caution when working with these commands, especially when
deleting files and directories, to avoid unintended data loss. Always double-check your
commands before executing them and be mindful of the files and directories you are
interacting with.
5
Linux System Administration Week 3
4.File Display Commands: cat, less, more, head, tail in Working on Files
& Directories in Linux
When working with files and directories in Linux, several commands are available for displaying
the contents of files. Here are some commonly used file display commands:
1. cat:
-The contents of one or more files can be concatenated and shown using the 'cat' command.
- It can also be used to create new files or append to existing files.
- Syntax: `cat [options] [file1] [file2] ...`
- Example: `cat file.txt` displays the contents of "file.txt" on the terminal.
2. less:
- The `less` command allows you to view file contents interactively, page by page.
- It provides navigation options to scroll through the file, search for specific text, and more.
- Syntax: `less [options] [file]`
- Example: `less file.txt` opens "file.txt" for interactive viewing.
3. more:
- Similar to `less`, the `more` command is used for viewing files page by page.
- It lacks some advanced features compared to `less` but is available on most Linux systems.
- Syntax: `more [options] [file]`
- Example: `more file.txt` displays "file.txt" page by page.
4. head:
- The 'head' command shows a file's first few lines.
- By default, it shows the first 10 lines, but you can specify a different number using the `-n`
option.
- Syntax: `head [options] [file]`
- Example: `head -n 5 file.txt` displays the first 5 lines of "file.txt".
5. tail:
-The 'tail' command shows a file's most recent few lines.
- By default, it shows the last 10 lines, but you can specify a different number using the `-n`
option.
- Syntax: `tail [options] [file]`
Example: The last 5 lines of "file.txt" are displayed, for instance, when "tail -n 5" is used.
These file display commands are handy for quickly examining file contents, viewing log files, or
extracting specific information.
6
Linux System Administration Week 3
Redirection in Linux:
Redirection is a powerful feature in Linux that allows you to control the input and output of
commands. It enables you to manipulate where command output goes and where command
input comes from. Here are some commonly used redirection operators:
1. Standard Output (stdout) Redirection: By default, the output of a command is sent to the
terminal. You can redirect it to a file using the `>` operator. For example:
```
command > file.txt
```
2. Standard Error (stderr) Redirection: Error messages generated by a command are sent to the
standard error stream (stderr). You can redirect them to a file using the `2>` operator. For
example:
```
command 2> error.txt
```
```
command >> file.txt
```
4. Redirecting Input: Instead of reading input from the keyboard, you can redirect it from a file
using the `<` operator. For example:
```
command < input.txt
```
5. Redirecting Output and Error: You can redirect both standard output and standard error to
separate files using the `&>` or `&>>` operator. For example:
7
Linux System Administration Week 3
```
command &> output.txt
command &>> output.txt
```
1. Read (r): Allows a user to view the contents of a file or list the files in a directory.
2. Write (w): Allows a user to modify or delete a file, as well as create or delete files within a
directory.
3. Execute (x): Allows a user to execute a file if it is a script or a binary executable. For
directories, it enables a user to access its contents.
Permissions are assigned to three user groups: owner, group, and others. The `chmod`
command uses a numeric or symbolic representation to set permissions. Here are some
examples:
1. Numeric Representation:
- A numerical number is assigned to each permission: Read (4), Write (2), and Execute (1).
- You add the values together to represent the desired permissions.
- For example, to give read and write permissions to the owner, and read-only permissions
to the group and others, you would use:
```
chmod 644 filename.txt
```
2. Symbolic Representation:
- The symbolic representation uses letters to represent the user groups and operators (+, -,
=) to modify permissions.
- For example, to give read and write permissions to the owner, and read-only permissions
to the group and others, you would use:
```
chmod u+rw,go+r filename.txt
```
8
Linux System Administration Week 3
To change the permissions of directories, you may need to use the `-R` option with `chmod` to
apply the changes recursively.
Understanding redirection and file and directory permissions is essential for effective file
management and control over data flow in Linux.
1. `chown` Command:
- Syntax: `chown [options] [new_owner] [file]`
- The 'chown' command is used to modify a file or directory's owner.
- The `new_owner` parameter specifies the new owner for the file.
- The `file` parameter represents the file or directory whose ownership needs to be changed.
Example:
- Change the owner of a file: `chown new_owner file.txt`
- Change the owner of a directory and its contents recursively: `chown -R new_owner
directory`
2. `chgrp` Command:
- Syntax: `chgrp [options] [new_group] [file]`
- To modify the group ownership of a file or directory, use the 'chgrp' command.
- The `new_group` parameter specifies the new group for the file.
- The `file` parameter represents the file or directory whose group ownership needs to be
changed.
Example:
- Change the group of a file: `chgrp new_group file.txt`
- Change the group of a directory and its contents recursively: `chgrp -R new_group directory`
9
Linux System Administration Week 3
Important considerations:
- Both `chown` and `chgrp` commands typically require root or superuser privileges to change
ownership or group ownership of files owned by other users.
- It's crucial to exercise caution while modifying ownership, as changing ownership improperly
can lead to permission issues and security vulnerabilities.
- Use the `ls -l` command to view the current ownership and group ownership of files and
directories.
Understanding and correctly using `chown` and `chgrp` commands can help you manage file
ownership and group ownership in Linux, ensuring appropriate access controls and
permissions for your files and directories.
10