bash 如何在bash脚本中检查文件名的扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/407184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check the extension of a filename in a bash script?
提问by Paul Stephenson
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 后缀?
回答by
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.
右侧==
是贝壳图案。如果您需要正则表达式,请使用=~
then。
回答by Paul Stephenson
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
需要,为“: - ”修饰手段不同的东西。
回答by Eldelshell
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 的第一部分,在那里您可以获得“文本”、“应用程序”等内容。
回答by Adam Peck
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 来查看它是否匹配。
回答by kvz
You could also do:
你也可以这样做:
if [ "${FILE##*.}" = "txt" ]; then
# operation for txt files here
fi
回答by dargaud
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
回答by Albin. Com.
The correct answer on how to take the extension available in a filename in linux is:
关于如何在 linux 中获取文件名中可用的扩展名的正确答案是:
${filename##*\.}
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
回答by desdecode
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 脚本,它查看文件的类型,然后将其复制到一个位置,我用它来查看我从 Firefox 缓存中在线观看的视频:
#!/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.
它使用与此处介绍的想法类似的想法,希望这对某人有所帮助。
回答by jfg956
I guess that '$PATH_TO_SOMEWHERE'
is something like '<directory>/*'
.
我想那'$PATH_TO_SOMEWHERE'
是类似的东西'<directory>/*'
。
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 ...