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. Target: Linux, run this script, enter the folder name, And you can copy and overwrite the folder 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. -bash :. /tcopy. sh: Permission denied. Grant the Permission to chmod + x tcopy. sh and then execute. /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 place the full folder in the array loop structure with wildcards to put all files in the original folder into all arrays.
Code:
#!/bin/shsed -i 's/alias cp/#&/' ~/.bashrc >/dev/nullecho "Please input the filename:"read namearray=($name*)name2="$name/*"for ((i=1; i<${#array}; i++))docp -r $name2 ${array[$i]}donesed -i "/#alias cp/i\alias cp='cp -i'" ~/.bashrc >/dev/nullsed -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 is 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 item: original ~ /. The bashrc file may not be alias cp = 'cp-I'
After using this script, you will be asked whether to overwrite the copied file, that is, cp: overwrite. Please modify it manually ~ /. Bashrc file, add # to the front of the alias cp line #