0
在一个文件夹下创建两个文件 123.txt ,456.py
- #!/usr/bin/env python
- # coding=utf-8
- import os
- import shutil
- name_list = []
- list_name = os.listdir('/home/liujia')
- for i in range(0,len(list_name)):
- name_list.append(list_name[i].split('.')[0])
- shutil.move('/home/liujia/'+name_list[0]+'.py','/home/liujia/'+name_list[1]+'.py')
- shutil.move('/home/liujia/'+name_list[1]+'.txt','/home/liujia/'+name_list[0]+'.txt')
- list_new_name = os.listdir('/home/liujia')
- print list_new_name
0
这个脚本感觉有点小瑕疵,./2.sh /shares/passwd /shares/111运行不成功,显示找不到文件,后来改了一下,能够正常运行了
#!/bin/bash
file1=/shares/passwd
file2=/shares/111
exch_name() {
/bin/mv $1 /tmp/tmp
/bin/mv $2 $1
/bin/mv /tmp/tmp $2
}
exch_name $file1 $file2
#!/bin/bash
file1=/shares/passwd
file2=/shares/111
exch_name() {
/bin/mv $1 /tmp/tmp
/bin/mv $2 $1
/bin/mv /tmp/tmp $2
}
exch_name $file1 $file2
0
- #!/bin/bash
- # 文件名由参数传递 在/tmp下创建临时文件
- cat $1 > /tmp/tmpfile
- cat $2 > $1
- cat /tmp/tmpfile > $2
- rm -f /tmp/tmpfile
0
#!/bin/bash
read -p "please input two text: " $1 $2
if [ -f $1 && -f $2 ] ;then
touch 3
cat $1 > 3
> $1
cat $2 > $1
> $2
cat 3 > $2
fi
不对,好像是括号那格式不对
read -p "please input two text: " $1 $2
if [ -f $1 && -f $2 ] ;then
touch 3
cat $1 > 3
> $1
cat $2 > $1
> $2
cat 3 > $2
fi
不对,好像是括号那格式不对
0
#!/bin/bash
read -p "Please input two files. The first:" file1
read -p "The second:" file2
/bin/mv $file1 /tmp/tmp
/bin/mv $file2 $file1
/bin/mv /tmp/tmp $file2
echo "It's OK.look test your files."
read -p "Please input two files. The first:" file1
read -p "The second:" file2
/bin/mv $file1 /tmp/tmp
/bin/mv $file2 $file1
/bin/mv /tmp/tmp $file2
echo "It's OK.look test your files."
0
本帖最后由 rolay8 于 2015-12-26 21:50 编辑
- #!/bin/bash
- read -p "请输入两个要互换文件名称的文件:" file1 file2
- if [ -z $file2 ]; then
- echo "必须输入两个文件的文件全名!"
- exit 0
- elif [[ -d $file1 || -d $file2 ]]; then
- echo "暂不支持文件和目录名称交换!"
- exit 0
- elif [[ ! -f $file1 || ! -f $file2 ]]; then
- echo "请勿输入不存在的文件!"
- fi
- /bin/mv $file1 tmpname
- /bin/mv $file2 $file1
- /bin/mv tmpname $file2
- echo "交换成功!"
0
#!/bin/bash
name1=$1
name2=$2
if [ -f $name1 ] && [ -f $name2 ]
then
temp=temp
mv $name1 $temp
mv $name2 $name1
mv $temp $name2
else
echo 文件 $name1 或文件 $name2 不存在,请输入正确的文件名
fi
name1=$1
name2=$2
if [ -f $name1 ] && [ -f $name2 ]
then
temp=temp
mv $name1 $temp
mv $name2 $name1
mv $temp $name2
else
echo 文件 $name1 或文件 $name2 不存在,请输入正确的文件名
fi
0
#!/bin/bash
read -p "please input filename1(or include path): " filename1
read -p "please input filename2(or include path): " filename2
mv filename1 filename_temp
mv filename2 filename1
mv filename filename_temp filename2
read -p "please input filename1(or include path): " filename1
read -p "please input filename2(or include path): " filename2
mv filename1 filename_temp
mv filename2 filename1
mv filename filename_temp filename2
0
#!/bin/bash
##filename:jhname.sh
##写一个脚本用来互换两个文件的文件名
##write by 20160118
if [ -n "$1" ]&&[ -n "$2" ];then
a="$1"
b="$2"
mv $a ${a}.bak
mv $b $a
mv ${a}.bak $b
echo "the filename swith ok!"
exit 0
else
echo "the option is null,you must run again with two option!"
fi
##filename:jhname.sh
##写一个脚本用来互换两个文件的文件名
##write by 20160118
if [ -n "$1" ]&&[ -n "$2" ];then
a="$1"
b="$2"
mv $a ${a}.bak
mv $b $a
mv ${a}.bak $b
echo "the filename swith ok!"
exit 0
else
echo "the option is null,you must run again with two option!"
fi
0
#!/bin/bash
read -p "input file1 name:" file1
read -p "input file2 name:" file2
if [[ -z "$file1" && -z "$file2" ]]
then
echo "please input switch file name"
exit 0
fi
/bin/mv $file1 $file1.bak
/bin/mv $file2 $file1
/bin/mv $file1.bak $file2
echo "switch done please check"
read -p "input file1 name:" file1
read -p "input file2 name:" file2
if [[ -z "$file1" && -z "$file2" ]]
then
echo "please input switch file name"
exit 0
fi
/bin/mv $file1 $file1.bak
/bin/mv $file2 $file1
/bin/mv $file1.bak $file2
echo "switch done please check"
0
本帖最后由 zkq_315 于 2016-1-28 11:43 编辑
只是一个测试,并不通用!
#!/bin/bash
dir1="/aming/test/file1"
dir2="/aming/test/file2"
#echo $(basename $dir1)
mv $dir1 $(dirname $dir1)/mmm
mv $dir2 $dir1
mv $(dirname $dir1)/mmm $dir2
rm -rf $(dirname $dir1)/mmm
只是一个测试,并不通用!
#!/bin/bash
dir1="/aming/test/file1"
dir2="/aming/test/file2"
#echo $(basename $dir1)
mv $dir1 $(dirname $dir1)/mmm
mv $dir2 $dir1
mv $(dirname $dir1)/mmm $dir2
rm -rf $(dirname $dir1)/mmm
编辑回复