简体   繁体   English

Linux:查找所有具有特定名称的文件夹,将其删除,然后将一个文件夹复制到这些文件夹的父目录中

[英]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 -lfind输出中获取每一行,并以每行作为参数调用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.

相关问题 将所有文件夹和文件移动到 Linux 目录中具有相同主题名称的文件夹中 - move all the folders and files to the folder with the same subject name in the directory Linux 查找目录中的文件夹,而不列出父目录 - find folders in a directory, without listing the parent directory linux文件和文件夹未继承父目录权限 - linux files and folders are not inheriting parent directory permissions 删除linux目录中的所有文件夹和文件,除了一个文件夹和该文件夹中的所有内容 - delete all folders and files within a linux directory except one folder and all contents inside that folder 命令列出Linux目录中的所有文件夹和子文件夹 - command to list all the folders and sub-folders in a directory in linux 查找具有特定名称的文件夹,并且没有指向它们的符号链接 - Find folders with specific name and no symlink pointing to them 如何在linux中查找具有特定父目录的所有文件? - How to find all files with a particular parent directory in linux? 在 Linux 中查找特定父文件夹并删除子文件夹中的文件 - Find specific parent folder and delete files inside sub-folders in Linux 删除目录中的所有文件(不要触摸任何文件夹或其中的任何内容) - Remove all files in a directory (do not touch any folders or anything within them) 在特定目录及其子目录中,找到所有扩展名为.tmp的文件夹 - In a particular directory and its sub-directories, find all the folders ending with .tmp extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM