簡體   English   中英

將腳本文件重命名為目錄名稱的Shell腳本

[英]Shell Script to Rename an Image File To the Directory Name

我將圖像文件放置在目錄中:

"/root/Desktop/my test dir/image.jpg"

我需要重命名圖像文件,如下所示(使用一些shell腳本):

image.jpg => "my test dir.jpg"

注意:此目錄中只有一個映像文件

有人可以給我一些提示嗎?

謝謝。

dir='/root/Desktop/my test dir/'

dirBase=$(basename -- "$dir") # should be equal to 'my test dir'
file=$(echo "$dir/"*) # things will break if your directory has more than one file
fileExtension=${file##*.} # 'jpg' in your case
mv -- "$file" "$dir/$dirBase.$fileExtension"

采用:

mv "/root/Desktop/my test dir/image.jpg" "/root/Desktop/my test dir/my test dir.jpg"

參數替換。

echo "${pathname##*/}"
echo "${pathname%.*}"

你可以做:

x="/root/Desktop/my test dir/image.jpg"

IFS="/" arr=( "$x" )
mv "$x" "${arr[1]}/${arr[2]}/${arr[3]}/${arr[3]}.${arr[4]##*.}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM