網站首頁 編程語言 正文
1.文件夾結構
準備如下文件夾結構作為演示:
如E:\Code\Shell有如下文件夾結構,有3個相同文件test.txt
2.查找某文件夾下指定文件所在的路徑
find可以查找某個目錄下的指定文件(或目錄)所在的路徑
find 目錄名 -name 文件名
# 查找Shell文件夾下test.txt所在路徑
find Shell -name test.txt
執行結果:
Shell/a/test/test.txt
Shell/b/test/test.txt
Shell/c/test/test.txt
如果不指定目錄名,則是查找當前文件夾下的文件
# 查找當前文件夾下的test.txt所在路徑
find -name test.txt
執行結果:
./Shell/a/test/test.txt
./Shell/b/test/test.txt
./Shell/c/test/test.txt
3.批量刪除某個文件夾下的指定文件
刪除某個目錄下的指定文件(或目錄)
find 目錄名 -name 文件名 |xargs rm -rf
# 刪除Shell文件夾下所有test.txt
find Shell -name test.txt |xargs rm -rf
刪除test.txt后的文件夾結構如下
4.批量重命名某文件夾下指定的文件名
編寫腳本batch_rename_file.sh,內容如下:
# 批量重命名指定文件夾下的文件名或目錄名
oldFileName="test.txt" # 原文件名
newFileName="case.txt" # 新文件名
targetFolder="Shell" # 指定文件夾名
for filePath in `find $targetFolder -name $oldFileName`
do
dirPath=`dirname $filePath` # 文件所在目錄
mv $filePath $dirPath/$newFileName
echo "$filePath -> $dirPath/$newFileName"
done
執行腳本,結果如下:
Shell/a/test/test.txt -> Shell/a/test/case.txt
Shell/b/test/test.txt -> Shell/b/test/case.txt
Shell/c/test/test.txt -> Shell/c/test/case.txt
重命名test.txt后的文件夾結構如下:
5.批量將某文件夾下指定文件移至上級目錄
編寫腳本mv_file_to_upperLevel.sh,內容如下:
# 批量將指定文件夾下的文件或目錄,移至上級目錄
fileName="test.txt" # 文件名
targetFolder="Shell" # 指定文件夾名
for filePath in `find $targetFolder -name $fileName`
do
upperLevelDir=`dirname $(dirname $filePath)` # 上級目錄
mv $filePath $upperLevelDir
echo "$filePath -> $upperLevelDir/$fileName"
done
執行腳本,結果如下:
Shell/a/test/test.txt -> Shell/a/test.txt
Shell/b/test/test.txt -> Shell/b/test.txt
Shell/c/test/test.txt -> Shell/c/test.txt
移動test.txt至上級目錄后的文件夾結構如下:
原文鏈接:https://blog.csdn.net/aidijava/article/details/127080413
相關推薦
- 2023-04-06 C++中的多態問題—理解虛函數表及多態實現原理_C 語言
- 2022-12-24 React?組件的狀態下移和內容提升的操作方法_React
- 2022-07-04 如何用python實現結構體數組_python
- 2022-06-28 C#遞歸算法和排列算法_C#教程
- 2022-02-27 Error in render: “TypeError: Cannot read propertie
- 2022-07-07 在Kubernetes集群中搭建Istio微服務網格的過程詳解_云其它
- 2023-06-18 深入了解Golang中的Slice底層實現_Golang
- 2022-07-07 關于C++智能指針shared_ptr和unique_ptr能否互轉問題_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支