Perl Directory Operations
The following lists some standard functions for directory operations:
opendir DIRHANDLE, EXPR # 打开目录 readdir DIRHANDLE # 读取目录 rewinddir DIRHANDLE # 定位指针到开头 telldir DIRHANDLE # 返回目录的当前位置 seekdir DIRHANDLE, POS # 定位指定到目录的 POS 位置 closedir DIRHANDLE # 关闭目录
Display all files
Display all files in the directory. The following example uses the glob operator, as demonstrated below:
Example
The following example can list all files in the current directory:
Example
If you want to display all files ending with .c in the /tmp directory, you can use the following code:
Example
Create a new directory
We can use themkdirfunction to create a new directory. Before executing, you need to have sufficient permissions to create the directory:
Example
Delete a directory
We can use thermdirfunction to delete a directory. Sufficient permissions are required to perform this operation. Additionally, the directory to be deleted must be an empty directory:
Example
Change directory
We can use thechdirfunction to switch the current directory. Sufficient permissions are required to perform this operation. Example:
Example
Executing the above program, the output result is:
你现在所在的目录为 /homeOther extensions