这是我的代码在这里:
#!/bin/bash
#read in a directory
echo "Enter input directory:"
read inputDir
#lists the files in a directory
#this lists the path with the file
for entry in "$inputDir"/*
do
echo "$entry"
touch $entry
done
echo "Enter output directory"
read outputDir
mkdir -p $outputDir
#changing directorys will allow us to not list the path
cd $outputDir
prefix=kml_
#after changing the directory we can then move the file and add a prefix.
for entry in *
do
mv $entry $prefix$entry
done
为什么我输入文件夹中的文件不会移到输出文件夹?
答案 0 :(得分:1)
简短的回答是“因为您没有尝试移动它们”。
更长的答案是,在运行mv
命令之前,您需要切换到另一个文件夹(cd $outputDir
),因此,在运行for循环(for entry in *
)时,它会展开一个不同的列表来自您想要的文件。
您可以像这样修改代码(删除原始注释):
#!/bin/bash
echo "Enter input directory:"
read inputDir
for entry in "$inputDir"/*
do
echo "$entry"
touch "$entry" # it is unclear why you do this step
done
echo "Enter output directory"
read outputDir
mkdir -p "$outputDir"
prefix=kml_
for entry in "$inputDir"/*
do
# strip directory
entryNoPath=${entry##*/} # pure bash method
# entryNoPath=$(basename "$entry") # common alternative method
mv $entry "$outputDir/$prefix$entryNoPath"
done