2017-03-08 117 views
-2

我目前有一些FTP上的IP攝像機圖像。例如,它們來自相機,格式爲192.168.1.50_01_20170308114736213_TIMING.jpg。Bash腳本重命名子目錄中的文件 - 基於文件名

我正在尋找一個批處理腳本來重命名這些文件的相機名稱,後面跟着圖像的時間和日期在DD/MM/YYYY_HHMM - 任何人都可以提供任何建議,我怎麼會去這個? 感謝

+1

你必須做的一個IP的方法對應一個相機名稱?另外,你到目前爲止嘗試過什麼? – Aserre

+1

'/'不是文件名中的有效字符,因此您必須更改爲其他分隔符 –

+0

我實際上在網上找到了一個腳本來嘗試和幫助 - 儘管我沒有真正理解它 - 它嘗試獲取exif信息 - 請參閱在這裏https://ubuntuforums.org/showthread.php?t=2328946 - 因爲沒有exif信息,讓文件下載格式如20170308 104742 – kiteboy

回答

0

這是我的腳本,作爲參數u使用目錄的路徑wtih JPG文件

前。 ./script.sh /家庭/用戶/ cam_photos

#!/bin/bash 

directory=$1 
temp_file=temp.txt 
new_file=new.txt 
filter2_out=filter2_out.txt 
filter3_out=filter3_out.txt 
name_out=out.txt 
old_names=old.txt 
new_names=new_names.txt 
command_to_do=command.txt 

function ReadFilenames() 
{ 
cd $directory 
ls -1 > $old_names 
ls -1 | sed 's/^[^_]*_//' | sed 's/^[^_]*_//' | sed 's/_TIMING//' > $temp_file 
} 

function Filter_1() 
{ 
awk -v clist='4 6 8 12' ' 
BEGIN { n = split(clist, cl,//) 
} 
{  for(i = n; i; i--) 
       $0 = substr($0, 1, cl[i]) " " substr($0, cl[i] + 1) 
     print 
}' $temp_file > $new_file 
} 

function Filter_2() 
{ 
awk '{print $3,$2,$1,$4}' $new_file > $filter2_out 
} 

function Filter_3() 
{ 
sed 's/ /_/g; s/$/.jpg/' $filter2_out > $filter3_out 
} 

function NameOutput() 
{ 
sed 's/$/.jpg/' $filter3_out > $name_out 
} 

function Make_command() 
{ 
sed 's/^/mv /' $old_names > $new_names 
sleep 1 
paste -d' ' $new_names $filter3_out > $command_to_do 
} 

function Run() 
{ 
bash $command_to_do 
} 

function Clear() 
{ 
rm $temp_file $new_file $filter2_out $filter3_out $name_out $old_names $new_names $command_to_do 
} 

ReadFilenames 
Filter_1 
Filter_2 
Filter_3 
NameOutput 
Make_command 
Run 
Clear 

此致名稱:

192.168.1.50_01_20170308114236213_TIMING.jpg 
192.168.1.50_01_20170308114336213_TIMING.jpg 
192.168.1.50_01_20170308114436213_TIMING.jpg 
192.168.1.50_01_20170308114536213_TIMING.jpg 
192.168.1.50_01_20170308114636213_TIMING.jpg 
192.168.1.50_01_20170308114736213_TIMING.jpg 

OUTPUT:

08_03_2017_1142.jpg 
08_03_2017_1143.jpg 
08_03_2017_1144.jpg 
08_03_2017_1145.jpg 
08_03_2017_1146.jpg 
08_03_2017_1147.jpg 
相關問題