[英]Check that two file exists in UNIX Directory
Good Morning, 早上好,
I am trying to write a korn shell script to look inside a directory that contains loads of files and check that each file also exists with .orig on the end. 我试图编写一个korn shell脚本,以查看包含文件负载的目录内部,并检查每个文件的末尾是否也带有.orig。
For example if a file inside the directory is called 'mercury_1' there must also be a file called 'mercury_1.orig' 例如,如果目录中的文件名为“ mercury_1”,则还必须有一个名为“ mercury_1.orig”的文件
If there isn't, it needs to move the mercury_1 file to another location. 如果没有,则需要将mercury_1文件移动到另一个位置。 However if the .orig file exists do nothing and move onto the next file.
但是,如果.orig文件存在,则不执行任何操作,然后移至下一个文件。
I am sure it is really simple but I am not that experienced in writing Linux scripts and help would be greatly appreciated!! 我敢肯定这确实很简单,但是我在编写Linux脚本方面经验不足,将不胜感激!
Here's a small ksh
snippet to check if a file exists in the current directory 这是一个
ksh
小片段,用于检查当前目录中是否存在文件
fname=mercury_1
if [ -f $fname ]
then
echo "file exists"
else
echo "file doesn't exit"
fi
Edit: 编辑:
The updated script that does the said functionality 具有上述功能的更新脚本
#/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
Hope its of help! 希望它的帮助!
You can use the below script 您可以使用以下脚本
script.sh : 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
Usage: 用法:
./script.sh <search directory> <destination dir if file not present>
Here, for each file with extension stripped check if "*.orig" is present, if not then move file to different directory, else do nothing. 在这里,对于每个带扩展名的文件,检查是否存在“ * .orig”,如果不存在,则将文件移动到其他目录,否则什么也不做。
Extension is stripped because you don't want to repeat the same steps for *.orig
files. 扩展名被删除,因为您不想对
*.orig
文件重复相同的步骤。
I tested this on OSX (basically mv should not differ to much from linux). 我在OSX上进行了测试(基本上mv与linux的差别不大)。 My test directory is zbar and destination is /tmp directory
我的测试目录是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.