Management File
Tags: ubuntu, systems, operations, management
Published: 2023-03-02 02:45:32

## File Management 
Is the process of managing and storing data on storage devices such as magnetic disks, magnetic tapes, and optical disks.

###  Types of files
 that can be stored include numeric data, character data, binary data, as well as programs such as source programs, object programs, and executable programs.

### The tasks and functions of file management

a. Meeting the needs of data management and users.
b. Ensuring data validity.
c. Optimizing system performance and user response time.
d. Providing I/O support for data storage devices.

### File management is also responsible for:
a. Minimizing the possibility of data loss or damage.
b. Providing standard I/O interfaces.
c. Providing I/O support for multiple users.
d. Creating, modifying, and deleting files.

#### Additionally, file management must provide mechanisms for:
 Concurrent file usage.
b. Data backup and recovery.
c. Maintaining the confidentiality and security of information.
d. Providing a user-friendly interface for users.

### Table of differences between FAT and NTFS
 Feature | FAT | NTFS |
| --------- |:---------:| -------:|
|File and folder access| No complex permissions | Complex permissions with different levels of access |
| Journaling  | No | Yes |
| Partition size | Up to 32 GB | Up to several terabytes |
| File and folder encryption | No | Yes (using EPS) |
| File compression | No| Yes |
| Deleted file recovery | Third-party software required | Built-in feature for internal recovery |
| Symbolic links | Not supported | Supported (symbolic link, hard link, and junction) |
| Reliability | More prone to corruption and requires more time to check and fix errors | More resilient and has mechanisms for automatic partition error fixing |

##### Note 
that the decision to use FAT or NTFS depends on specific usage needs and conditions. If you only need a small partition to store files or data, FAT may be a good choice. However, if you require advanced features such as encryption and complex permissions, NTFS may be a better choice.

### Table comparing Structured Tree Directory and Acyclic Graph Directory
Feature | Structured Tree Directory | Acyclic Graph Directory |
| --------- |:---------:| -------:|
|Structure| Uses a tree structure | Uses a graph structure |
| Files and Directories  | Does not allow files and directories to be used together | Allows files and directories to be used together|
|Path Names| Each file has a unique path name | The same file or subdirectory may exist in multiple directories|
|Security| More secure since files and directories cannot be used together | More vulnerable since files and directories can be used together and exist in multiple directories |
|Simplicity| More simple since it uses a tree structure | More complex since it uses a graph structure and allows files and directories to be used together |
|Usage| Suitable for systems that require security and clear structure organization | Suitable for systems that require flexibility and the ability to use files and directories together |

##### Note 
the decision to use either directory structure depends on the specific needs and purposes of the system.

### Operations on files and directories

#### 1. Create
- Create a new directory using the command: $ mkdir <directory name>
  Example: 

```
$ mkdir Latihan or $ mkdir latihan1 latihan2 latihan3
```
- Create a directory with subdiectories using the command: $ mkdir –p <directory name>/<subdirectory name>/<subdirectory name>
Example: 

```
$ mkdir –p OS/pratikum1/tugas1
```
- Create an empty file using the command: $ touch <file name>
Example: 

```
$ touch file1 file2 file3 file4
```
- Create an empty file with a specific extension using the command: $ touch <file name>.<extension>
Example: 

```
$ touch file1.txt
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vn6pajbpljoizhlfwvx.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d80qzjf775nnnvi7o9iv.png)

#### 2. Write
- Write content to a file using an editor such as Nano or Vim.
Example: 

```
$ nano file1
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q4rl0pishz4nwh2kcxcb.png)

- Write content to a file using the echo command: $ echo “text” > <file name>
Example: 

```
$ echo “isi konten di sini” > draft
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjcn1bdmqroqwtk1jewb.png)

#### 3. Read
To read the contents of a file, use the command "cat <file name>"
Example: 

```
$ cat draft
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wuy4nv5whk3m9y0xzcdq.png)

#### 4. Delete
- To remove a file, use the command "rm <file name>"
  Example: 

```
$ rm fill1
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgpexvlclj2jrz7d0wbc.png)

- If you want to remove a directory, make sure the directory is empty, and then use the command "rmdir <directory name>"
  Example: 

```
$ rmdir latihan3
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fo977tf20ajw0p5fl26x.png)

- If you want to remove a directory and its contents recursively, use the command "rm -r <directory name>"
   Example: 

```
$ rm -r /home/ta-lim/kuliah/SO/pratikum1/tugas1
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e9obeptgf2io0untfkcr.png)


#### 5. Reposition
Searching files or directories can be done using the "find" command Files can be searched based on their name, type, or time
Examples:

```
$ find . -name "text"
$ find . -name latihan_nengok
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rs6an2x87ylg5sqh5yz0.png)

The "locate" command can also be used for searching, with examples like:
        

```
$ locate <keyword>
$ locate -i latihan
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jjkasnu36xo9p8ap2sm.png)


#### 6. Truncate
Truncate is used to change the size of a file without changing its attributes
The file is resized to zero but still remains available
The "truncate" operation can be done using 

```
the command: truncate –s <requested size> <filename>
Example: truncate –s 0 file1
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/boaxsfp1tk1icr5boikq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5snhwtuj1zylt278z13p.png)

Partial content can be deleted without opening the file itself
**Example:** file1 becomes zero-sized without deleting the file or its contents

### File and directory manipulation
####- View directory listings
Use the command `$ pwd` to know the current directory

Use the command `$ cd` to change directory, for example `$ cd ta-lim`

Use the command `$ cd .. `to move to the previous directory

Use the command `$ cd /` to move to the root directory

Use the command `$ ls` to view the contents of the directory

Use the command `$ ls -l` to view the contents of the directory in list format

Use the command `$ ls -a` to view the contents of the directory including hidden files and directories

Use the command `$ ls -aL` to view the contents of the directory recursively


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1wolcek1hhz9ofp6ebf.png)

####- Copy and Move files/directories
Use the command 
```
$ cp <file name> <destination directory>
$ cp tugaskuliah /home/ta-lim/kuliah/SO
```
 to copy files/directories
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6iwupcuxj93qzxioxdy.png)

Use the command 
```
$ mv <file name> <destination directory>
```
to move files/directories or rename files/directories


#### - Recursion on files and directories
In the context of delete operations, we discussed how to delete an empty directory in a previous discussion.
Now, we will discuss how to delete a directory that still contains files inside. This requires a recursive approach.Recursion is a problem-solving method where the solution depends on another solution that is smaller in scope and part of the same problem.
To delete files, directories, and subdirectories recursively, we use the command: $ rm -r <directory> (in some cases, root access may be required).

The -r option is used to delete files recursively. For example, to delete the file tugas1 that we previously created, we can run the command and see that the tugas1 folder and its contents have been deleted successfully.

Use the command `$ rm <file name> to delete files`
Use the command 
```
$ rm -r <directory name>
$ rm -r /home/ta-lim/kuliah/SO/pratikum1/tugas1
```
to delete directories recursively

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e9obeptgf2io0untfkcr.png)

### Access rights
Access rights on Ubuntu Server can be configured through ownership, permission, and group settings on files or directories. Here is a summary of access rights on Ubuntu Server:
#### Ownership
Each file or directory has an owner (user) and an owning group (group).The owner can access and modify the file or directory, and the owning group can have certain access rights.Ownership can be set using the `$ chown` and `$ chgrp` commands.

#### Permission
Each file or directory has three types of access rights: read, write, and execute. Access rights can be granted to the owner, owning group, and other users.Permissions can be set using the `$ chmod` command.

#### Group
Files or directories can be grouped into a group, and access rights can be granted to that group. Users can be added to a group to gain certain access rights.Group settings can be configured using the `$ addgroup` and `$ usermod` commands.

#### Superuser (root)
The superuser has full access rights to the system.Users with superuser access can configure access rights for other users and the system as a whole. Users can use the `$ sudo` command to gain access as the superuser.

### Change file or directory permissions
Use the chmod command with the format ``$ chmod [mode][file/directory] ``. The mode can be in numeric notation (**e.g. 744**) or symbolic notation (**e.g. rwxr--r--**).The numeric and symbolic notations represent the permissions for owner, group, and others, each with a value of 0-7. The order of the permissions is **read (r), write (w), and execute (x)**.
```
Example:$ chmod 744 Latihan or $ chmod u=rw,g=r,o=r Latihan
(give read-write permission to owner and read-only permission to group and others)
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wg0ev48mahgz9ap5lziq.png)


### Changing Ownership of Files and Directories
Use the chown command with the format `` $ chown [owner]:[group] [file/directory]``.
Owner is the name of the new owner and group is the name of the new group.
```
    Example: $ chown lim:lim Latihan 
(change the owner to john and the group to users)
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lfhbo7ivmlmpveekhcgi.png)


### Example Case
In a job interview for a System Administrator position, you are asked to create two users that represent your identity (**e.g. user1=first name; user2=last name**). For each user, create a directory named Ujian1 (**user1**) and Ujian2 (**user2**). Then, **create 3 files** with a size of 100 bytes simultaneously in the Ujian1 directory, and create 3 files with a size of 200 bytes simultaneously in the Ujian2 directory. **Swap the ownership** of the Ujian1 directory to User2 and vice versa. Then, **set the access permission** of one file (of your choice) to be readable only by the user and group who own the file, but not readable by other users.

#### Create User, Directory, and  File
```
$ sudo adduser muhamad (user1)
$ sudo adduser talim (user2)

$ mkdir Ujian1 (user muhamad)
$ mkdir Ujian2 (user talim)

$ touch soal4 soal5 soal6 (user muhamad) 
$ touch soal1 soal2 soal3 (user talim)

$ truncate -s 100 soal4 soal5 soal6
$ truncate -s 100 soal1 soal2 soal3
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r92v2xgnw6lsk1xy3oux.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fyve99w6kmsn09x90x5n.png)

#### Swap the ownership directory
The "chown" command in Linux is used to change the ownership of files and directories. Only the root user or a user with sudo privileges can change the ownership of a file or directory. This is because changing ownership can have significant security implications, and only privileged users should be able to make such changes.

When a file or directory is created, it is assigned an owner and a group. The owner is the user who created the file or directory, and the group is a set of users who have access to the file or directory. The "chown" command allows you to change the owner and group of a file or directory to any other user or group on the system, as long as you have the necessary permissions.

```
$ sudo chown talim Ujian1 (set access in dir Ujian1 to user talim)
$ sudo chown muhamad Ujian2 (set access in dir Ujian2 to user muhamad)
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxlh7xnhj3j0itdsj71l.png)

#### Set access for a file
```
$ chmod 440 soal1
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ck5uhexixjuiqbhg38rl.png)

##Reference
[Difference between FAT32 and NTFS](https://www.geeksforgeeks.org/difference-between-fat32-and-ntfs/)
[Structures of Directory in Operating System](https://www.geeksforgeeks.org/structures-of-directory-in-operating-system/?ref=gcse)