我试图在一个文件夹中搜索所有制表符分隔文件,如果找到任何文件,我需要使用bash将它们全部传输到另一个文件夹.
在我的代码中,我目前正在尝试查找所有文件,但不知何故它无法正常工作.
这是我的代码:
>nul 2>nul dir /a-d "folderName\*" && (echo Files exist) || (echo No file found)
Run Code Online (Sandbox Code Playgroud)
提前致谢 :)
对于一个简单的举动- (或复制替换mv用cp的文件),@ tripleee的回答是足够的.要递归搜索文件并在每个文件上运行命令,find都会派上用场.
例:
find <src> -type f -name '*.tsv' -exec cp {} <dst> \;
Run Code Online (Sandbox Code Playgroud)
要从<src>中复制的目录在哪里,并且是要复制到的目录.请注意,这会以递归方式搜索,因此任何名称重复的文件都会导致覆盖.您可以在覆盖之前传递给它提示:<dst>-icp
find <src> -type f -name '*.tsv' -exec cp -i {} <dst> \;
Run Code Online (Sandbox Code Playgroud)
解释:
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
Run Code Online (Sandbox Code Playgroud)
为了了解在没有实际find运行实际命令的情况下发生的事情,您可以在echo其前面加上只打印每个命令而不是运行它:
find <src> -type f -name '*.tsv' -exec echo cp -i {} <dst> \;
Run Code Online (Sandbox Code Playgroud)