无法在Linux中的for循环中处理目录的文件

发布于 2024-11-14 20:56:39 字数 881 浏览 6 评论 0原文

我的目录有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妄想挽回 2024-11-21 20:56:39

您可以使用 find 并查看类型文件,然后使用 -exec 更改所有文件,然后附加 .html。

find main-directory -type f -exec mv -v '{}' '{}'.html \; 

you can use find and look into of type file and then -exec to change all the file and then appending the .html.

find main-directory -type f -exec mv -v '{}' '{}'.html \; 
稀香 2024-11-21 20:56:39

在第一个 for 循环中,mv 命令不应包含在反引号中。

在第二个 for 循环中,if 语句的语法不正确。没有 thenfi。应该是:

for file in `ls -1R main-directory`
do
if [ -f "$file" ]
then
   echo $file
fi
done

但即便如此,这也不起作用,因为 ls -1R main-directory 只为您提供文件名,而不是文件的绝对路径。将 echo 移到 if 语句之外进行测试:

for file in `ls -1R main-directory`
do
   echo $file
done

因此 ls -1R main-directory 并不是获取当前目录中所有文件的好方法。使用 find 。 - 输入 f 来代替。

In your first for loop, the mv command should not be in back-ticks.

In your second for loop, the if-statement has incorrect syntax. There is no then or fi. It should be:

for file in `ls -1R main-directory`
do
if [ -f "$file" ]
then
   echo $file
fi
done

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:

for file in `ls -1R main-directory`
do
   echo $file
done

Therefor ls -1R main-directory is not a good way to get all files in the current directory. Use find . -type f instead.

掩于岁月 2024-11-21 20:56:39

由于某种原因,我永远记不起 find ... -exec 语法与 {}\;。相反,我已经养成了只使用 find 提供的循环的习惯:

find main-directory -type f | while read file
do
    mv "$file" "$file.html"
done

find 将每个文件输出到 stdout,并且 read file 一次会消耗一行,将该行的内容设置为 $file 环境变量。然后您可以在循环体的任何地方使用它。

我使用这种方法来解决很多像这样的小问题,我需要循环一堆输出并做一些有用的事情。因为它更通用,所以我比深奥的 find ... -exec {} \; 方法更多地使用它。

另一个技巧是在命令前面添加 echo ,以便在对系统进行可能损坏的操作之前进行快速健全性检查:

find find main-directory -type f | while read file
do
    echo mv "$file" "$file.html"
done

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 main-directory -type f | while read file
do
    mv "$file" "$file.html"
done

find outputs each file to stdout and the read 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:

find find main-directory -type f | while read file
do
    echo mv "$file" "$file.html"
done
波浪屿的海角声 2024-11-21 20:56:39

这是我问题的答案。人们回复了 1 个衬垫,这是一个巧妙的方法,但我没有从这些 1 个衬垫中得到太多,所以这里是我想要的东西

IFS=

sed 's/:$//' 检测到 : 位于行尾并将其删除。这是阻止我的代码运行的原因之一,因为每当 ls -1R main-directory 检测到目录时,它都会在末尾附加一个 :

\n' // this is for setting field separator to new line because the default is whitespace dir=main-directory for file in `ls -1R main-directory | sed 's/:$//'` // -1 for showing file and directory names separated by new lines and -R for recursive travel do if [ -f "$dir/$file" ] then `mv "$dir/$file" "$dir/$file.html"` elif [ -d "$file" ] then dir=$file fi done

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

IFS=

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 whenever ls -1R main-directory detected a directory it appended a : at the end

\n' // this is for setting field separator to new line because the default is whitespace dir=main-directory for file in `ls -1R main-directory | sed 's/:$//'` // -1 for showing file and directory names separated by new lines and -R for recursive travel do if [ -f "$dir/$file" ] then `mv "$dir/$file" "$dir/$file.html"` elif [ -d "$file" ] then dir=$file fi done

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 whenever ls -1R main-directory detected a directory it appended a : at the end

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文