A script that copies directories to all directories of the same name under Linux

Source: Internet
Author: User
Tags array length save file

Requirements: Copy the folder to the Linux folder. Overwrite all folders with the same name under the Linux folder target: Linux, run this script. Enter a directory name. You can bulk copy overrides. Instructions for use:    first putThis script fileand thethe folder to copymove to the Linux that will be overwrittenfolder, and then run this script to do so.     To Run the command:            ./tcopy.sh    1. Hypothesis Hints-bash:./tcopy.sh:permission denied, assign permissions first:            chmod +x tcopy.sh            run it again./tcopy.sh can.     2. If you feel trouble, sh tcopy.sh is ok.
Logical Description:    The CP command for the original system. is the Cp-i command. This way, running the CP command is not an active override.

So change the document ~/.BASHRC, and finally change back. reads the user input file name, uses the wildcard character to put the whole directory in the array loop structure to put all the files in the original directory into all the 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 $name 2 ${array[$i]}donesed-i "/#alias cp/i\alias cp= ' cp-i '" ~/.b ASHRC  >/dev/nullsed-i '/#alias cp/d ' ~/.BASHRC  >/dev/null
save file as tcopy.sh
Code Description:
Sed-i ' S/alias cp/#&/' ~/.BASHRC  >/dev/null
Stare out the alias CP line in the ~/.BASHRC file
array= ($name *)
Use wildcards to put all directories into arrays
Name2= "$name/*"
name2 All files under the original folder folder
${#array}
Get array length
Cp-r $name 2 ${array[$i]}
Copy files
<pre name= "code" class= "CPP" style= "FONT-SIZE:14PX; line-height:21px; " >sed-i "/#alias cp/i\alias cp= ' cp-i '" ~/.BASHRC  >/dev/nullsed-i '/#alias cp/d ' ~/.BASHRC  >/dev/null
Change back~/.BASHRC File
Risk items: The original ~/.BASHRC file may not be alias cp= ' cp-i '
Suppose you use this script. A copy file appears every time you ask if the overlay is: Cp:overwrite. Please manually change the ~/.BASHRC file and add # to the front of the alias CP Line

A script that copies directories to all directories of the same name under Linux

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.