[英]Check that two file exists in UNIX Directory
早上好,
我试图编写一个korn shell脚本,以查看包含文件负载的目录内部,并检查每个文件的末尾是否也带有.orig。
例如,如果目录中的文件名为“ mercury_1”,则还必须有一个名为“ mercury_1.orig”的文件
如果没有,则需要将mercury_1文件移动到另一个位置。 但是,如果.orig文件存在,则不执行任何操作,然后移至下一个文件。
我敢肯定这确实很简单,但是我在编写Linux脚本方面经验不足,将不胜感激!
这是一个ksh
小片段,用于检查当前目录中是否存在文件
fname=mercury_1
if [ -f $fname ]
then
echo "file exists"
else
echo "file doesn't exit"
fi
编辑:
具有上述功能的更新脚本
#/usr/bin/ksh
if [ ! $# -eq 1 ]
then
echo "provide dir"
exit
fi
dir=$1
cd $dir
#process file names not ending with orig
for fname in `ls | grep -v ".orig$"`
do
echo processing file $fname
if [ -d $fname ] #skip directory
then
continue
fi
if [ -f "$fname.orig" ] #if equiv. orig file present
then
echo "file exist"
continue
else
echo "moving"
mv $fname /tmp
fi
done
希望它的帮助!
您可以使用以下脚本
script.sh:
#!/bin/sh
if [ ! $# -eq 2 ]; then
echo "error";
exit;
fi
for File in $1/*
do
Tfile=${File%%.*}
if [ ! -f $Tfile.orig ]; then
echo "$File"
mv $File $2/
fi
done
用法:
./script.sh <search directory> <destination dir if file not present>
在这里,对于每个带扩展名的文件,检查是否存在“ * .orig”,如果不存在,则将文件移动到其他目录,否则什么也不做。
扩展名被删除,因为您不想对*.orig
文件重复相同的步骤。
我在OSX上进行了测试(基本上mv与linux的差别不大)。 我的测试目录是zbar,目标是/ tmp目录
#!/bin/bash
FILES=zbar
cd $FILES
array=$(ls -p |grep -v "/") # we search for file without extension so put them in array and ignore directory
echo $array
for f in $array #loop in array and find .orig file
do
#echo $f
if [ -e "$f.orig" ]
then
echo "found $f.orig"
else
mv -f "$f" "/tmp"
fi
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.