如何移动扩展名为(.JPEG、.JPG、.JPEG、.JPG)的单个文件,并使用Linux bash将扩展名更改为.JPG
我有一个inotify wait脚本,每当它检测到文件已上载到源目录时,就会将文件从一个位置移动到另一个位置 我面临的挑战是,我需要保留文件的基本名称,并将以下扩展名转换为.JPEG、.JPG、.JPEG到.JPG,以便仅使用.JPG扩展名重命名文件 目前我有:如何移动扩展名为(.JPEG、.JPG、.JPEG、.JPG)的单个文件,并使用Linux bash将扩展名更改为.JPG,linux,bash,file-extension,file-rename,Linux,Bash,File Extension,File Rename,我有一个inotify wait脚本,每当它检测到文件已上载到源目录时,就会将文件从一个位置移动到另一个位置 我面临的挑战是,我需要保留文件的基本名称,并将以下扩展名转换为.JPEG、.JPG、.JPEG到.JPG,以便仅使用.JPG扩展名重命名文件 目前我有: TARGET="/target" SRC="/source" ( while [ 1 ] do inotifywait -m -r -e close_write --format %f -q \ $SRC |
TARGET="/target"
SRC="/source"
( while [ 1 ]
do inotifywait -m -r -e close_write --format %f -q \
$SRC | while read F
do mv "$SRC/$F" $TARGET
done
done ) &
因此,我需要一种方法来分离和测试这些非标准扩展名,并使用正确的扩展名移动文件。所有没有这4个扩展名的文件都会按原样移动
谢谢
Dave使用带有一些参数扩展的
extglob
选项:
#! /bin/bash
shopt -s extglob
TARGET=/target
SRC=/source
( while : ; do
inotifywait -m -r -r close_write --format %f -q \
$SRC | while read F ; do
basename=${F##*/} # Remove everything before /
ext=${basename##*.} # Remove everything before .
basename=${basename%.$ext} # Remove .$ext at the end
if [[ $ext == @(JPG|JPEG|jpeg) ]] ; then # Match any of the words
ext=jpg
fi
echo mv "$F" "$TARGET/$basename.$ext"
done
done ) &
使用带有一些参数扩展的
extglob
选项:
#! /bin/bash
shopt -s extglob
TARGET=/target
SRC=/source
( while : ; do
inotifywait -m -r -r close_write --format %f -q \
$SRC | while read F ; do
basename=${F##*/} # Remove everything before /
ext=${basename##*.} # Remove everything before .
basename=${basename%.$ext} # Remove .$ext at the end
if [[ $ext == @(JPG|JPEG|jpeg) ]] ; then # Match any of the words
ext=jpg
fi
echo mv "$F" "$TARGET/$basename.$ext"
done
done ) &
试试这种格式。(更新)
如果您发现echo
命令已经正确,请将其从mv
命令中删除。它也适用于bash
,但也可以与其他shell兼容。如果read
命令出错,请尝试删除-r
选项。尝试此格式。(更新)
如果您发现echo
命令已经正确,请将其从mv
命令中删除。它也适用于bash
,但也可以与其他shell兼容。如果read
命令出现错误,请尝试删除-r
选项
TARGET="/target"
SRC="/source"
(
while :; do
inotifywait -m -r -e close_write --format %f -q "$SRC" | while IFS= read -r F; do
case "$F" in
*.jpg)
echo mv "$SRC/$F" "$TARGET/" ## Move as is.
;;
*.[jJ][pP][eE][gG]|*.[jJ][pP][gG])
echo mv "$SRC/$F" "$TARGET/${F%.*}.jpg" ## Move with new proper extension.
;;
esac
done
done
) &