Script for copying folders to all folders with the same name under Linux

Source: Internet
Author: User
Tags array length save file

Requirements: Copy the folder to the Linux directory, overwriting all folders with the same name under the Linux directory target: Linux, execute this script, enter the folder name, you can bulk copy overwrite. Instructions for use:    first putThis script fileand thethe folder to copymove to the Linux that will be overwrittenCatalogue, and then execute this script.     Execute command:            ./tcopy.sh    1. If prompted-bash:./tcopy.sh:permission denied, assign permissions first:            chmod +x tcopy.sh            then execute the./tcopy.sh.     2. If you feel trouble, sh tcopy.sh is ok.
Logical Description:    the CP command of the original system is the Cp-i command, so the command to execute the CP cannot be overwritten automatically. So change the file .~/.BASHRC, and finally change back.     read the user input file name and use wildcards to put the full folder in the array    the loop structure places all the files in the original folder 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
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 $name 2 ${array[$i]}
Copying 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 '
If you use this script, the copy file appears every time you ask whether to overwrite that is: Cp:overwrite, please manually modify the ~/.BASHRC file, add # in front of alias CP Line

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Scripts for copying folders to all folders with 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.