DEV Community

ta-lim
ta-lim

Posted on

Management File

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 Example:
$ mkdir Latihan or $ mkdir latihan1 latihan2 latihan3
Enter fullscreen mode Exit fullscreen mode
  • Create a directory with subdiectories using the command: $ mkdir –p // Example:
$ mkdir –p OS/pratikum1/tugas1
Enter fullscreen mode Exit fullscreen mode
  • Create an empty file using the command: $ touch Example:
$ touch file1 file2 file3 file4
Enter fullscreen mode Exit fullscreen mode
  • Create an empty file with a specific extension using the command: $ touch . Example:
$ touch file1.txt
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

2. Write

  • Write content to a file using an editor such as Nano or Vim. Example:
$ nano file1
Enter fullscreen mode Exit fullscreen mode

Image description

  • Write content to a file using the echo command: $ echo “text” > Example:
$ echo “isi konten di sini” > draft
Enter fullscreen mode Exit fullscreen mode

Image description

3. Read

To read the contents of a file, use the command "cat "
Example:

$ cat draft
Enter fullscreen mode Exit fullscreen mode

Image description

4. Delete

  • To remove a file, use the command "rm " Example:
$ rm fill1
Enter fullscreen mode Exit fullscreen mode

Image description

  • If you want to remove a directory, make sure the directory is empty, and then use the command "rmdir " Example:
$ rmdir latihan3
Enter fullscreen mode Exit fullscreen mode

Image description

  • If you want to remove a directory and its contents recursively, use the command "rm -r " Example:
$ rm -r /home/ta-lim/kuliah/SO/pratikum1/tugas1
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

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

$ locate <keyword>
$ locate -i latihan
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

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

- Copy and Move files/directories

Use the command

$ cp <file name> <destination directory>
$ cp tugaskuliah /home/ta-lim/kuliah/SO
Enter fullscreen mode Exit fullscreen mode

to copy files/directories
Image description

Use the command

$ mv <file name> <destination directory>
Enter fullscreen mode Exit fullscreen mode

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 (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
Enter fullscreen mode Exit fullscreen mode

to delete directories recursively

Image description

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)
Enter fullscreen mode Exit fullscreen mode

Image description

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)
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

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)
Enter fullscreen mode Exit fullscreen mode

Image description

Set access for a file

$ chmod 440 soal1
Enter fullscreen mode Exit fullscreen mode

Image description

Reference

Difference between FAT32 and NTFS
Structures of Directory in Operating System

Top comments (0)