[英]Linux: Find all folders with a particular name, remove them and have a folder copied into the parent directory of those folders
I am trying to see if I can do the following with a single line of command in Linux: 我正在尝试查看是否可以在Linux中使用单行命令执行以下操作:
I have a folder called FolderA that sits in 3 different spots in my PC. 我有一个名为FolderA的文件夹,该文件夹位于PC的3个不同位置。 I have to run a command across a few Linux machines to replace FolderA (they could all be hidden in separate parent folders, get their locations and replace FolderB (which I know where it is and it is a fixed path, say in my current directory, which is different from where FolderA is.) Delete FolderA, and copy FolderB into where FolderA is. 我必须在几台Linux机器上运行命令以替换FolderA(它们都可以隐藏在单独的父文件夹中,获取它们的位置并替换FolderB(我知道它在哪里,并且它是固定路径,例如在当前目录中) ,这与FolderA所在的位置不同。)删除FolderA,然后将FolderB复制到FolderA所在的位置。
I know this is a lot to do and I can roughly figure out to use find command to get the locations, rm -rf to remove the folders (but I don't know how I can make use of the results in find) and then use cp to copy the folder. 我知道这有很多工作要做,我可以粗略地弄清楚使用find命令来获取位置,使用rm -rf来删除文件夹(但是我不知道如何使用find中的结果),然后使用cp复制文件夹。 However how can I do them in a single line? 但是,如何单行处理呢?
Thanks! 谢谢!
Here, I think this should do what you want. 在这里,我认为这应该做您想要的。
find / -name '*FolderA' -delete -print | xargs -l dirname | xargs -l cp FolderB
The find
command will search through your whole filesystem for a path that ends in FolderA, delete it, then print the path of the folder. find
命令将在整个文件系统中搜索以FolderA结尾的路径,将其删除,然后打印该文件夹的路径。 xargs -l
takes each line from the find
output and call dirname
with each line as the argument. xargs -l
从find
输出中获取每一行,并以每行作为参数调用dirname
。 dirname
takes a path and truncates the final item on the path. dirname
采用路径并截断路径上的最后一项。 The last command uses xargs
to put each line of output from the previous command as the destination of the cp
command. 最后一个命令使用xargs
将上一个命令的每一行输出作为cp
命令的目标。 Warning: this has not been tested with spaces in the path. 警告:这尚未通过路径中的空格进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.