Shell | 文件和目录 *** 作

Shell | 文件和目录 *** 作,第1张

-p : 递归创建目录,若父目录不存在则依次创建

-r : 将【目录】及以下文件删除

-f : 强制删除

全拼:move

功能:将文件或目录转移、改名

-r : 复制目录

-r : 递归复制整个目录

1)看下面的脚本a1.sh,假设要显示目录/home/user/tmp/下面的所有的文件和(子)目录的名字:\x0d\x0a\x0d\x0a$ cat a1.sh\x0d\x0a#!/bin/bash\x0d\x0a\x0d\x0afor file in /home/user/tmp/*\x0d\x0ado\x0d\x0aecho $file\x0d\x0adone\x0d\x0a\x0d\x0a2)假设目录/home/user/tmp/下面的所有的文件和(子)目录如下:\x0d\x0a\x0d\x0a$ ls\x0d\x0a1.txt 2.txt a1.sh a.sh b.sh email_back m1.doc tmp\x0d\x0a\x0d\x0a3)运行脚本:\x0d\x0a$ a1.sh (或者./a1.sh)\x0d\x0a/home/user/tmp/1.txt\x0d\x0a/home/user/tmp/2.txt\x0d\x0a/home/user/tmp/a1.sh\x0d\x0a/home/user/tmp/a.sh\x0d\x0a/home/user/tmp/b.sh\x0d\x0a/home/user/tmp/email_back\x0d\x0a/home/user/tmp/m1.doc\x0d\x0a/home/user/tmp/tmp\x0d\x0a\x0d\x0a4)脚本a1.sh的作用只是显示文件和子目录的列表,要显示文件的内容,脚本继续改造,内容如下,看脚本a2.sh:\x0d\x0a\x0d\x0a$ cat a2.sh\x0d\x0a#!/bin/bash\x0d\x0a\x0d\x0afor file in /home/shiqingd/tmp/*\x0d\x0ado\x0d\x0aecho $file\x0d\x0aif [ -f $file ]then\x0d\x0acat $file\x0d\x0afi\x0d\x0adone\x0d\x0a\x0d\x0a脚本a2.sh可以达到目的。

1)看下面的脚本a1.sh,假设要显示目录/home/user/tmp/下面的所有的文件和(子)目录的名字:

$ cat a1.sh

#!/bin/bash

for file in /home/user/tmp/*

do

echo $file

done

2)假设目录/home/user/tmp/下面的所有的文件和(子)目录如下:

$ ls

1.txt 2.txt a1.sh a.sh b.sh email_back m1.doc tmp

3)运行脚本:

$ a1.sh (或者./a1.sh)

/home/user/tmp/1.txt

/home/user/tmp/2.txt

/home/user/tmp/a1.sh

/home/user/tmp/a.sh

/home/user/tmp/b.sh

/home/user/tmp/email_back

/home/user/tmp/m1.doc

/home/user/tmp/tmp

4)脚本a1.sh的作用只是显示文件和子目录的列表,要显示文件的内容,脚本继续改造,内容如下,看脚本a2.sh:

$ cat a2.sh

#!/bin/bash

for file in /home/shiqingd/tmp/*

do

echo $file

if [ -f $file ]then

cat $file

fi

done

脚本a2.sh可以达到目的。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7946324.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存