Copy a folder to all scripts with the same name in Linux

Source: Internet
Author: User

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 #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.