2017-03-08 125 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 
相关问题