Shell獲取某個文件夾下的所有文件名(含文件夾)


1. 獲取某個文件夾下的所有文件名(含文件夾),並顯示

#!/bin/sh 
#============ get the file name =========== 
Folder_A="/home/youname/shell/gotfilename/bin" 
for file_a in ${Folder_A}/*
do 
temp_file=`basename $file_a` 
echo $temp_file 
done

2、若要去掉文件名的后綴(假如該文件夾下的所有文件為.txt格式),則代碼為

#!/bin/sh 
#============ get the file name =========== 
Folder_A="/home/youname/shell/gotfilename/bin" 
for file_a in ${Folder_A}/*
do 
temp_file=`basename $file_a .txt` 
echo $temp_file 
done

3、如果要輸出到一個文件的話也可以重定向到一個文件中去

#!/bin/sh 
#============ get the file name =========== 
Folder_A="/home/Neo/shell/gotfilename/bin" 
Output_file="output.txt" 
#這里用於清空原本的輸出文件,感覺 : 這個符號用處挺大,shell的學習還是要多用才是 
: > $Output_file 
for file_a in ${Folder_A}/*
do 
temp_file=`basename $file_a` 
echo $temp_file >> $Output_file 
done

4、增加了交互性

#!/bin/sh 
#============ get the file name =========== 
echo -e "請輸入你要讀取的文件夾路徑\n當前路徑為${PWD}" 
read InputDir 
echo "你輸入的文件夾路徑為${InputDir}" 
echo -e "請輸入你要將數據輸出保存的文件路徑n當前路徑為${PWD}" 
read OutputFile 
echo "輸出保存的文件路徑為${OutputFile}" 
: > $OutputFile #清空OutputFile 
#循環讀取文件夾名 
for file_a in ${InputDir}/*
do 
temp_file=`basename $file_a` 
echo $temp_file >> $OutputFile 
done

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM