0% found this document useful (0 votes)
30 views10 pages

Week Broader Topic Topics Tools To Be Covered: Linux System Administration

This document discusses working with files and directories in Linux. It covers common Linux file types, such as regular files, directories, symbolic links, device files, pipes, sockets and special files. It also describes two commands for locating files - find and locate. Find allows complex searches based on various criteria, while locate searches a pre-built database, making it faster but less real-time. Understanding file types and using find/locate commands effectively is important for file management in Linux.

Uploaded by

Varinder Dhillon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views10 pages

Week Broader Topic Topics Tools To Be Covered: Linux System Administration

This document discusses working with files and directories in Linux. It covers common Linux file types, such as regular files, directories, symbolic links, device files, pipes, sockets and special files. It also describes two commands for locating files - find and locate. Find allows complex searches based on various criteria, while locate searches a pre-built database, making it faster but less real-time. Understanding file types and using find/locate commands effectively is important for file management in Linux.

Uploaded by

Varinder Dhillon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Linux System Administration Week 3

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

1. Working with Files & Directories: Linux file types


In Linux, files and directories form the foundation of the file system hierarchy and are essential
for organizing and managing data. Files are containers for data, ranging from text documents
and images to executables and configuration files. Directories, on the other hand, serve as
containers for organizing files and other directories in a hierarchical structure. Working with
files involves creating, modifying, moving, copying, and deleting them. Linux provides a wide
range of commands and utilities to perform these operations. For example, you can create a
new file using the `touch` command, edit it with a text editor like `vi` or `nano`, and copy it to
a different location using the `cp` command. Permissions can be set on files to control who can
read, write, or execute them. Directories help organize files into a structured system. You can
create directories using the `mkdir` command and navigate through them using commands like
`cd` (change directory) and `ls` (list files and directories). To move or rename files and
directories, you can use the `mv` command. Additionally, the `rm` command allows you to
remove files, while the `rmdir` command removes empty directories.

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:

1. Regular Files (Type: `-`):


- Regular files contain data and are the most common type of file in Linux.
- They can be text files, binary files, scripts, documents, or any other file type.
- Regular files can be opened and modified using appropriate applications or editors.

2. Directories (Type: `d`):


- To arrange files in a hierarchical arrangement, utilise directories.
- They might include additional files and directories.

- Directories are denoted by the 'd' in the file type column when listing files (`ls -l`).

3. Symbolic Links (Type: `l`):


- Symbolic links, also known as soft links or symlinks, are special files that point to another
file or directory.
- They act as shortcuts or references to the target file or directory.
- Symbolic links are denoted by the 'l' in the file type column when listing files (`ls -l`).

4. Device Files (Type: `b` or `c`):

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.

5. Pipes (Type: `p`):


- Pipes are special files that facilitate inter-process communication (IPC) between two or
more processes.
- They allow the output of one process to be directly connected to the input of another
process.
- Pipes are denoted by the 'p' in the file type column when listing files (`ls -l`).

6. Sockets (Type: `s`):


- Sockets are used for communication between processes on the same or different
computers over a network.
- They provide a mechanism for inter-process communication (IPC) using network protocols.
- Sockets are denoted by the 's' in the file type column when listing files (`ls -l`).

7. Special Files (Type: `s`):


- Special files are used for various system-related purposes and have unique characteristics.
- Examples include device files representing system hardware, pseudo-terminals (pty), or
system-specific files.
- Special files can have different types and are denoted by the 's' in the file type column when
listing files (`ls -l`).

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.

2. Find and Locate command: Working with Files & Directories


In Linux, the `find` and `locate` commands are commonly used for locating files and directories
in the file system. While both commands serve a similar purpose, they differ in terms of
functionality and underlying mechanisms. Here's a detailed explanation of `find` and `locate`:

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.

When to Use `find` vs. `locate`:


- Use `find` when you need to perform a comprehensive search based on specific criteria or
when real-time results are required.
- Use `locate` when you want a faster search and can work with potentially slightly outdated
results.
It's worth noting that the `find` and `locate` commands are versatile tools that can be
combined with other commands and options to refine and manipulate search results according
to your specific needs.

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).

2. Copying Files and Directories (cp):


- To duplicate files and directories, use the 'cp' command.

- To copy a file, use the following syntax: `cp source_file destination_file`.

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.

3. Removing Files and Directories (rm):


-In order to delete files and directories, use the 'rm' command.
- To remove a file, use the following syntax: `rm filename`.
- To remove a directory and its contents recursively, use the `-r` flag: `rm -r directoryname`.
- Be cautious when using `rm` as it permanently deletes files and directories without
confirmation. To avoid accidental deletions, use the `-i` flag for interactive mode.

4. Moving/Renaming Files and Directories (mv):


-The "mv" command can be used to move or rename files and directories.

- 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.

5. Creating Directories (mkdir):


- The mkdir command creates directories.
- To create a single directory, use the following syntax: `mkdir directoryname`.
- You can create multiple directories at once by separating their names with spaces: `mkdir
dir1 dir2 dir3`.
- Use the `-p` flag to create parent directories as needed, even if they don't exist: `mkdir -p
parent/child/grandchild`.

6. Removing Directories (rmdir):


-The 'rmdir' command can be used to delete empty folders.
- To remove an empty directory, use the following syntax: `rmdir directoryname`.
- If the directory is not empty, `rmdir` will not remove it. In such cases, use `rm -r` to remove
non-empty directories recursively.

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

5. Redirection, Files and directory permissions (chmod): Working on


Files & Directories in Linux
Here are detailed explanations of redirection and file and directory permissions (chmod) in
Linux:

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

```

3. Appending Output: To append the output of a command to an existing file instead of


overwriting it, you can use the `>>` operator. For example:

```
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

```

File and Directory Permissions (chmod) in Linux:


Access to files and directories is managed by Linux's permission system. To modify a file or
directory's permissions, use the 'chmod' command. Permissions are represented by three
types of users: the owner of the file, the group associated with the file, and other users. Here's
a breakdown of the permissions:

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.

6. File ownership commands (chown, chgrp) in Working on Files &


Directories in Linux
In Linux, the file ownership commands `chown` and `chgrp` are used to modify the ownership
and group ownership of files and directories. These commands allow you to change the user
and group associated with a file, granting or restricting access to certain users or groups. Here's
an overview of the `chown` and `chgrp` commands:

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.

Commonly used options:


- `-R` or `--recursive`: Recursively change the ownership of files and directories within the
specified directory.
- `-c` or `--changes`: Print a message for each changed file.
- `-v` or `--verbose`: Display detailed output, including file ownership changes.

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.

Commonly used options:


- `-R` or `--recursive`: Recursively change the group ownership of files and directories within
the specified directory.
- `-c` or `--changes`: Print a message for each changed file.
- `-v` or `--verbose`: Display detailed output, including group ownership changes.

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

You might also like