2013-04-22 25 views
0

我想根据每个文件夹中以相同的两个字母开头的文件数做不同的操作---如果TS中的文件小于或等于6一组动作和否则再做另一组 我的数据是这样的如果取决于每个文件夹的文件数--unix

files/TS01 -- which has 2 files 
files/TS02 -- which has 5 files 
files/TS03 -- which has 2 files 
files/TS04 -- which has 7 files 
files/TS05 -- which has 9 files 

我已经试过

FILES="$TS*" 
for W in $FILES 
do 
    doc=$(basename $W) 
    if [ $W -le 6 ] 
    then 
    .... 
    done ... 
    fi 
done 

,但我得到一个错误说“整数表达式预期”

我试图

if [ ls $W -le 6 ] 

我得到另一个错误回报说:“太多的参数”

能否请你帮

回答

2

为了得到线我会建议管道ls -l命令进入厕所的数量 - L,这将吐出的行数在目录如下...

Atlas $ ls -l | wc -l 
    19 

我做了一个小脚本,它展示了如何再利用这个结果conditi onally做一件事或其他...

#!/bin/bash 

amount=$(ls -l | wc -l) 

if [ $amount -le 5 ]; then 
    echo -n "There aren't that many files, only " 
else 
    echo -n "There are a lot of files, " 
fi 

echo $amount 

当一个文件夹上与之呼应19个文件执行..

Atlas $ ./howManyFiles.sh 
    There are a lot of files, 19 

和一个用不到5个文件...

Atlas $ ./howManyFiles.sh 
    There aren't that many files, only 3 

希望这会有助于向您展示如何从文件夹获取可用的文件数,然后如何在“if”语句中使用这些结果!

+0

我不会设法得到您呈现的结果,我正在编辑以提出问题以显示数据结构 – 2013-04-22 20:05:30

+0

当您运行ls -l | wc -l在目录上? – 2013-04-22 20:09:15

+0

我正在运行这里面的子目录,当我运行,我给我的主目录中的文件数量,它不会通过循环---我解决了问题'金额= $(ls $ W | wc -l)' - thanx的帮助 – 2013-04-22 20:17:11

相关问题