linux - Bash 脚本重命名子目录中的文件 - 基于文件名

标签 linux bash

我目前在 FTP 上有一些网络摄像机图像。例如,它们来自相机,格式为 192.168.1.50_01_20170308114736213_TIMING.jpg。

我正在寻找一个批处理脚本,将这些文件重命名为相机名称,后跟 DD/MM/YYYY_HHMM 中图像的时间和日期 - 谁能提供任何建议,我将如何解决这个问题? 谢谢

最佳答案

这是我的脚本,作为参数,您使用包含 jpg 文件的目录路径

ex. ./script.sh /home/user/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

输出:

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

关于linux - Bash 脚本重命名子目录中的文件 - 基于文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42672675/

相关文章:

perl - bash cgi 不会返回图像

regex - 删除换行符 (\n) 但排除具有特定正则表达式的行?

python - anaconda ipython 的路径是否中断?

linux - Postfix和Amazon S3

python - 操作系统如何处理 python 和 python 脚本的子进程......?

c - 套接字缓冲区大小不增加

bash get 在管道符号之前使用的命令

c - 是否需要关闭 systemv 消息队列

linux - 为什么 "echo l >/proc/sysrq-trigger"调用跟踪输出总是相似的?

linux - 将 'grep --color=auto' 结果写入文件时颜色信息消失