linux用户与文件基础命令介绍(1)

2019-09-23 09:14:50刘景俊

另外如果cd后面任何参数都没有的时候,执行的效果是切换回家目录:

[root@centos7 /]# cd
[root@centos7 ~]# pwd
/root

3、ls 列出目录内容

ls [OPTION]... [FILE]...
当命令ls后不跟任何参数的时候显示当前目录的内容

[root@centos7 ~]# ls
anaconda-ks.cfg install.log install.log.syslog

上面的例子显示了/root目录下的三个文件anaconda-ks.cfg、anaconda-ks.cfg、anaconda-ks.cfg。
选项-l可以使ls命令的结果以长格式显示:

[root@centos7 ~]# ls -l
total 84
-rw------- 1 root root 1666 Jan 14 2016 anaconda-ks.cfg
-rw-r--r-- 1 root root 55745 Jan 14 2016 install.log
-rw-r--r-- 1 root root 5039 Jan 14 2016 install.log.syslog

显示结果的意思后述。

4、mkdir 创建目录

mkdir [OPTION]... DIRECTORY...
通常的使用方法是命令名之后直接跟目录名(可以多个),这里说一下linux文件命名的规则:linux允许文件名使用除字符/之外的所有字符,文件名的最大字符数为255(中文字符为127),linux不鼓励在文件名中出现特殊字符(容易引起混淆),文件名对大小写敏感。文件或目录数量限制与所使用的文件系统有关。
如当前目录下创建temp目录并用ls查看:

[root@centos7 ~]# mkdir temp
[root@centos7 ~]# ls
anaconda-ks.cfg install.log install.log.syslog temp

选项-p可以递归地创建子目录,如进入temp并创建目录dir1和dir2,dir1的子目录test:

[root@centos7 ~]# cd temp
[root@centos7 temp]# mkdir -p dir1/test dir2
[root@centos7 temp]# ls
dir1 dir2
[root@centos7 temp]# cd dir1
[root@centos7 dir1]# ls
test

5、touch “创建文件”

touch [OPTION]... FILE...
其实此命令作用是修改文件时间,当指定的文件不存在时就会创建新文件。由于文件时间的更改可以通过许多其它途径,反而许多用户都误以为它就是创建文件的命令。如在temp目录下创建文件file1 在temp的子目录dir1下创建文件file2:

[root@centos7 temp]# touch file1 dir1/file2
[root@centos7 temp]# ls
dir1 dir2 file1
[root@centos7 temp]# cd dir1
[root@centos7 dir1]# ls
file2 test

6、useradd 添加账号

useradd [options] name
如创建一个名叫learner的账号:

[root@centos7 dir1]# useradd learner
useradd命令默认在创建用户账号的同时也会创建用户的家目录,同时更新系统中与用户相关的配置文件(linux中有许多配置文件,它们的作用是为软件运行设置环境信息、参数等,它们通常是纯文本的格式,方便用户变更其内容以改变软件运行环境。在linux中,大多数配置文件都处于目录/etc内,如与用户管理相关的配置文件:/etc/passwd,/etc/group,/etc/shadow,/etc/gshadow等)。