網站首頁 編程語言 正文
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
相關推薦
- 2022-12-23 解讀pandas.DataFrame.corrwith_python
- 2023-05-29 docker如何對已經啟動的容器添加目錄映射(掛載目錄)_docker
- 2022-03-15 When allowCredentials is true, allowedOrigins cann
- 2022-05-01 Python類的常用高級函數匯總_python
- 2022-06-30 Python使用Pillow添加水印_python
- 2022-07-26 更新kali源并,安裝dirseach
- 2022-12-06 C#基礎教程之類class與結構struct的區別_C#教程
- 2022-05-09 Python的Pandas時序數據詳解_python
- 最近更新
-
- 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同步修改后的遠程分支