linux - 检查 UNIX 目录中是否存在两个文件

标签 linux unix ksh

早上好

我正在尝试编写一个 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

编辑:

执行上述功能的更新脚本<​​/p>

#/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

希望对您有所帮助!

关于linux - 检查 UNIX 目录中是否存在两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28580355/

相关文章:

c++ - 不使用退格键替换 Linux 终端中的文本

java - C 或 Java 的 Linux 无线 API

bash - 在特定位置插入字符串(定长文件)

Linux 邮件命令行 : file input turns into attachment

c++ - 如何在 C/C++ linux 中实现类似 invokeOnMainThread() 的函数?

c - 在Windows和除Linux之外的其他Unice中实现mmap的 `MAP_POPULATE`标志,以及在Windows中实现MADV_WILLNEED

shell - 通过更改路径中的一个目录来更快地访问 Unix cd?

linux - 如何从字符类的范围中排除字符?

linux - 整合sed命令

linux - Shell 文件未从其他 Shell 文件调用