Bash 如何检查文件夹中是否有任何以制表符分隔的文件?
我试图在一个文件夹中搜索所有以制表符分隔的文件,如果找到任何文件,我需要使用bash将它们全部传输到另一个文件夹 在我的代码中,我目前正在尝试查找所有文件,但不知何故,它不起作用 这是我的密码:Bash 如何检查文件夹中是否有任何以制表符分隔的文件?,bash,Bash,我试图在一个文件夹中搜索所有以制表符分隔的文件,如果找到任何文件,我需要使用bash将它们全部传输到另一个文件夹 在我的代码中,我目前正在尝试查找所有文件,但不知何故,它不起作用 这是我的密码: >nul 2>nul dir /a-d "folderName\*" && (echo Files exist) || (echo No file found) 提前感谢:您的尝试中几乎没有有效的Bash脚本 mv foldername/*.tsv otherfolder/
>nul 2>nul dir /a-d "folderName\*" && (echo Files exist) || (echo No file found)
提前感谢:您的尝试中几乎没有有效的Bash脚本
mv foldername/*.tsv otherfolder/
如果没有匹配的文件,将出现错误消息。您的尝试中几乎没有有效的Bash脚本
mv foldername/*.tsv otherfolder/
如果没有匹配的文件,将显示错误消息。对于简单的移动或复制-用文件的cp替换mv,@tripleee的回答就足够了。要递归搜索文件并在每个文件上运行命令,find很方便 例如:
find <src> -type f -name '*.tsv' -exec cp {} <dst> \;
解释:
find <src> -type f -name '*.tsv' -exec cp -i {} <dst> \;
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^^
| | | | | | | | | | | ||
| | | | | | | | | | | | --- terminator
| | | | | | | | | | | --- escape for terminator
| | | | | | | | | | --- destination directory
| | | | | | | | | --- the path of each file found
| | | | | | | | --- prompt before overwriting
| | | | | | | --- the copy command
| | | | | | --- flag for executing a command (cp in this case)
| | | | | --- pattern of files to match
| | | | --- flag for specifying file name pattern
| | | --- 'f' for a regular file (as opposed to e.g. 'd' for directory)
| | --- flag for specifying the file type
| --- location to search
--- the find command, useful for searching for files
对于简单的移动或复制-用cp替换mv文件,@tripleee的回答就足够了。要递归搜索文件并在每个文件上运行命令,find很方便 例如:
find <src> -type f -name '*.tsv' -exec cp {} <dst> \;
解释:
find <src> -type f -name '*.tsv' -exec cp -i {} <dst> \;
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^^
| | | | | | | | | | | ||
| | | | | | | | | | | | --- terminator
| | | | | | | | | | | --- escape for terminator
| | | | | | | | | | --- destination directory
| | | | | | | | | --- the path of each file found
| | | | | | | | --- prompt before overwriting
| | | | | | | --- the copy command
| | | | | | --- flag for executing a command (cp in this case)
| | | | | --- pattern of files to match
| | | | --- flag for specifying file name pattern
| | | --- 'f' for a regular file (as opposed to e.g. 'd' for directory)
| | --- flag for specifying the file type
| --- location to search
--- the find command, useful for searching for files
它不起作用。这意味着stackoverflow上的内容很少 让我们首先检查一下您所做的工作:
>nul 2>nul dir /a-d "folderName\*"
所以,您正在做一个dir,大多数Linux用户都会使用ls,但soit是开放的
/a-d
一切都以folderName命名
输出在文件nul中。出于调试目的,最好查看nul do cat nul中的内容。我敢打赌它是这样的:
dir: cannot access '/a-d': No such file or directory
dir: cannot access 'folderName\*': No such file or directory
这意味着dir退出时出错。因此,将不执行任何找到的文件
这意味着您的输出可能是
No file found
这正是我们所期望的
在代码中,您说过要查找所有文件。这意味着您需要
ls folderName
或
如果你想递归地做事。因为这个发现已经在上面由jsageryd解释过了,所以我不会详细说明
如果您只想查看该特定目录,可以执行以下操作:
if dir folderName/*.tsv > nul 2>nul ; then
echo "Files exist"
else
echo "No file found"
fi
然后从那里开始。它不起作用。这意味着stackoverflow上的内容很少 让我们首先检查一下您所做的工作:
>nul 2>nul dir /a-d "folderName\*"
所以,您正在做一个dir,大多数Linux用户都会使用ls,但soit是开放的
/a-d
一切都以folderName命名
输出在文件nul中。出于调试目的,最好查看nul do cat nul中的内容。我敢打赌它是这样的:
dir: cannot access '/a-d': No such file or directory
dir: cannot access 'folderName\*': No such file or directory
这意味着dir退出时出错。因此,将不执行任何找到的文件
这意味着您的输出可能是
No file found
这正是我们所期望的
在代码中,您说过要查找所有文件。这意味着您需要
ls folderName
或
如果你想递归地做事。因为这个发现已经在上面由jsageryd解释过了,所以我不会详细说明
如果您只想查看该特定目录,可以执行以下操作:
if dir folderName/*.tsv > nul 2>nul ; then
echo "Files exist"
else
echo "No file found"
fi
从这里开始。所有这些看起来都不像Bash。你真的想为Windows cmd编写脚本吗?不,我只想在bash中完成。所有这些看起来都不像bash。你真的想为Windows cmd编写脚本吗?不,我只想在bash中完成。{}是用来做什么的?我是否需要每次在花括号内提及找到的每个文件的路径?find将{}替换为每个文件的路径。如果您有一个文件./foo.tsv和destination../bar/,find将运行cp-i./foo.tsv../bar/。Yes。{}是find能够理解的特殊语法。仅将和替换为源目录和目标目录。{}用于什么?我是否需要每次在花括号内提及找到的每个文件的路径?find将{}替换为每个文件的路径。如果您有一个文件./foo.tsv和destination../bar/,find将运行cp-i./foo.tsv../bar/。Yes。{}是find能够理解的特殊语法。仅将和替换为源目录和目标目录。ehm。。。实际上,它是有效的bash。@LjmDullaart dir/a-d、nul重定向和反斜杠作为文件夹分隔符看起来非常像Windows,但本质上没有任何东西禁止您使用DOS实用程序和名为nul的常规文件。当然,括号只是多余的。不确定窗口是否使用双引号内的通配符做了有用的事情;在Bash中,它当然只是一个字面上的星号。。。实际上,它是有效的bash。@LjmDullaart dir/a-d、nul重定向和反斜杠作为文件夹分隔符看起来非常像Windows,但本质上没有任何东西禁止您使用DOS实用程序和名为nul的常规文件。当然,括号只是多余的。不确定窗口是否使用双引号内的通配符做了有用的事情;在Bash中,它当然只是一个字面上的星号。谢谢你的回答。我在这里发布了这个问题后意识到了我的错误,因为我是Linux新手,我发现了我正在处理的另一个问题的代码,其中没有提到代码是用于CMD而不是bash的。谢谢你的回答。在这里发布了这个问题之后,我意识到了我的错误,因为我是Linux新手,我发现我正在处理的另一个问题的代码中没有提到这个问题 t代码用于CMD而不是bash。