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.
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:

Related Article

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.