Use a for loop to create files in a specified directory in batches and rename all files

Source: Internet
Author: User


Requirement 1:Use the for loop to create ten files in the/root/scripts/practice/q1/oldboy directory. The names are as follows:

Oldboy-1,

Oldboy-2,

.....

Oldboy-10.


Script implementation:

#! /Bin/bash # question1shopt-s expand_aliasesalias LL = "ls-L" CD/root/scripts/practice/q1 # first, determine whether the directory exists. If it does not exist, create the directory if [! -E oldboy] Then/bin/mkdir oldboyelse echo "" fipath =/root/scripts/practice/q1/oldboycd $ pathfor (I = 1; I <= 10; I ++) Do/bin/touch oldboy-$ idonell $ path

Script Execution result:

# [Email protected] Q1] # PWD/root/scripts/practice/q1 [[email protected] Q1] # ll total 4-rw-r -- r -- 1 Root 286 06 -26 q1.sh [[email protected] Q1] # [[email protected] Q1] # sh q1.sh total 0-rw-r -- r -- 1 Root 0 06-26 oldboy-1-rw-r -- r -- 1 root 0 06-26 oldboy-10-rw-r -- r -- 1 Root 0 06-26 oldboy-2-rw-r -- r -- 1 Root 0 06-26 oldboy-3-rw-r -- r -- 1 Root 0 06 -26 oldboy-4-rw-r -- r -- 1 Root 0 06-26 oldboy-5-rw-r -- r -- 1 Root 0 06-26 oldboy-6-rw-r -- r -- 1 Root 0 06-26 oldboy-7-rw-r -- r -- 1 Root 0 06-26 oldboy-8-rw-r -- r -- 1 Root 0 06-26 oldboy-9 # When the directory exists # Clear the oldboy directory, but do not delete this directory, the simulated directory contains [[email protected] Q1] # rm-RF oldboy/* [[email protected] Q1] # lsoldboy q1.sh [[email protected] Q1] # ll oldboy/total 0 [[email protected] Q1] # sh q1.sh total 0-rw-r -- r -- 1 Root 0 06-26 oldboy-1-rw-r -- r -- 1 Root 0 06-26 oldboy-10-rw-r -- r -- 1 root 0 06-26 oldboy-2-rw-r -- r -- 1 Root 0 06-26 oldboy-3-rw-r -- r -- 1 Root 0 06-26 oldboy-4-rw-r -- r -- 1 Root 0 06 -26 oldboy-5-rw-r -- r -- 1 Root 0 06-26 oldboy-6-rw-r -- r -- 1 Root 0 06-26 oldboy-7-rw-r -- r -- 1 Root 0 06-26 oldboy-8-rw-r -- r -- 1 Root 0 06-26 oldboy-9 [[[email protected] Q1] # ll oldboy/total 0-rw-r -- r -- 1 Root 0 06-26 oldboy-1-rw-r -- r -- 1 Root 0 06-26 oldboy-10-rw-r -- r -- 1 Root 0 06-26 oldboy-2-rw-r -- r -- 1 Root 0 06-26 oldboy-3-rw-r -- r -- 1 Root 0 06-26 oldboy-4-rw-r -- r -- 1 Root 0 06-26 oldboy-5-rw-r -- r -- 1 Root 0 06-26 oldboy-6-rw-r -- r -- 1 Root 0 06-26 -- r -- 1 Root 0 06-26 oldboy-7-rw-r -- r -- 1 Root 0 06-26 oldboy-8-rw-r -- r -- 1 Root 0 06-26 oldboy-9


The script can normally complete the file creation task.


Requirement 2: modify all oldboy-files created by appeal in batches using the for loop to Linux -.


Script implementation:

#!/bin/bash#Question2shopt -s expand_aliasesalias ll=‘ls -l‘path=/root/scripts/practice/q1/oldboycd $pathfor ((i=1;i<=10;i++))do     /bin/mv oldboy-$i Linux-$idonell $path

Execution result:

[[Email protected] Q1] # sh q2.sh total 0-rw-r -- r -- 1 Root 0 06-26 Linux-1-rw-r -- r -- 1 Root 0 06-26 Linux-10-rw-r -- r -- 1 root 0 06-26 Linux-2-rw-r -- r -- 1 Root 0 06-26 Linux-3-rw-r -- r -- 1 Root 0 06-26 Linux-4-rw-r -- r -- 1 Root 0 06 -26 Linux-5-rw-r -- r -- 1 Root 0 06-26 Linux-6-rw-r -- r -- 1 Root 0 06-26 Linux-7-rw-r -- r -- 1 Root 0 06-26 Linux-8-rw-r -- r -- 1 Root 0 06-26 Linux-9 [[[email protected] Q1] # ls oldboy/Linux-1 Linux-2 Linux-4 Linux-6 Linux-8Linux-10 Linux-3 Linux-5 Linux-7 Linux-9 [[email protected] Q1] #


The name has been successfully renamed.



This article is from "Shi zhenning's technology blog" blog, please be sure to keep this source http://magic3.blog.51cto.com/1146917/1431155

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.