文件通配符linux,文件管理命令及通配符练习

1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例

按增删改查的思路来

增:

touch filename:这个命令本身不是用来新建文件的,它是用来修改文件的时间属性,不过默认情况时当filename不存在时,它会默认建立一个空文件

mkdir:

常用参数

-p:相当于递归创建目录

-v:显示出创建过程中的信息

cp :

常用参数

-r:递归复制目录,用于复制目录时使用

-a:归档复制,相当于保留原文件的所有属性信息

几条结论:

[root@zejin240 test]# tree

.

├── dir1

└── f1

drwxr-xr-x. 2 root root 4096 Nov  6 11:46 dir1

-rw-r–r–. 1 root root    0 Nov  6 11:45 f1

[root@zejin240 test]# cp f1 f2

[root@zejin240 test]# cp f1 dir1

[root@zejin240 test]# tree

.

├── dir1

│   └── f1

├── f1

└── f2

[root@zejin240 test]# cp f1 f2

cp: overwrite `f2'? N

1. cp时,如果源是一个文件,目标是一个未存在的文件名时,就会把源文件复制到目标,如果目标是一个文件夹时,则会复制到这个文件夹里面去,如果目标是已经存在的一个文件时,会提示是否要覆盖。

[root@zejin240 test]# cp dir1 dir2

cp: omitting directory `dir1'

[root@zejin240 test]# ll

total 4

drwxr-xr-x. 2 root root 4096 Nov  6 15:04 dir1

-rw-r–r–. 1 root root    0 Nov  6 11:45 f1

-rw-r–r–. 1 root root    0 Nov  6 15:04 f2

[root@zejin240 test]# cp -r dir1 dir2

[root@zejin240 test]# tree

.

├── dir1

│   └── f1

├── dir2

│   └── f1

├── f1

└── f2

[root@zejin240 test]# cp -r dir1 dir2

[root@zejin240 test]# tree

.

├── dir1

│   └── f1

├── dir2

│   ├── dir1

│   │   └── f1

│   └── f1

├── f1

└── f2

2.如果源是一个文件夹,得加上-r参数,目标是一个未存在的文件名时,就会把源文件夹复制成目标文件夹,如果目标是一个已存在的文件夹时,就会把源文件夹复制到目标文件夹下。

[root@zejin240 test]# cp -r f1 f2 dir1 dir3

cp: target `dir3' is not a directory

[root@zejin240 test]# mkdir dir3

[root@zejin240 test]# cp -r f1 f2 dir1 dir3

[root@zejin240 test]# tree

.

├── dir1

│   └── f1

├── dir2

│   ├── dir1

│   │   └── f1

│   └── f1

├── dir3

│   ├── dir1

│   │   └── f1

│   ├── f1

│   └── f2

├── f1

└── f2

3. cp命令时,会把最后一个当成目标,其它的文件及文件夹当成源,而且源有多个时,最后一个必须是一个已经存在的文件夹

最普通的文件复制:cp srcfile destfile

最普通的文件夹复制:cp -r srcdir destdir

多个文件或文件夹复制到一个文件夹去 cp -r srcfile srcdir existdesdir

rm 默认不删除目录

常用参数

-f:强制删除,不用提示

-r:递归删除,当删除目标为文件时,需要此参数

[root@zejin240 test]# ll

total 12

drwxr-xr-x. 2 root root 4096 Nov  6 15:04 dir1

drwxr-xr-x. 3 root root 4096 Nov  6 15:26 dir2

drwxr-xr-x. 3 root root 4096 Nov  6 15:28 dir3

-rw-r–r–. 1 root root    0 Nov  6 11:45 f1

-rw-r–r–. 1 root root    0 Nov  6 15:04 f2

[root@zejin240 test]# rm f2

rm: remove regular empty file `f2'? y

[root@zejin240 test]# rm f1 -f

[root@zejin240 test]# rm dir3

rm: cannot remove `dir3': Is a directory

[root@zejin240 test]# rm dir3 -rf

[root@zejin240 test]# ll

total 8

drwxr-xr-x. 2 root root 4096 Nov  6 15:04 dir1

drwxr-xr-x. 3 root root 4096 Nov  6 15:26 dir2

mv 重命名源文件名成目标文件名,也可以将文件移到某个目录下

常用用法:

mv file1 file2 将文件file1重命名为file2

mv dir1 dir2 将目录dir1重命名为dir2

mv file1 dir1 dir2 将文件file1及目录dir1 移动到dir3目录下

[root@zejin240 test]# ll

total 8

drwxr-xr-x. 2 root root 4096 Nov  6 15:39 dir2

drwxr-xr-x. 2 root root 4096 Nov  6 15:39 dir3

-rw-r–r–. 1 root root    0 Nov  6 15:38 f3

[root@zejin240 test]# tree

.

├── dir2

├── dir3

└── f3

2 directories, 1 file

[root@zejin240 test]# mv f3 f1

[root@zejin240 test]# mv dir3 dir1

[root@zejin240 test]# tree

.

├── dir1

├── dir2

└── f1

2 directories, 1 file

[root@zejin240 test]# mv f1 dir2 dir1

[root@zejin240 test]# tree

.

└── dir1

├── dir2

└── f1

ls

2.bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示

每个命令执行后,状态返回结果保留在$?中,其中0表示执行正常,1-255表示执行异常

[root@zejin240 test]# echo "a"

a

[root@zejin240 test]# echo $?

0

[root@zejin240 test]# echoo

-bash: echoo: command not found

[root@zejin240 test]# echo $?

127

3.  创建/tmp目录下的a_c,a_d,b_c,b_d文件夹

[root@zejin240 tmp]# mkdir /tmp/{a,b}_{c,d}

[root@zejin240 tmp]# ll /tmp/

total 16

drwxr-xr-x. 2 root root 4096 Nov  6 09:58 a_c

drwxr-xr-x. 2 root root 4096 Nov  6 09:58 a_d

drwxr-xr-x. 2 root root 4096 Nov  6 09:58 b_c

drwxr-xr-x. 2 root root 4096 Nov  6 09:58 b_d

创建如下目录结构

[root@zejin240 tmp]# tree /tmp/mylinux/

/tmp/mylinux/

├── bin

├── boot

│   └── grub

├── dev

├── etc

│   ├── rc.d

│   │   └── init.d

│   └── sysconfig

│       └── network-scripts

├── lib

│   └── modules

├── lib64

├── proc

├── sbin

├── sys

├── tmp

├── usr

│   └── local

│       ├── bin

│       └── sbin

└── var

├── lock

├── log

└── run

[root@zejin240 tmp]# mkdir -p /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}

4.文件的元数据信息有哪些,分别表示什么含义,如何查看,如何修改文件的时间戮信息

文件权限rwx,文件的modify time,access time,change time,文件大小,文件所属的用户及用户组,链接数

可以用stat命令查看时间戮信息,touch命令修改文件的时间戮信息

touch t_file

-a :修改access time属性

-m:修改modify time属性

-d:指定时间

[root@zejin240 tmp]# touch -a tfile -d '2016-01-01'

[root@zejin240 tmp]# stat tfile

File: `tfile'

Size: 0         Blocks: 0          IO Block: 4096   regular empty file

Device: 802h/2050dInode: 676569      Links: 1

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2016-01-01 00:00:00.000000000 +0800

Modify: 2016-11-06 11:20:40.002008933 +0800

Change: 2016-11-06 11:21:10.405007827 +0800

[root@zejin240 tmp]# touch -m tfile -d '2016-02-01'

[root@zejin240 tmp]# stat tfile

File: `tfile'

Size: 0         Blocks: 0          IO Block: 4096   regular empty file

Device: 802h/2050dInode: 676569      Links: 1

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2016-01-01 00:00:00.000000000 +0800

Modify: 2016-02-01 00:00:00.000000000 +0800

Change: 2016-11-06 11:21:36.291007800 +0800

如果不加-d选项,则默认为当前时间

-r :参考某个文件的时间属性

[root@zejin240 tmp]# stat tfile1

File: `tfile1'

Size: 0         Blocks: 0          IO Block: 4096   regular empty file

Device: 802h/2050dInode: 676570      Links: 1

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2016-11-06 11:26:03.023007347 +0800

Modify: 2016-11-06 11:26:03.023007347 +0800

Change: 2016-11-06 11:26:03.023007347 +0800

[root@zejin240 tmp]# touch tfile1 -r tfile

[root@zejin240 tmp]# stat tfile1

File: `tfile1'

Size: 0         Blocks: 0          IO Block: 4096   regular empty file

Device: 802h/2050dInode: 676570      Links: 1

Access: (0644/-rw-r–r–)  Uid: (    0/    root)   Gid: (    0/    root)

Access: 2016-01-01 00:00:00.000000000 +0800

Modify: 2016-02-01 00:00:00.000000000 +0800

Change: 2016-11-06 11:26:34.211008000 +0800

5.如何定义一个命令的别名,如何在命令中引用另一个命令的执行结果

alias youraliasname=`your command`

[root@zejin240 test]# alias mkdir='mkdir -p'

[root@zejin240 test]# mkdir /tmp/a/b/c/d

[root@zejin240 test]# ll /tmp/a

total 4

drwxr-xr-x. 3 root root 4096 Nov  6 19:40 b

[root@zejin240 test]# tree /tmp/a

/tmp/a

└── b

└── c

└── d

6.显示/var目录下所有以l开头,以一个小写字母结尾,且中间至少出现一位数据(可以有其它字符)的文件或目录

ll /var/l*[0-9]*[a-z]

7 显示/etc目录下,以任意一个数字开头,且以非数字结尾的文件或目录

ll /etc/[0-9]*[!0-9]

8.显示/etc目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录

ll /etc/[![:alpha:]][[:alpha:]]*

9.在/tmp目录下创建以tfile开头,后跟当前日期和时间的文件,文件名如:/tfile-2016-05-27-09-43-22

[root@zejin240 tmp]# touch /tmp/tfile-`date +%F-%H-%M-%S`

[root@zejin240 tmp]# ll /tmp/tfile-2016-11-06-10-24-55

-rw-r–r–. 1 root root 0 Nov  6 10:24 /tmp/tfile-2016-11-06-10-24-55

10、复制/etc目录下所有以p开头,以非数字结尾的文件或目录到/tmp/mytest1目录中

cp -r /etc/p*[![:digit:]] /tmp/mytest1

11.复制/etc目录下所有以.d结尾的文件或目录至/tmp/mytest2目录中

cp -r /etc/*.d /tmp/mytest2/

12.复制/etc目录下所有以1或m或n开头,惟.conf结尾的文件至/tmp/mytest3目录中

[root@zejin240 tmp]# cp   /etc/{1,m,n}*.conf   /tmp/mytest3/

原创文章,作者:chenzejin,如若转载,请注明出处:http://www.178linux.com/58008

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值