linux - 从shell脚本中的目录获取文件名

标签 linux shell unix

我想使用 shell 脚本对目录中存在的所有文件进行循环。此外,我想显示每个文件的内容。我将目录作为命令行参数传递。

我有一个简单的循环如下:

for file in $1
do
    cat $file
done

如果我跑

sh script.sh test

其中 test 是一个目录,我只获取第一个文件的内容。

谁能帮我解决这个问题?

最佳答案

几个备选方案:

SMA 代码的紧凑修改:

for file in $1/*
 do
      [[ -f $file ]] && cat $file
 done

或使用查找:

find $1 -type f -exec cat \{\} \;

关于linux - 从shell脚本中的目录获取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29179686/

相关文章:

linux - 珀尔 : background process not running because of print statement

linux - 使用循环解析ksh中的长参数和短参数

linux - Linux Darwin 和 Windows_NT 系统中的禁止文件/目录名称

bash - Bash 4.0.0 中 $@ 未绑定(bind)变量的解决方法?

windows - 将 Unix ImageMagick 脚本转换为 Windows 批处理文件

linux - 以编程方式获取给定国家 future 几年的时间变化日期

shell - 在服务器上作为后台进程运行 java jar 文件

git - CLI : implement something like git commit (open a text editor and get value)

file - 如何 'cut' 为空?

unix - 仅打印字符串中的第一个字段