循环遍历目录中的文件并使用 HEAD 命令

Looping over files in a directory and using the HEAD command

好的,所以我正在尝试让我的脚本循环遍历目录中的 n 个文件,并使终端在已循环的每个文件的顶部打印 3-4 行。

这是我得到的:

 #!/bin/bash

 for f in   #for the file in the directory (Specified by user)

 do

 head  $f    #Print the first n lines (Specified by user) of the file.

 done          #finish

一直收到这个错误: 头:无法打开“3”进行读取:没有这样的文件或目录

您的脚本中有两个错误:

1) 如果指定目录,则应通过通配符遍历文件,因此更改

    for f in 

    for f in /*

2) head 命令的行数由 -n 标志指定,因此更改

    head  $f

    head -n  $f