我是一個嘗試編寫腳本的新手,該腳本利用包含不同文件夾(subj1,subj2,subj3 ...等)名稱的文本文件,遍歷每個文件夾並進入這些文件夾以提取完整路徑 (即/Users/desktop/subj1/animals/pig.jpg,/Users/desktop/subj1/animals/cow.jpg),然後將主題ID和兩個文件保存在3列中,分隔文本文件。 (例如,subj1 /Users/desktop/subj1/animals/pig.jpg /Users/desktop/subj1/animals/cow.jpg)等等。如何編寫通過不同文件夾循環的腳本並將PATH輸出保存到製表符分隔的文本文件中?
輸出將如下所示:
subj1 /Users/desktop/subj1/animals/pig.jpg /Users/desktop/subj1/animals/cow.jpg
subj2 /用戶/桌面/ subj2 /動物/ pig.jpg /用戶/桌面/subj2/animals/cow.jpg
我試過尋找答案,但沒有真正接近回答這個查詢..我也需要這樣做超過1000個文件夾所以嘗試手動創建文件是瘋狂的。
(編輯):我將不得不首先驗證文件是否存在於每個文件夾內。我使用文件夾名稱的文本文件創建了一個數組。這是我到目前爲止:
read -r -a array <<< ${subj_numbs}
array_error_file=()
for subj in "${array[@]}"
do
echo "Working on.." \"${subj}\"
pig_file=${dir}/${subj}/animals/pig.jpg
cow_file=${dir}/${subj}/animals/cow.jpg
if [ ! -f $pig_file ] && [ ! -f $cow_file ]; then
echo " [!] Files ${pig_file} and ${cow_file} do not exist."
array_error_file+=($pig_file)
array_error_file+=($cow_file)
else
echo "Writing path names to text file.. -> \"${subj}\"
pig_path="$pig_file"; pwd
cow_path="$cow_file"; pwd
請使用命令「tree -a」顯示文件夾結構。如果您沒有它,請安裝樹形包 –
您是否知道文件存在於每個文件夾中,還是您想檢查它們是否存在,然後編寫完整路徑?如果他們存在,任務只是組合字符串,不需要進入文件夾。如果你需要檢查他們 - 我會使用'find'並根據你的喜好來格式化輸出。 –
@dr_agon我已對我的帖子進行了更改,以便指定 –