简体   繁体   中英

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:

I have a folder called FolderA that sits in 3 different spots in my PC. 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.

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. 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. xargs -l takes each line from the find output and call dirname with each line as the argument. dirname takes a path and truncates the final item on the path. The last command uses xargs to put each line of output from the previous command as the destination of the cp command. Warning: this has not been tested with spaces in the path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM