开发者

Printing the name of each file with certain extension

开发者 https://www.devze.com 2023-03-03 09:57 出处:网络
how can I print out the name of each file in a certain directory with a specific extension? Here\'s what I have so far:

how can I print out the name of each file in a certain directory with a specific extension?

Here's what I have so far:

#!/bin/sh

DIR="~/Desktop"
SUFFIX="in"

for file 开发者_C百科in $DIR/*.$SUFFIX
do
    if [ -f $file ]; then
    echo $file
    fi  
done

Unfortunately it doesn't work.

What's wrong with it?


In your DIR="~/Desktop" the "~" not expanded, because it is in "". remove the "". DIR=~/Desktop


You could use find with -type f

#!/bin/sh
DIR="~/Desktop"
SUFFIX="in"
find "$DIR" -maxdepth 1 -type f -name "*.${SUFFIX}" -exec somecommand {} \;


For your information: "file" in Unix systems is typically the name of a command.

Its purpose is to analyze the files given as argument and infer the format. Example:

$ file entries.jar 
entries.jar: Zip archive data, at least v2.0 to extract


for file in `ls $DIR/*.$SUFFIX`

Note the ls and backticks

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号