无法在Linux中的for循环中处理目录的文件
我的目录有 2 个子目录,并且也有几个子目录,并且它们有一些文件。我需要重命名所有文件以将 html 扩展名附加到文件名中。 目录结构看起来像这样,
main-directory
sub-directory
sub-directory
sub-directory
file1
file2
and so on to lot of files
现在我无法使用这样的东西
for file in main-directory/*
do
if [ -f "$file" ]
then `mv "$file" "$file.html"`
fi
done
,因为 for 循环不会递归地使用路径。所以我使用了类似的东西
for file in `ls -1R main-directory` // -1 for showing file and directory names separated by new lines and -R for recursive travel
do
if [ -f "$file" ]
then `mv "$file" "$file.html"`
fi
done
,上面的代码无法重命名文件。为了检查线路是否
for file in `ls -1R main-directory`
正常工作,我写了这样的内容
for file in `ls -1R main-directory`
do
if [ -f "$file" ]
echo $file
done
,但没有显示任何内容。有什么问题吗?
i have directory that has 2 sub-directories and that again has few sub-directory and they have some files. I need to rename all the files to append an html extension to the filenames.
the directory structure looks like this
main-directory
sub-directory
sub-directory
sub-directory
file1
file2
and so on to lot of files
now i could not use something like this
for file in main-directory/*
do
if [ -f "$file" ]
then `mv "$file" "$file.html"`
fi
done
because the for loop wont use the path recursively. so i used something like this
for file in `ls -1R main-directory` // -1 for showing file and directory names separated by new lines and -R for recursive travel
do
if [ -f "$file" ]
then `mv "$file" "$file.html"`
fi
done
the above code is not able to rename files. to check whether the line
for file in `ls -1R main-directory`
is working i wrote something like this
for file in `ls -1R main-directory`
do
if [ -f "$file" ]
echo $file
done
this doesn't show anything. what can be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 find 并查看类型文件,然后使用 -exec 更改所有文件,然后附加 .html。
you can use find and look into of type file and then -exec to change all the file and then appending the .html.
在第一个
for
循环中,mv
命令不应包含在反引号中。在第二个
for
循环中,if 语句的语法不正确。没有then
或fi
。应该是:但即便如此,这也不起作用,因为 ls -1R main-directory 只为您提供文件名,而不是文件的绝对路径。将 echo 移到 if 语句之外进行测试:
因此 ls -1R main-directory 并不是获取当前目录中所有文件的好方法。使用
find 。 - 输入 f
来代替。In your first
for
loop, themv
command should not be in back-ticks.In your second
for
loop, the if-statement has incorrect syntax. There is nothen
orfi
. It should be:But even then, this won't work because
ls -1R main-directory
gives you just the file names, not the absolute paths to the file. Move your echo outside the if-statement to test:Therefor
ls -1R main-directory
is not a good way to get all files in the current directory. Usefind . -type f
instead.由于某种原因,我永远记不起
find ... -exec
语法与{}
和\;
。相反,我已经养成了只使用 find 提供的循环的习惯:find 将每个文件输出到 stdout,并且 read file 一次会消耗一行,将该行的内容设置为
$file
环境变量。然后您可以在循环体的任何地方使用它。我使用这种方法来解决很多像这样的小问题,我需要循环一堆输出并做一些有用的事情。因为它更通用,所以我比深奥的
find ... -exec {} \;
方法更多地使用它。另一个技巧是在命令前面添加
echo
,以便在对系统进行可能损坏的操作之前进行快速健全性检查:For some reason, I can never remember the
find ... -exec
syntax off the top of my head with the{}
and the\;
. Instead, I've fallen into the habit of just using a loop fed from find:find
outputs each file to stdout and theread file
will consume one line at a time and set the contents of that line to the$file
environment variable. You can then use that anywhere in the body of your loop.I use this approach to solve lots of little problems like this where I need to loop over a bunch of output and do something useful. Because it is more versatile, I use it more than the esoteric
find ... -exec {} \;
approach.Another trick is to prepend you command with
echo
to do a quick sanity check before doing potentially damaging things to your system:这是我问题的答案。人们回复了 1 个衬垫,这是一个巧妙的方法,但我没有从这些 1 个衬垫中得到太多,所以这里是我想要的东西
sed 's/:$//'
检测到:
位于行尾并将其删除。这是阻止我的代码运行的原因之一,因为每当 ls -1R main-directory 检测到目录时,它都会在末尾附加一个:
here is the answer to my question. people responded by 1 liners which are a neat approach but i didnt get much out of those 1 liners so here is something that i wanted
here the
sed 's/:$//'
detects a:
at the end of line and removes it. this was one of the things that prevented my code to run because wheneverls -1R main-directory
detected a directory it appended a:
at the end