交换两个文件的文件名

回复 收藏
写一个脚本用来互换两个文件的文件名。

{{{密码回复可见}}}

2015-08-07 09:55 举报
已邀请:
0

oldorab

赞同来自:

学习
0

duyanbin

赞同来自:

学习一下
0

ggangelo

赞同来自:

#!/bin/bash
echo "this script will exchange the file name"
echo
read -p "please input you first file name: " f
read -p "please input you second file name: " s
if [ -f $f -a -f $s ]
then
mv $f "$f".bak
mv $s "$s".bak
mv "$s".bak $f
mv "$f".bak $s
fi
0

wangzai

赞同来自:

xuexi
0

wsw13640218682

赞同来自:

看看
0

阿凯

赞同来自:

不会。。
0

CNS2016

赞同来自:

学习
0

等风来

赞同来自:

学习
0

linle

赞同来自:

read -p "please input a filename: " a
read -p "please input a filename: " b
cat $a
cp $a /tmp/s.txt
cat "$b" > $a
cat /tmp/s.txt > $b
cat $a
0

branttsai

赞同来自:

study,tks
0

qidi2015

赞同来自:

这需求很少吧。
0

mind_sky

赞同来自:

看看
0

西瓜.M

赞同来自:

kankan
0

牛牛牛爱洗澡

赞同来自:

看看
0

nihao426181

赞同来自:

^^^^
0

十月鱼

赞同来自:

学习
0

xmmixiangzhu

赞同来自:

学习一下
0

午夜DJ

赞同来自:

练习
0

善思致用

赞同来自:

学习
0

jokerhuman

赞同来自:

学习
0

5as

赞同来自:

ww
0

且听风吟

赞同来自:

1
0

wy1112980

赞同来自:

  1. #!/bin/bash

  2. read -p "plese input filename 1:" a
  3. while :;do
  4.    if [ ! -f "$a" ];
  5.    then
  6.    echo "The file is not found"
  7.    read -p "plese input filename 1:" a
  8.    else
  9.    break;
  10.    fi
  11. done
  12. read -p "plese input filename 2:" b
  13. while :;do
  14.    if [ ! -f "$b" ];
  15.    then
  16.    echo "The file is not found"
  17.    read -p "plese input filename 2:" b
  18.    else
  19.    break;
  20.    fi
  21. done
  22. echo "wait for a minutes"
  23. mv $a $a.bak
  24. mv $b $a
  25. mv $a.bak $b
  26. sleep 1
  27. echo "It's OK!"
0

GaryHuang0113 - 世上无难事,只怕有心人

赞同来自:

666
0

yoyodark

赞同来自:

学习
0

13650928714

赞同来自:

#!/bin/bash read -p "Please input a filename:" x read -p "Please input another filename:" y touch xz /bin/cp $x xz /bin/cp $y $x mv xz $y
0

hhao

赞同来自:

1
0

wy1028

赞同来自:


练习
0

15812926028

赞同来自:

look
0

liqian

赞同来自:

学习
0

小熊

赞同来自:

查看
0

luckytodd

赞同来自:

看看
0

小杰

赞同来自:

{:4_91:}
0

cj2017

赞同来自:

#!/bin/bash
n1=$1
n2=$2
mv $1 $1_bak
mv $2 $n1
mv $1_bak $n2
0

riverxyz

赞同来自:

看了都是用mv,看下老师是不是用其他方法
0

hmh

赞同来自:

ok
0

linuxcp

赞同来自:

{:4_91:}
0

13805775620

赞同来自:

看看思路
0

kevinjin

赞同来自:

#! /bin/bash
cd /root/shell_practice
cp 2.txt 2.txt.bak
mv 1.txt 2.txt
mv 2.txt.bak 1.txt
0

幻夜猫儿

赞同来自:

本帖最后由 幻夜猫儿 于 2016-8-27 23:24 编辑
  1. #!/bin/bash
  2. #
  3. #
  4. /bin/mv file1 /tmp/
  5. /bin/mv file2 file1
  6. /bin/mv /tmp/file1 file2
0

kw是id

赞同来自:

#!/bin/bash
dir=/usr/local/sbin/test
cp $dir/$2 $dir/$2.bak
rm -f $dir/$2
mv $dir/$1 $dir/$2
mv $dir/$2.bak $dir/$1

0

hch735347147

赞同来自:

学习

0

大雁

赞同来自:

学习下

0

王斌

赞同来自:

#!/bin/bash

#Auth: Cityzen Wang

#Date: 2017-05-01

#Scripts: Change filename for two files

a=`ls $1`

b=`ls $2`

mv $2 tmpfile

mv $1 $2

mv tmpfile $1

0

王斌

赞同来自:

改了一下,增加了判断语句

#! /bin/bash
#Auth: Cityzen Wang
#Date: 2017-05-01
#Scripts: Change two files
function changefile() {
    a=`ls $file1`
    b=`ls $file2`
    mv $file2 tmpfile
    mv $file1 $file2
    mv tmpfile $file1
}

while :
do
    read -p "Please input two file: " file1 file2
    if [ ! -e $file1 ] || [ ! -e $file2 ];then
        echo "Two files must all exist!"
    elif [ -d $file1 ] && [ -d $file2 ];then
        changefile
        exit
    elif [ -f $file1 ] && [ -f $file2 ]; then
         changefile
         exit
    else
        echo "Can not change filename between Directory and File"
    fi
done

0

杨银根

赞同来自:

看看

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: