Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I';我试图使用bash查看目录中的所有文件,然后比较大小_Bash - Fatal编程技术网

I';我试图使用bash查看目录中的所有文件,然后比较大小

I';我试图使用bash查看目录中的所有文件,然后比较大小,bash,Bash,我正在查找目录中三个最小的文件,然后使用stat命令显示有用的信息,但是我无法让脚本读取正确的目录 目录正在被传递到脚本中,每当我传入目录时,它都会告诉我它不存在 #!/bin/bash #This script will find the files in a directory with the smallest size and print out the properties for those files. filepath='$1' smallestFileSize=10000000

我正在查找目录中三个最小的文件,然后使用stat命令显示有用的信息,但是我无法让脚本读取正确的目录

目录正在被传递到脚本中,每当我传入目录时,它都会告诉我它不存在

#!/bin/bash
#This script will find the files in a directory with the smallest size and print out the properties for those files.
filepath='$1'
smallestFileSize=10000000000000000000000000
secondSmallestSize=10000000000000000000000000
thirdSmallestSize=10000000000000000000000000
smallestFile=''
secondSmallestFile=''
thirdSmallestFile=''
for file in '$filepath'
do
  if [ $(stat -c %s file) -lt '$smallestFileSize' ]
  then
     '$smallestFileSize'=$(stat -c %s file)
     '$smallestFile'=file
  fi
  if [ $(stat -c %s file) -lt '$secondSmallestSize' && $(stat -c %s file) -gt '$smallestFileSize' ]
  then
     '$seondSmallestSize'=$(stat -c %s file)
     '$secondSmallestFile'=file
  fi
  if [ $(stat -c %s file) -lt '$thirdSmallestSize' && $(stat -c %s file) -gt '$secondSmallestSize' ]
  then
     '$thirdSmallestSize'=$(stat -c %s file)
     '$thirdSmallestFile'=file
  fi
done < '$filepath'

stat -c '%i %b %A %h %U %G %s $y' '$smallestFile'
stat -c '%i %b %A %h %U %G %s $y' '$secondSmallestFile'
stat -c '%i %b %A %h %U %G %s $y' '$thirdSmallestFile'
#/bin/bash
#此脚本将在最小大小的目录中查找文件,并打印出这些文件的属性。
文件路径=“$1”
smallestFileSize=100000000000000000
secondSmallestSize=100000000000000000
ThirdSalestSize=100000000000000000
Smallest文件=“”
SecondSmallest文件=“”
ThirdSalestFile=''
对于“$filepath”中的文件
做
如果[$(stat-c%s文件)-lt'$smallestFileSize']
然后
“$smallestFileSize”=$(stat-c%s文件)
“$smallestFile”=文件
fi
如果[$(stat-c%s文件)-lt'$secondSmallestSize'&&$(stat-c%s文件)-gt'$smallestFileSize']
然后
“$seondSmallestSize”=$(stat-c%s文件)
“$secondSmallestFile”=文件
fi
如果[$(stat-c%s文件)-lt'$thirdSalestSize'&&$(stat-c%s文件)-gt'$secondSmallestSize']
然后
“$thirdSalestSize”=$(stat-c%s文件)
“$thirdSalestFile”=文件
fi
完成<“$filepath”
统计-c“%i%b%A%h%U%G%s$y”$smallestFile
统计-c“%i%b%A%h%U%G%s$y”$secondSmallestFile
统计-c“%i%b%A%h%U%G%s$y”$thirdSalestFile

您的脚本有很多错误,其中大部分是语法和打字错误。我修复了它们(但此脚本有几个限制,例如文件名不能有空格):

但有一种更简单的方法可以达到同样的效果:

$ ls -1S | tail -3 | tac | xargs -d '\n' stat -c "%i %b %A %h %U %G %s %N"
解释

  • ls-1S
    :按大小排序的列表(第一个最大)
  • tail-3
    :仅打印最后3行
  • tac
    cat
    inversed:只打印输入(最后一行在前)
  • xargs-d'\n'
    :执行下一个参数,并在每个读取行中附加一个参数
示例

$ ls -l
total 1264
-rw-rw-r-- 1 wpomier wpomier 101637 Mar 22 21:59 01.dat
-rw-rw-r-- 1 wpomier wpomier 107713 Mai 30 15:45 02.dat
-rw-rw-r-- 1 wpomier wpomier 244813 Mai 30 16:18 03.dat
-rw-rw-r-- 1 wpomier wpomier 147281 Set  4 23:40 04.dat
-rw-rw-r-- 1 wpomier wpomier  69379 Set  4 23:52 05.dat
-rw-rw-r-- 1 wpomier wpomier 535752 Ago 28  2014 06.dat
-rw------- 1 wpomier wpomier  75021 Set  4 23:59 07.dat

$ ls -1S | tail -3 | tac | xargs -d '\n' stat -c "%i %b %A %h %U %G %s %N"
2491705 144 -rw-rw-r-- 1 wpomier wpomier 69379 '05.dat'
20327657 152 -rw------- 1 wpomier wpomier 75021 '07.dat'
20316583 200 -rw-rw-r-- 1 wpomier wpomier 101637 '01.dat'

尝试以下bash循环:

  • 查找/var/log(例如)中的所有文件
  • 获取每个文件大小
  • 按数字值按大小排序文件列表的输出
  • 头打印前3行
  • awk只打印第二列(文件路径)

  • 用于'find/var/log-type f-exec du-s'{}\\\中的文件sort-n | head-n3 | awk'{print$2}`;do stat-c“%i%b%A%h%U%G%s$y'${file}”;完成了

    单引号不会做你想做的事。请将代码粘贴到中并修复它报告的错误。如果在那之后你仍然有问题,或者你不明白它告诉你什么,那么问一个问题。
    zsh-c'stat*(oL[1,3])
    注意
    stat
    不是bash的一部分,它是一个完全独立的二进制文件,其行为和用法取决于你的操作系统。我在FreeBSD中运行bash,其中获取文件大小的命令可能是
    stat-f“%z'$filename”
    。如果代码依赖于特定于平台的工具,那么指定平台无疑是一个好主意。
    $ ls -l
    total 1264
    -rw-rw-r-- 1 wpomier wpomier 101637 Mar 22 21:59 01.dat
    -rw-rw-r-- 1 wpomier wpomier 107713 Mai 30 15:45 02.dat
    -rw-rw-r-- 1 wpomier wpomier 244813 Mai 30 16:18 03.dat
    -rw-rw-r-- 1 wpomier wpomier 147281 Set  4 23:40 04.dat
    -rw-rw-r-- 1 wpomier wpomier  69379 Set  4 23:52 05.dat
    -rw-rw-r-- 1 wpomier wpomier 535752 Ago 28  2014 06.dat
    -rw------- 1 wpomier wpomier  75021 Set  4 23:59 07.dat
    
    $ ls -1S | tail -3 | tac | xargs -d '\n' stat -c "%i %b %A %h %U %G %s %N"
    2491705 144 -rw-rw-r-- 1 wpomier wpomier 69379 '05.dat'
    20327657 152 -rw------- 1 wpomier wpomier 75021 '07.dat'
    20316583 200 -rw-rw-r-- 1 wpomier wpomier 101637 '01.dat'