简体   繁体   English

Shell Bash将扩展名添加到没有一个文件

[英]Shell Bash Add extension to files without one

I am trying to create a scrits that can find files without an extension and add "html" to the end of it. 我正在尝试创建一个scrit,它可以查找不带扩展名的文件,并在其末尾添加“ html”。 I was able to create a small script to get anything without a "." 我能够创建一个小的脚本来获取没有“。”的任何内容。 in the end, however sometimes people have file with "." 最后,但是有时人们使用“。”作为文件。 in it's name without being related to it's extension and i don't known what to do about that. 它的名字与它的扩展名无关,我也不知道该怎么办。

Here's is an example: 这是一个例子:

File1.xml
File2.txt
File3
File.4
File5.sh
File6

This is what i have so far: 这是我到目前为止所拥有的:

#! /bin/bash

cd TestFiles
dir=$(pwd)

echo "Searching Files Without Extension"


array=$(find . -type f ! -name "*.*")

echo $array

for file in $array; do
    filename=$(basename "$file");
    echo $filename
    #extension=$(echo "$filename" | cut -d'.' -f2)
    mv -f $filename $filename".html"

done

ls

Again, the code above works but only if the file does not have a "." 同样,上面的代码有效,但前提是该文件没有“。”。 on it's name. 在它的名字上。

Simple and compatible with all POSIX-compliant shells: 简单且与所有POSIX兼容外壳兼容:

for f in *; do
  case $f in *.*) continue;; esac
  mv -- "$f" "${f}.html"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM