简体   繁体   English

Bash脚本打印文件列表

[英]Bash scripting print list of files

Its my first time to use BASH scripting and been looking to some tutorials but cant figure out some codes. 这是我第一次使用BASH脚本,一直在寻找一些教程,但无法弄清楚一些代码。 I just want to list all the files in a folder, but i cant do it. 我只想列出文件夹中的所有文件,但我做不到。 Heres my code so far. 到目前为止,这是我的代码。

#!/bin/bash
# My first script
echo "Printing files..."
FILES="/Bash/sample/*"

for f in $FILES
do
    echo "this is $f"
done

and here is my output.. 这是我的输出

Printing files... this is /Bash/sample/* 正在打印文件...这是/ Bash / sample / *

What is wrong with my code? 我的代码有什么问题?

You misunderstood what bash means by the word "in". 您误解了bash中“ in”一词的含义。 The statement for f in $FILES simply iterates over (space-delimited) words in the string $FILES , whose value is "/Bash/sample" (one word). for f in $FILES语句仅对$FILES字符串中的(用空格分隔)单词进行迭代,其值是“ / Bash / sample”(一个单词)。 You seemingly want the files that are "in" the named directory, a spatial metaphor that bash's syntax doesn't assume, so you would have to explicitly tell it to list the files. 您似乎希望文件位于命名目录中,这是bash语法不假定的空间隐喻,因此您必须明确地告诉它列出文件。

for f in `ls $FILES`         # illustrates the problem - but don't actually do this (see below)
...

might do it. 可能会做到。 This converts the output of the ls command into a string, "in" which there will be one word per file. 这会将ls命令的输出转换为字符串“ in”,每个文件中只有一个单词。

NB: this example is to help understand what "in" means but is not a good general solution. 注意:此示例是为了帮助理解“ in”的含义,但不是一个好的通用解决方案。 It will run into trouble as soon as one of the files has a space in its name—such files will contribute two or more words to the list, each of which taken alone may not be a valid filename. 一旦其中一个文件名称中有空格,它将遇到麻烦-这些文件将为列表贡献两个或多个单词,每个单词单独使用可能不是有效的文件名。 This highlights (a) that you should always take extra steps to program around the whitespace problem in bash and similar shells, and (b) that you should avoid spaces in your own file and directory names, because you'll come across plenty of otherwise useful third-party scripts and utilities that have not made the effort to comply with (a). 这突显了(a)您应该始终采取额外的步骤来解决bash和类似shell中的空格问题,并且(b)您应该避免在自己的文件名和目录名中使用空格,因为否则会遇到很多其他问题尚未努力遵守(a)的有用的第三方脚本和实用程序。 Unfortunately, proper compliance can often lead to quite obfuscated syntax in bash. 不幸的是,适当的遵从性经常会导致bash中语法的混淆。

I think problem in path " /Bash/sample/* ". 我认为路径“ /Bash/sample/* ”中存在问题。

U need change this location to absolute, for example: 您需要将此位置更改为绝对位置,例如:

/home/username/Bash/sample/*

Or use relative path, for example: 或使用相对路径,例如:

~/Bash/sample/*

On most systems this is fully equivalent for: 在大多数系统上,这完全等效于:

/home/username/Bash/sample/*

Where username is your current username, use whoami to see your current username. 其中username是您的当前用户名,请使用whoami查看您的当前用户名。

Best place for learning Bash: http://www.tldp.org/LDP/abs/html/index.html 学习Bash的最佳场所: http : //www.tldp.org/LDP/abs/html/index.html

This should work: 这应该工作:

echo "Printing files..."
FILES=(/Bash/sample/*) # create an array.
                       # Works with filenames containing spaces.
                       # String variable does not work for that case.

for f in "${FILES[@]}" # iterate over the array.
do
    echo "this is $f"
done

& you should not parse ls output . &您不应该解析ls输出

Take a list of your files) 列出您的文件)

If you want to take list of your files and see them: 如果要列出文件列表并查看它们:

    ls                           ###Takes list###
    ls -sh                       ###Takes list + File size###
    ...

If you want to send list of files to a file to read and check them later: 如果要将文件列表发送到文件以读取并稍后检查它们:

    ls > FileName.Format         ###Takes list and sends them to a file###
    ls > FileName.Format         ###Takes list with file size and sends them to a file###

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

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