当前位置: 动力学知识库 > 问答 > 编程问答 >

bash - Shellscript Looping Through All Files in a Folder

问题描述:

I want to write a shellscript that will loop through all the files in a folder and echo "put ${filename}". Can anyone point me in the right direction?

网友答案:
for filename in *; do echo "put ${filename}"; done
网友答案:
for file in *; do
    echo "put $file"
done

Or, if you want to include subdirectories:

find . -type f -exec echo put {} \;

If you want to include the folders themselves, take out the -type f part.

网友答案:

recursively, including files in subdirectories?

find dir -type f -exec echo "put {}" \;

only files in that directory?

find dir -maxdepth 1 -type f -exec echo "put {}" \;
网友答案:

One more alternative using ls and sed:

$ ls -1 <dir> | sed -e 's/^/put /'

and using ls and xargs:

$ ls -1 <dir> | xargs -n1 -i%f echo 'put %f'
网友答案:

this will work also recursively if you have any sub directories and files inside them:

find . -type f|awk -F"/" '{print "put ",$NF}'
网友答案:

If you don't have any files, then instead of printing * we can do this.

format=*.txt
for i in $format;
do
 if [[ "$i" == "$format" ]]
 then
    echo "No Files"
 else
    echo "file name $i"
 fi
done
分享给朋友:
您可能感兴趣的文章:
随机阅读: