I am definitely new to scripting. I have a script that processes some files and I can't figure out how to run this against all files in a directory that has many sub-directories and thousands of files. Basically I want to take this script and run it against a directory titled 2011 and have it process every file in every sub-folder.
#!/bin/bash
FILES=./userfiles/*
for f in $FILES
do
echo ""
echo ""
echo "Processing File $f ...."
echo ""
echo ""
make FILE_TYPE=user CSS_PATH=./log.css SCRIPT_PATH=./toggle.js SAXON_JAR=/opt/saxon/saxon9he.jar $f.html
echo ""
echo ""
echo "File $f.html Generated"
done
The following script will recurse into every directory under ./userfiles
and work on all files there, even ones with spaces in their names (or other nefarious characters).
Also, the way you have the script written, make will be passed as its last argument the filename as it appears on your disk appended with .html
. For example, the file ./userfiles/subdir/foobar.txt
will be passed to make as ./userfiles/subdir/foobar.txt.html
. If this is not what you want, then adjust ${file}.html
accordingly.
Additionally, if you want find
to only work on files ending in, for example, .html
, then you can append the option -name "*.html"
to the find
command anywhere after the pathname.
#!/bin/bash
topLevelDir="./userfiles"
while IFS= read -r -d $'\0' file; do
echo
echo
echo "Processing File $file ...."
echo
echo
make FILE_TYPE=user CSS_PATH=./log.css SCRIPT_PATH=./toggle.js SAXON_JAR=/opt/saxon/saxon9he.jar "${file}".html
echo
echo
echo "File $file.html Generated"
done < <(find "$topLevelDir" -type f -print0)
Here's a function that does what you ask, you pass it a folder see the call at the bottom func_process_folder_set "/folder".
# --- -------------------------------- --- #
# FUNC: Process a folder of files
# --- -------------------------------- --- #
func_process_folder_set(){
FOLDER="${1}"
while read -rd $'\0' file; do
fileext=${file##*.} # -- get the .ext of file
case ${fileext,,} # -- make ext lowercase for checking in case statement
echo "FILE: $file" # -- print the file (always use " " to handle file spaces)
done < <(find ${FOLDER} -type f -maxdepth 20 -name '*.*' -print0)
}
# -- call the function above with this:
func_process_folder_set "/some/folder"
尝试使用find
命令获取所有文件和目录的列表
不确定脚本的作用是什么,但是为了从当前文件夹开始为树中的每个文件运行脚本,可以使用:
find . -exec yourscript.sh {} \;
由于您的脚本已经可以理解文件扩展的一个级别,因此在包含./userfiles
目录的每个目录上运行该脚本都将变得快速而简单。
$ find . -type d -a -name userfiles -a -exec sh /whatever/myscript {}/.. \;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.