RH124-02 通过命令管理文件和目录

第二章 通过命令管理文件和目录

课程目标:

认识重要的系统目录

了解绝对路径和相对路径

通过命令创建,拷贝,移动,删除文件和目录

使用通配符匹配一个或多个文件

2.1  Linux文件系统的结构

Linux一切皆为文件

目录结构

常见的重要目录说明:

/usr

安装的软件,共享库,程序数据.重要的子目录有

/usr/bin用户命令

/usr/sbin管理员命令

/usr/local本地自定义安装的软件(一般为通过源代码安装的软件)

/etc

系统,软件的配置文件

/var

存放系统引导启动时产生的可变文件,文件通常动态更改的,例如数据库文件,缓存目录,日志文件,打印池文件,网页等

/run

系统启动后期运行的程序的运行时数据,包括进程的pid文件,锁文件等.这些文件一般在系统重启后会重建

/home

一般用户的主目录都在该目录下以用户名为名字的子目录的形式存在

/root

系统管理员root的主目录

/tmp

存放临时文件,文件一般存放超过10天以上都会自动删除,可以通过对系统的设定,更改删除这些临时文件的期限

/boot

存放系统引导时候需要的文件

/dev

存放设备文件

注意:

在rhel7中,  /bin , /sbin , /lib , /lib64都以字符链接的形式链接到/usr/目录下,例如

/bin --> /usr/bin

/sbin --> /usr/sbin

2.2 通过名字定位文件或目录

介绍 pwd,cd,ls命令  cd =change directory  改变目录  cd ~ 到根目录,cd -上次的目录 ,ctrl+l 或clear 是清屏

绝对路径: 所有路径描述都必须是以 "/" 开头

相对路径: 所有路径直接以文件名字或者 "." 或者 ".."开头   在当前绝对跟径下的相对目录。你的背景路径在哪里,相对路径在哪里。执行的结果是不同的,相对路径不一定对,因为依靠背景路径,比如在etc下 ls ./run无结果,但是在根下执行同样的命令就有输出。

../   上一级目录     ls ../看上面一个目录的内容

./当前目录

etc/sysconfig/

路径区分大小写

2.3 通过命令行工具管理文件   ctrl+shift+T  再打开一个窗口,可以作远程

命令作用需要掌握的参数

ls列表-l , -h , -a , -t ,d   list列表命令

touch创建空白文件

cp拷贝文件-a 保留属性 , -r

mv移动文件-f

rm删除文件-f , -r

mkdir创建目录-p , -f

练习:用student身份登陆desktop虚拟机上完成练习

在用户家目录下创建6个文件 song1.mp3 ~ song6.mp3

把上出创建的songX.mp3文件移动到Music目录下

在用户家目录下创建三个目录,分别为 friends,family,work

切换到friends目录下,把Music目录下的song1.mp3 ~ song3.mp3 拷贝到当前目录

切换到family目录下,把Music目录下的song4.mp3 ~ song6.mp3 移动到当前目录

切换到用户主目录

删除family目录

切换到friends目录,把目录下的所有文件删除

切换到主目录,把friends目录删除.

2.4  使用正则表达式匹配一个或多个文件

$ mkdir glob;cd glob

$ touch alfa bravo charlie delta echo able baker cast dog easy

*  匹配零个或若干个任意字符

?  匹配一个任意字符

[ac]* 包含a或者c任意一个字符开头的字符串

[ac]? 以a和c不开头,并且两个字符组成的字符串

{a..k}

~

\ 转义符,取消符号的特殊作用

课堂听讲笔记:

1、lsblk看磁盘大小

nautilus /usr 显示图形界面

2、ls命令

[[email protected] home]#    ls -l /home

total 4

drwx------. 17 student student 4096 Apr 29 10:31 student

[[email protected] home]#    ls -l -h  /home    可以看大小     命令:ls -lh /home   h一定要和l一起用,区别就是单位变成K了。

total 4.0K

drwx------. 17 student student 4.0K Apr 29 10:31 student

ls -a   才可以看到以.开头的文件或文件夹,这是为了保护一些重要文件,降低误操作的机率。

[[email protected] home]# ls /etc/cron.d/ -l -d    只看一个文件,不会显示很多

drwxr-xr-x. 2 root root 72 Jul 11  2014 /etc/cron.d/

3、cp命令

[[email protected] student]# cp /etc/hosts ./     copy到当前文件夹

[[email protected] student]# cp /etc/hosts ./newhosts copy到当前文件夹并改名   相对路径

[[email protected] student]# cp /etc/hosts /home/student/hosts2   绝对路径

[[email protected] student]# cp -a /etc/hosts /home/student/hosts3    保留文件的属性,如ls -lh时可以看到时间还是原来最初生成的时间

[[email protected] ~]$ cp/home/student/hosts2 /tmp/

bash: cp/home/student/hosts2: No such file or directory

[[email protected] ~]$ cp /home/student/hosts2 /tmp/

[[email protected] ~]$ cp -a /home/student/hosts2 /tmp/hosts22

[[email protected] ~]$ ll /tmp/hosts*

-rw-r--r--. 1 student student 231 Apr 29 13:39 /tmp/hosts2

-rw-r--r--. 1 student student 231 Apr 29 13:34 /tmp/hosts22

备份时需要保留属性,所以用这个-a参数   可以保留下组和名

拷目录:

[[email protected] ~]$ cp /home/student /tmp/  一般管理员不具有权限,需要加-r参数

cp: omitting directory ‘/home/student’

[[email protected] ~]$ ^C

[[email protected] ~]$ ^C

[[email protected] ~]$ cp -r /home/student /tmp/

[[email protected] ~]$ ls /tmp/student

Desktop    Downloads  hosts2  hostsnew  Pictures  tanpao.txt  Templates

Documents  hosts      hosts3  Music     Public    Tanpao.txt  Videos

[[email protected] ~]$ ls -ld /tmp/student

drwx------. 17 student student 4096 Apr 29 13:42 /tmp/student

4、移文件

[[email protected] ~]$ mv /home/student/tanpao.txt  /tmp

[[email protected] ~]$ su -              切换成超级用户

Password:

Last login: Sat Apr 29 10:55:59 CST 2017 on :0

[[email protected] ~]# mv /home/student/T

Tanpao.txt  Templates/

[[email protected] ~]# mv /home/student/Tanpao.txt /tmp/tanpao.txt     超级用户会提示是否覆盖

mv: overwrite ‘/tmp/tanpao.txt’?

[[email protected] ~]# exit    退出超级用户

logout

[[email protected] ~]$ ^C

5、删除文件(相对跟径和绝对跟径)

[[email protected] ~]$ rm hosts2

rm: remove write-protected regular file ‘hosts2’? y

[[email protected] ~]$ rm /home/student/hosts

rm: remove write-protected regular file ‘/home/student/hosts’? y

[[email protected] ~]$ ls -lh /home/student/

total 8.0K

drwxr-xr-x. 2 student student   6 Jan  5  2015 Desktop

drwxr-xr-x. 2 student student   6 Jan  5  2015 Documents

drwxr-xr-x. 2 student student   6 Jan  5  2015 Downloads

-rw-r--r--. 1 root    root    231 Jul 11  2014 hosts3

-rw-r--r--. 1 root    root    231 Apr 29 13:33 hostsnew

drwxr-xr-x. 2 student student   6 Jan  5  2015 Music

drwxr-xr-x. 2 student student   6 Jan  5  2015 Pictures

drwxr-xr-x. 2 student student   6 Jan  5  2015 Public

-rw-r--r--. 1 root    root      0 Apr 29 13:30 Tanpao.txt

drwxr-xr-x. 2 student student   6 Jan  5  2015 Templates

drwxr-xr-x. 2 student student   6 Jan  5  2015 Videos

6、创建目录

[[email protected] ~]$ mkdir /tmp/test

[[email protected] ~]$ ls -ld /tmp/test

drwxrwxr-x. 2 student student 6 Apr 29 13:51 /tmp/test

[[email protected] ~]$

[[email protected] etc]$ cp /etc/hosts /tmp/test/

[[email protected] etc]$ ls /tmp/test

hosts

[[email protected] etc]$

删除目录

[[email protected] /]$ rm /tmp/test

rm: cannot remove ‘/tmp/test’: Is a directory

[[email protected] /]$ rm -r /tmp/test   删除目录需要-r,同时不提示,但是root用户就会提示

如果需要提示

[[email protected] /]$ rm -i -r /tmp/test

rm: descend into directory ‘/tmp/test’? n

用超级管理员来作操作:

[[email protected] ~]# mkdir /tmp/test

[[email protected] ~]# cp /etc/host* /tmp/test

[[email protected] ~]# cd /tmp/test

[[email protected] test]# ls

host.conf  hosts  hosts.allow  hosts.deny

删除所有文件(千万注意,不要在/目录下作这个操作

[[email protected] ~]# cd /tmp/test

[[email protected] test]# ls

host.conf  hosts  hosts.allow  hosts.deny

[[email protected] test]# pwd

/tmp/test

[[email protected] test]# rm ./*

rm: remove regular file ‘./host.conf’? y

rm: remove regular file ‘./hosts’? y

rm: remove regular file ‘./hosts.allow’? y

rm: remove regular file ‘./hosts.deny’? y

rm -rf ./* 这个可以不用提示一下删除掉

一次性创建我个目录

[[email protected] test]# mkdir /tmp/test/a/b

mkdir: cannot create directory ‘/tmp/test/a/b’: No such file or directory

[[email protected] test]# mkdir /tmp/test/a

[[email protected] test]# mkdir /tmp/test/a/b

[[email protected] test]# mkdir -p /tmp/test/a/b

时间: 2024-08-27 18:34:32

RH124-02 通过命令管理文件和目录的相关文章

linux基础命令-管理文件和目录

今天学习了对Linux命令的理解和Linux系统中对文件和目录进行管理的基础命令. 在Linux系统中需要通过命令来实现某一项功能,Linux命令的执行需要依赖于Shell命令解释器.Shell是在Linux系统中运行的一种特殊程序,Shell解释器位于用户和内核之间,Shell接收到用户的命令并进行解释,然后将需要执行的的操作传递给内核来执行.根据Linux命令和Shell程序的关系,分为内部命令和外部命令. Linux命令对大小写敏感,格式:命令字 [选项] [参数] 以下辅助操作可以提高输

在Linux中用chattr和lsattr命令管理文件和目录属性

PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的linux系统都是2.6以上内核了.通过chattr命令修改属性能够提高系统的安全性,但是它并不适合所有的目录.chattr命令不能保护/./dev./tmp./var目录.lsattr命令是显示chattr命令设置的文件属性. 这两个命令是用来查看和改变文件.目录属性的,与chmod这个命令相比,ch

Linux基本命令(1)管理文件和目录的命令

Linux管理文件和目录的命令 命令 功能 命令 功能 pwd 显示当前目录 ls 查看目录下的内容 cd 改变所在目录 cat 显示文件的内容 grep 在文件中查找某字符 cp 复制文件 touch 创建文件 mv 移动文件 rm 删除文件 rmdir 删除目录 1.1 pwd命令 该命令的英文解释为print working directory(打印工作目录).输入pwd命令,Linux会输出当前目录. 1.2 cd命令 cd命令用来改变所在目录. cd /      转到根目录中 cd

linux基础命令:(1)管理文件和目录的命令

1.管理文件和目录的命令:(1)pwd:print working directory(打印工作目录),输入pwd时,就是要求linux系统显示当前位置.(2)cd命令:cd命令用来改变所在目录.总是返回到当前的主目录.如果需要转换到其他目录,则需要一个路径名.[绝对路径与相对路径的区别:如果一个路径的第一个字符是/,那么这个路径就是绝对路径] cd ..告诉系统向上移到当前目录所在目录的直接上级目录中,如要上移两级目录,则为cd ../..(3)ls命令:用来查看目录的内容.   ls -a

Linux 常用命令之文件和目录

1. cd 命令格式:cd  [dirName](cd和目录之间使用空格隔开) 作用:切换当前目录至dirName. 实例: cd /home 切换当前目录到/home (查看当前目录命令为pwd) cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd ~ 进入个人的主目录 2. ls 命令格式:ls [选项] [目录名] (目录名为空时表示当前目录) 作用: 查看目录中的文件 常用选项: -a, –all 列出目录下的所有文件,包括以 . 开头的隐含文件. -l 除了文件名之外,

Linux常用命令之文件和目录操作命令

以下是linux操作系统常用的文件和目录操作命令: cd /home 进入 '/ home' 目录' cd .. 返回上一级目录 cd ../.. 返回上两级目录 cd 进入个人的主目录 cd ~user1 进入个人的主目录 cd - 返回上次所在的目录 pwd 显示工作路径 ls 查看目录中的文件 ls -F 查看目录中的文件 ls -l 显示文件和目录的详细资料 ls -a 显示隐藏文件 ls *[0-9]* 显示包含数字的文件名和目录名 tree 显示文件和目录由根目录开始的树形结构(1)

chown命令修改文件或目录的所有者

在linux中, chown命令用于修改文件或者目录的所有者 通常是root用户或者有权限的用户使用. 格式:chown [选项]  用户或组  文件 说明: chown将指定文件的所有改为指定的用户或组.用户可以是用户名或 用户I D:组可以是组名或组ID. 文件是以空格分开的要改变权限的文件列表,支持通配符. 选项含义: -R 递归式地改变指定目录及其下的所有子目录和文件的拥有者. -v 显示chown命令所做的工作. 举例 (1)chown wang shiyan.c          

使用命令chown改变文件、目录的所有权;使用chgrp命令修改文件、目录的组;

使用命令chown改变文件.目录的所有权 超级用户root 或者具有超级用户身份的用户(这里系统的所有者nling 就是一个超级用户),有权利修改文件/目录的所有权,这是Linux 系统所必须确定的.超级用户可以通过使用chown 命令,变更一个文件或一个目录的所有权,这个命令的语法如下: chown newuser file or directory 例如: chown damboo tools.txt 提示:这个命令可以使用户damboo 成为文件tools.txt 的新所有者. 如果超级用

查找某个命令相应文件所在目录的方法

问题:有时候需要查看某个命令相应文件所在目录,怎么办呢. 方法:windows用where命令(如where cmd),linux用whereis命令(如whereis apachectl). 版权声明:本文为博主原创文章,未经博主允许不得转载.