如何在bash脚本中检查文件名的扩展名?

时间:2022-10-04 00:12:13

I am writing a nightly build script in bash.
Everything is fine and dandy except for one little snag:

我正在用bash编写一个每晚构建脚本。一切都很好,花花公子除了一点点障碍:


#!/bin/bash

for file in "$PATH_TO_SOMEWHERE"; do
      if [ -d $file ]
      then
              # do something directory-ish
      else
              if [ "$file" == "*.txt" ]       #  this is the snag
              then
                     # do something txt-ish
              fi
      fi
done;

My problem is determining the file extension and then acting accordingly. I know the issue is in the if-statement, testing for a txt file.

我的问题是确定文件扩展名,然后相应地采取行动。我知道问题出在if语句中,测试txt文件。

How can I determine if a file has a .txt suffix?

如何确定文件后缀是否为.txt?

10 个解决方案

#1


167  

I think you want to say "Are the last four characters of $file equal to .txt?" If so, you can use the following:

我想你想说“$ file的最后四个字符是否等于.txt?”如果是这样,您可以使用以下内容:

if [ ${file: -4} == ".txt" ]

Note that the space between file: and -4 is required, as the ':-' modifier means something different.

请注意,file:和-4之间的空格是必需的,因为': - '修饰符意味着不同的东西。

#2


191  

Make

使

if [ "$file" == "*.txt" ]

like this:

喜欢这个:

if [[ $file == *.txt ]]

That is, double brackets and no quotes.

也就是说,双括号,没有引号。

The right side of == is a shell pattern. If you need a regular expression, use =~ then.

==的右侧是一个shell模式。如果需要正则表达式,请使用=〜then。

#3


23  

You can use the "file" command if you actually want to find out information about the file rather than rely on the extensions.

如果您确实想要查找有关该文件的信息而不是依赖于扩展,则可以使用“file”命令。

If you feel comfortable with using the extension you can use grep to see if it matches.

如果您对使用扩展程序感到满意,可以使用grep查看它是否匹配。

#4


23  

You just can't be sure on a Unix system, that a .txt file truly is a text file. Your best bet is to use "file". Maybe try using:

你不能确定在Unix系统上,.txt文件确实是一个文本文件。你最好的选择是使用“文件”。也许尝试使用:

file -ib "$file"

Then you can use a list of MIME types to match against or parse the first part of the MIME where you get stuff like "text", "application", etc.

然后,您可以使用MIME类型列表来匹配或解析MIME的第一部分,您可以在其中获取“text”,“application”等内容。

#5


12  

You could also do:

你也可以这样做:

   if [ "${FILE##*.}" = "txt" ]; then
       # operation for txt files here
   fi

#6


8  

Similar to 'file', use the slightly simpler 'mimetype -b' which will work no matter the file extension.

与'file'类似,使用稍微简单的'mimetype -b',无论文件扩展名是什么,都可以使用。

if [ $(mimetype -b "$MyFile") == "text/plain" ]
then
  echo "this is a text file"
fi

Edit: you may need to install libfile-mimeinfo-perl on your system if mimetype is not available

编辑:如果mimetype不可用,您可能需要在系统上安装libfile-mimeinfo-perl

#7


3  

I wrote a bash script that looks at the type of a file then copies it to a location, I use it to look through the videos I've watched online from my firefox cache:

我写了一个bash脚本,查看文件的类型,然后将其复制到一个位置,我用它来查看我在火狐缓存中在线观看的视频:

#!/bin/bash
# flvcache script

CACHE=~/.mozilla/firefox/xxxxxxxx.default/Cache
OUTPUTDIR=~/Videos/flvs
MINFILESIZE=2M

for f in `find $CACHE -size +$MINFILESIZE`
do
    a=$(file $f | cut -f2 -d ' ')
    o=$(basename $f)
    if [ "$a" = "Macromedia" ]
        then
            cp "$f" "$OUTPUTDIR/$o"
    fi
done

nautilus  "$OUTPUTDIR"&

It uses similar ideas to those presented here, hope this is helpful to someone.

它使用与此处提供的相似的想法,希望这对某人有帮助。

#8


2  

I guess that '$PATH_TO_SOMEWHERE'is something like '<directory>/*'.

我猜'$ PATH_TO_SOMEWHERE'就像' / *'。

In this case, I would change the code to:

在这种情况下,我会将代码更改为:

find <directory> -maxdepth 1 -type d -exec ... \;
find <directory> -maxdepth 1 -type f -name "*.txt" -exec ... \;

If you want to do something more complicated with the directory and text file names, you could:

如果您想对目录和文本文件名做一些更复杂的事情,您可以:

find <directory> -maxdepth 1 -type d | while read dir; do echo $dir; ...; done
find <directory> -maxdepth 1 -type f -name "*.txt" | while read txtfile; do echo $txtfile; ...; done

If you have spaces in your file names, you could:

如果文件名中有空格,则可以:

find <directory> -maxdepth 1 -type d | xargs ...
find <directory> -maxdepth 1 -type f -name "*.txt" | xargs ...

#9


2  

The correct answer on how to take the extension available in a filename in linux is :

关于如何在linux中使用文件名中的扩展名的正确答案是:

${strFileName##*\\.} 

Example of printing all file extensions in a directory

打印目录中的所有文件扩展名的示例

for fname in $(find . -maxdepth 1 -type f) # only regular file in the current dir
    do  echo ${fname##*\.} #print extensions 
done

#10


1  

My take on it with cut

我对它进行切割

>cut -d'.' -f2<<<"hi_mom.txt"
txt

My take on it with awk would be something like the following.

我对awk的看法将如下所示。

>MY_DATA_FILE="my_file.sql"
>FILE_EXT=$(awk -F'.' '{print $NF}' <<< $MY_DATA_FILE)
>if [ "sql" = "$FILE_EXT" ]
>then
>   echo "file is sql"
>fi

>awk -F'.' '{print $NF}' <<eof
>hi_mom.txt
>my_file.jpg
>eof

#1


167  

I think you want to say "Are the last four characters of $file equal to .txt?" If so, you can use the following:

我想你想说“$ file的最后四个字符是否等于.txt?”如果是这样,您可以使用以下内容:

if [ ${file: -4} == ".txt" ]

Note that the space between file: and -4 is required, as the ':-' modifier means something different.

请注意,file:和-4之间的空格是必需的,因为': - '修饰符意味着不同的东西。

#2


191  

Make

使

if [ "$file" == "*.txt" ]

like this:

喜欢这个:

if [[ $file == *.txt ]]

That is, double brackets and no quotes.

也就是说,双括号,没有引号。

The right side of == is a shell pattern. If you need a regular expression, use =~ then.

==的右侧是一个shell模式。如果需要正则表达式,请使用=〜then。

#3


23  

You can use the "file" command if you actually want to find out information about the file rather than rely on the extensions.

如果您确实想要查找有关该文件的信息而不是依赖于扩展,则可以使用“file”命令。

If you feel comfortable with using the extension you can use grep to see if it matches.

如果您对使用扩展程序感到满意,可以使用grep查看它是否匹配。

#4


23  

You just can't be sure on a Unix system, that a .txt file truly is a text file. Your best bet is to use "file". Maybe try using:

你不能确定在Unix系统上,.txt文件确实是一个文本文件。你最好的选择是使用“文件”。也许尝试使用:

file -ib "$file"

Then you can use a list of MIME types to match against or parse the first part of the MIME where you get stuff like "text", "application", etc.

然后,您可以使用MIME类型列表来匹配或解析MIME的第一部分,您可以在其中获取“text”,“application”等内容。

#5


12  

You could also do:

你也可以这样做:

   if [ "${FILE##*.}" = "txt" ]; then
       # operation for txt files here
   fi

#6


8  

Similar to 'file', use the slightly simpler 'mimetype -b' which will work no matter the file extension.

与'file'类似,使用稍微简单的'mimetype -b',无论文件扩展名是什么,都可以使用。

if [ $(mimetype -b "$MyFile") == "text/plain" ]
then
  echo "this is a text file"
fi

Edit: you may need to install libfile-mimeinfo-perl on your system if mimetype is not available

编辑:如果mimetype不可用,您可能需要在系统上安装libfile-mimeinfo-perl

#7


3  

I wrote a bash script that looks at the type of a file then copies it to a location, I use it to look through the videos I've watched online from my firefox cache:

我写了一个bash脚本,查看文件的类型,然后将其复制到一个位置,我用它来查看我在火狐缓存中在线观看的视频:

#!/bin/bash
# flvcache script

CACHE=~/.mozilla/firefox/xxxxxxxx.default/Cache
OUTPUTDIR=~/Videos/flvs
MINFILESIZE=2M

for f in `find $CACHE -size +$MINFILESIZE`
do
    a=$(file $f | cut -f2 -d ' ')
    o=$(basename $f)
    if [ "$a" = "Macromedia" ]
        then
            cp "$f" "$OUTPUTDIR/$o"
    fi
done

nautilus  "$OUTPUTDIR"&

It uses similar ideas to those presented here, hope this is helpful to someone.

它使用与此处提供的相似的想法,希望这对某人有帮助。

#8


2  

I guess that '$PATH_TO_SOMEWHERE'is something like '<directory>/*'.

我猜'$ PATH_TO_SOMEWHERE'就像' / *'。

In this case, I would change the code to:

在这种情况下,我会将代码更改为:

find <directory> -maxdepth 1 -type d -exec ... \;
find <directory> -maxdepth 1 -type f -name "*.txt" -exec ... \;

If you want to do something more complicated with the directory and text file names, you could:

如果您想对目录和文本文件名做一些更复杂的事情,您可以:

find <directory> -maxdepth 1 -type d | while read dir; do echo $dir; ...; done
find <directory> -maxdepth 1 -type f -name "*.txt" | while read txtfile; do echo $txtfile; ...; done

If you have spaces in your file names, you could:

如果文件名中有空格,则可以:

find <directory> -maxdepth 1 -type d | xargs ...
find <directory> -maxdepth 1 -type f -name "*.txt" | xargs ...

#9


2  

The correct answer on how to take the extension available in a filename in linux is :

关于如何在linux中使用文件名中的扩展名的正确答案是:

${strFileName##*\\.} 

Example of printing all file extensions in a directory

打印目录中的所有文件扩展名的示例

for fname in $(find . -maxdepth 1 -type f) # only regular file in the current dir
    do  echo ${fname##*\.} #print extensions 
done

#10


1  

My take on it with cut

我对它进行切割

>cut -d'.' -f2<<<"hi_mom.txt"
txt

My take on it with awk would be something like the following.

我对awk的看法将如下所示。

>MY_DATA_FILE="my_file.sql"
>FILE_EXT=$(awk -F'.' '{print $NF}' <<< $MY_DATA_FILE)
>if [ "sql" = "$FILE_EXT" ]
>then
>   echo "file is sql"
>fi

>awk -F'.' '{print $NF}' <<eof
>hi_mom.txt
>my_file.jpg
>eof