java使用hutools工具集,实现迁移centos中的文件,并且判断每一层目录的权限,看所有者是否是gnos用户,如果不是则变更所有者为gnos。文件和目录结构如下:/nas/ytdata/TM03/rawLog/2023/04/02/abc.txt

时间: 2023-10-19 17:17:05 浏览: 33
以下是使用hutools工具集实现迁移并变更权限的Java代码: ```java import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.StrUtil; import java.io.File; public class FileTransfer { public static void main(String[] args) { String srcPath = "/nas/ytdata/TM03/rawLog/2023/04/02/abc.txt"; String dstPath = "/mnt/data/abc.txt"; String owner = "gnos"; transferFile(srcPath, dstPath, owner); } public static void transferFile(String srcPath, String dstPath, String owner) { File srcFile = new File(srcPath); if (!srcFile.exists()) { System.out.println("源文件不存在"); return; } if (!srcFile.isFile()) { System.out.println("源文件不是普通文件"); return; } String srcDirPath = srcFile.getParent(); int startIndex = StrUtil.indexOfIgnoreCase(srcDirPath, "/rawLog/"); if (startIndex == -1) { System.out.println("源文件不在/rawLog/目录下"); return; } String dstDirPath = "/mnt/data" + srcDirPath.substring(startIndex); File dstDir = new File(dstDirPath); if (!dstDir.exists()) { FileUtil.mkdir(dstDirPath); } if (!dstDir.isDirectory()) { System.out.println("目标路径不是目录"); return; } String dstFilePath = dstDirPath + "/" + srcFile.getName(); File dstFile = new File(dstFilePath); if (dstFile.exists()) { System.out.println("目标文件已存在"); return; } FileUtil.copy(srcFile, dstFile, true); changeOwner(dstDir, owner); } public static void changeOwner(File dir, String owner) { if (!dir.exists() || !dir.isDirectory()) { return; } File[] files = dir.listFiles(); for (File file : files) { if (file.isDirectory()) { changeOwner(file, owner); } else { String fileOwner = FileUtil.getOwner(file).getUser(); if (!owner.equals(fileOwner)) { FileUtil.chown(file, owner); } } } } } ``` 其中,`transferFile`方法中,首先判断源文件是否存在,是否是普通文件,以及是否在`/rawLog/`目录下。如果满足条件,则计算目标路径,创建目标目录,复制文件到目标路径,并调用`changeOwner`方法变更所有者。`changeOwner`方法递归遍历目录下的所有文件和子目录,并判断每个文件的所有者是否为`gnos`,如果不是则变更所有者。

相关推荐

最新推荐

recommend-type

centos7限制普通用户访问单一目录.docx

要求给开发同事开设一个查看日志的账号,并限制其只能访问该目录下的单一文件
recommend-type

python修改linux中文件(文件夹)的权限属性操作

今天生成的对流云团路径图片放在linux下,文件的权限都是rw,没有x,后续的别人的程序调用不了,这里附上对三个属性的简单解释,有不够的欢迎大家补充 Linux的权限不是很细致,只有RWX三种 r(Read,读取):对文件而...
recommend-type

CentOS8中的nmcli使用详解

主要介绍了CentOS8的nmcli使用,基于RHEL8/CentOS8的nmcli常见命令使用,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Linux Centos 下使用yum 命令安装mysql实现步骤

主要介绍了Linux Centos 下使用yum 命令安装mysql实现步骤的相关资料,需要的朋友可以参考下
recommend-type

CentOS判断硬盘是SSD、HDD、还是U盘.docx

CentOS系统下判断硬盘是SSD、HDD、还是U盘,平时比较常用的运维命令,在这里记录一下,以免遗忘。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。