簡體   English   中英

重命名子目錄中文件的 Bash 腳本 - 基於文件名

[英]Bash Script to Rename Files In Subdirectory - Based on file name

我目前在 FTP 上有一些 IP 攝像機圖像。 例如,它們以 192.168.1.50_01_20170308114736213_TIMING.jpg 格式從相機傳入。

我正在尋找一個批處理腳本來將這些文件重命名為相機名稱,然后是 DD/MM/YYYY_HHMM 中圖像的時間和日期 - 任何人都可以提供任何建議,我將如何處理這個問題? 謝謝

這是我的腳本,作為參數,你使用帶有 jpg 文件的目錄的路徑

前任。 ./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

暫無
暫無

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

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