I'm a beginner in the terminal and bash language, so please be gentle and answer thoroughly. :)
I'm using Cygwin terminal.
I'm using the file command, which returns the file type, like:
$ file myfile1
myfile1: HTML document, ASCII text
Now, I have a directory called test, and I want to check the type of all files in it.
My endeavors:
I checked in the man page for file (man file), and I could see in the examples that you could type the names of all files after the command and it gives the types of all, like:
$ file myfile{1,2,3}
myfile1: HTML document, ASCII text
myfile2: gzip compressed data
myfile3: HTML document, ASCII text
But my files' names are random, so there's no specific pattern to follow. I tried using the for loop, which I think is going to be the answer, but this didn't work:
$ for f in ls; do file $f; done
ls: cannot open `ls' (No such file or directory)
$ for f in ./; do file $f; done
./: directory
Any ideas?
答案 0 :(得分:2)
You can use a combination of the find
and xargs
command.
For example:
find /your/directory/ | xargs file
HTH
答案 1 :(得分:2)
Every Unix or Linux shell supports some kind of globs. In your case, all you need is to use *
glob. This magic symbol represents all folders and files in the given path.
eg., file directory/*
Shell will substitute the glob with all matching files and directories in the given path. The resulting command that will actually get executed might be something like:
file directory/foo directory/bar directory/baz
答案 2 :(得分:1)
file directory/*
可能是解决问题的最简单最简单的解决方案,但这更能解释为什么你的循环无效。
for f in ls; do file $f; done
ls: cannot open `ls' (No such file or directory)
对于这个循环,它说“对于目录或文件中的f'ls'; do ...”如果你想要它执行ls
命令那么你需要做这样的事情
for f in `ls`; do file "$f"; done
但是,如果任何文件名包含空格,那将无法正常工作。使用shell的内置“globbing”更安全,更高效,如此
for f in *; do file "$f"; done
对于这个,有一个简单的解决方法。
for f in ./; do file $f; done
./: directory
目前,您要求它运行目录“./”的文件命令。 通过将其更改为“./*”含义,当前目录中的所有内容(与*相同)。
for f in ./*; do file "$f"; done
请记住,双引号变量可以防止通配和分词。