Copy a folder to all scripts with the same name in Linux
Requirement: copy the folder to the Linux directory to overwrite all folders with the same name under the Linux directory.
Objective: in Linux, run the script and enter the folder name to copy and overwrite the file in batches.
Instructions for use:
First, move the script file and the folder to be copied to the Linux directory to be overwritten, and then execute the script.
Run the following command:
./Tcopy. sh
1. If the prompt "-bash:./tcopy. sh: Permission denied" is displayed, grant the Permission to it first:
Chmod + x tcopy. sh
Run./tcopy. sh.
2. If you are in trouble, sh tcopy. sh is fine.
Logic description:
The cp command of the original system is the cp-I command, so that the cp command cannot be automatically overwritten. So change the file ~ /. Bashrc, and then change back.
Read the user input file name and use wildcards to put the full folder in the array
Loop Structure puts all files in the original folder into all arrays.
Code:
#! /Bin/sh
Sed-I's/alias cp /#&/'~ /. Bashrc>/dev/null
Echo "Please input the filename :"
Read name
Array = ($ name *)
Name2 = "$ name /*"
For (I = 1; I <$ {# array}; I ++ ))
Do
Cp-r $ name2 $ {array [$ I]}
Done
Sed-I "/# alias cp/I \ alias cp = 'cp-I '"~ /. Bashrc>/dev/null
Sed-I '/# alias cp/d '~ /. Bashrc>/dev/null
Save the file as tcopy. sh.
Code Description:
Sed-I's/alias cp /#&/'~ /. Bashrc>/dev/null
Change ~ /Comment out the alias cp line in the bashrc File
Array = ($ name *)
Use wildcards to put all folders into an array
Name2 = "$ name/*" name2
All files in the original folder directory
$ {# Array}
Get array Length
Cp-r $ name2 $ {array [$ I]}
Copy a file
Sed-I "/# alias cp/I \ alias cp = 'cp-I '"~ /. Bashrc>/dev/null
Sed-I '/# alias cp/d '~ /. Bashrc>/dev/null
Change back ~ /. Bashrc File
Risk items:
Original ~ /. The bashrc file may not be alias cp = 'cp-I'
If you use this script, you will be asked whether to overwrite the copied file every time: cp: overwrite,
Please manually modify ~ /. Bashrc file, add # to the front of the alias cp line #
This article permanently updates the link address: