Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在linux中处理for循环中目录的文件_Linux_Bash_For Loop_Directory_File Rename - Fatal编程技术网

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

无法在linux中处理for循环中目录的文件,linux,bash,for-loop,directory,file-rename,Linux,Bash,For Loop,Directory,File Rename,我有一个目录,它有两个子目录,同样也有几个子目录,它们有一些文件。我需要重命名所有文件,以便在文件名中附加一个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 [ -

我有一个目录,它有两个子目录,同样也有几个子目录,它们有一些文件。我需要重命名所有文件,以便在文件名中附加一个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 file in `ls -1R main-directory`
do
if [ -f "$file" ]
echo $file
done
现在我不能用这样的东西

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 file in `ls -1R main-directory`
do
if [ -f "$file" ]
echo $file
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`  
我写了这样的东西

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 file in `ls -1R main-directory`
do
if [ -f "$file" ]
echo $file
done

这没有显示任何东西。有什么问题吗?

您可以使用“查找并查找”文件类型,然后使用-exec更改所有文件,然后添加.html

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

您可以使用find和lookin-of-type-file和-exec来更改所有文件,然后追加.html

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

在您的第一个
for
循环中,
mv
命令不应在back ticks中

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

for file in `ls -1R main-directory`
do
if [ -f "$file" ]
then
   echo $file
fi
done
但即使这样,这也不起作用,因为
ls-1R主目录
只提供文件名,而不是文件的绝对路径。将回音移到if语句之外进行测试:

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

因此,
ls-1R主目录
不是获取当前目录中所有文件的好方法。使用
find-改为键入f

在第一个
for
循环中,
mv
命令不应在back ticks中

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

for file in `ls -1R main-directory`
do
if [ -f "$file" ]
then
   echo $file
fi
done
但即使这样,这也不起作用,因为
ls-1R主目录
只提供文件名,而不是文件的绝对路径。将回音移到if语句之外进行测试:

for file in `ls -1R main-directory`
do
   echo $file
done
因此,
ls-1R主目录
不是获取当前目录中所有文件的好方法。使用
find-改为键入f

由于某种原因,我永远记不起
查找-用
{}
\,执行
语法。相反,我养成了只使用find提供的循环的习惯:

find main-directory -type f | while read file
do
    mv "$file" "$file.html"
done
find
将每个文件输出到stdout,
read file
将一次使用一行,并将该行的内容设置为
$file
环境变量。然后,您可以在循环体的任何位置使用它

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

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

find find main-directory -type f | while read file
do
    echo mv "$file" "$file.html"
done
由于某种原因,我永远记不起
查找-用
{}
\,执行
语法。相反,我养成了只使用find提供的循环的习惯:

find main-directory -type f | while read file
do
    mv "$file" "$file.html"
done
find
将每个文件输出到stdout,
read file
将一次使用一行,并将该行的内容设置为
$file
环境变量。然后,您可以在循环体的任何位置使用它

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

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

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

这是我问题的答案。人们的回应是1行,这是一个整洁的方法,但我没有从这些1行中得到太多,所以这里是我想要的东西

IFS=$'\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的/:$/”
在行尾检测到一个
,并将其删除。这是阻止我的代码运行的原因之一,因为每当
ls-1R主目录
检测到一个目录时,它就会在末尾附加一个

以下是我问题的答案。人们的回应是1行,这是一个整洁的方法,但我没有从这些1行中得到太多,所以这里是我想要的东西

IFS=$'\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的/:$/”
在行尾检测到一个
,并将其删除。这是阻止我的代码运行的原因之一,因为每当
ls-1R主目录
检测到一个目录时,它就会在末尾附加一个

@Jasonw:for循环的上面有什么问题?你能解释一下你的命令吗?{}做了什么?为什么\最后?@lovesh,首先,你应该知道循环哈希语法错误。其次,ls只列出目录的内容,然后if检查验证文件是否存在。对于您的实现,在文件不存在之前,不会显示该文件。在检查if之前,试着做一个echo$文件,然后做一个pwd,你会明白的。@Jasonw:我在for循环中的
do
之后放了一个
echo$文件,我得到了一个新目录上主目录的名称和目录列表line@lovesh,这是我的版本,如果您注意到$cur_dir和$file,$cur_dir将始终输出执行此脚本的位置。那么$file就是文件名,而不是绝对路径。所以当你检查[-f“$file”];bash找不到该文件。#/bin/sh用于ls-R主目录中的文件
;do cur_dir=
pwd
echo“where$cur_dir file$file”如果[-f“$file”];然后echo$file fidone@Jasonw::是的,我得到了这个,我需要将当前目录名附加到file@Jasonw:上面的for循环有什么问题?你能解释一下你的命令吗?{}做了什么?为什么\最后?@lovesh,首先,你应该知道循环哈希语法错误。第二,只有我