linux基础命令 一、cd命令 全称:change Directory 做用:修改所在的目录 格式:cd /etc/sysconfig pwd 特殊操做: cd / :直接切换到根目录下 cd ~ :返回到当前用户家目录 cd ..:返回到当前所在位置的上一级目录 cd - :返回到上一次所在的目录(返回切换目录以前的那个目录) 补充: 家目录本质上就是一个目录而已,linux中每一个用户都有一个本身的专属, 这个目录就是用户的家目录,用户能够在家目录下执行任意操做 用户家目录有两种状况 例如:用户zhangsan,家目录/home/zhangsan目录 例如:用户zhangsan执行命令cd ~,就切换到了/home/zhangsan 用户sy执行命令cd ~,就切换到了/home/sy useradd zhangsan passwd cd ~ pwd useradd lisi passwd cd ~ pwd 练习: 一、切换到目录/etc/sysconfig [root@localhost ~]# cd /etc/sysconfig [root@localhost sysconfig]# pwd /etc/sysconfig 二、切换到/home 目录下 [root@localhost ~]# cd /home/ [root@localhost home]# pwd /home 三、切换到目录/etc/sysconfig [root@localhost ~]# cd /etc/sysconfig [root@localhost sysconfig]# pwd /etc/sysconfig 四、查看当前所在位置 [root@localhost ~]# su zs [zs@localhost root]$ cd /etc/ [zs@localhost etc]$ cd ~ [zs@localhost ~]$ pwd /home/zs 五、返回到系统的根目录 [zs@localhost /]$ su root Password: [root@localhost /]# cd / [root@localhost /]# pwd / 二、pwd命令 全称:Print Working Directory 做用:打印所在目录(工做目录) linux中所有的文件都在根目录存放 /:就表示linux的根目录(目录名是/) linux 是单根系统 windows 是多根系统 三、mkdir命令 全称:make Directory 做用:建立目录 格式:mkdir [OPTION]... DIRECTORY... 例子:建立三个目录bajie wukong guodegang [root@localhost ~]# mkdir bajie wukong guodegang [root@localhost ~]# ls bajie guodegang test1 wukong 例子:建立三个目录/yunwei/linux/python [root@localhost ~]# mkdir yunwei/linux/python mkdir: cannot create directory `yunwei/linux/python': No such file or directory 报错、没有父目录 选项: -p:建立目录的时候先建立父目录,再建立子目录。 -v:显示建立目录的过程信息 [root@localhost ~]# mkdir -p yunwei/linux/python [root@localhost ~]# ls bajie guodegang test1 wukong yunwei 例子:建立目录/a1/a2/a3并显示建立目录的过程信息 [root@localhost ~]# mkdir -pv a1/a2/a3 mkdir: created directory `a1' mkdir: created directory `a1/a2' mkdir: created directory `a1/a2/a3' [root@localhost ~]# ls a1 bajie guodegang test1 wukong yunwei 例子:在home下建立yw1 #mkdir -p/home/yw1 例子:在根目录下建立目录yw1 #mkdir /yw1 例子:在当前用户的家目录下建立目录yw1 #mkdir /root/yw1 #mkdir ~/yw1 例子:在当前位置下建立目录yw1 #mkdir yw1 #mkdir ./yw1 例子:在上一级目录下建立目录yw1 #cd .. #mkdir yw1 或者 #mkdir ../yw1 补充:表示路径的时候 ./: 表示当前位置(若是不指定路径也是表示当前位置) ../: 表示上一级目录 mkdir abc >>> 在当前位置下建立目录abc mddir ./abc >>>在当前位置下建立目录abc echo "mkdir abc" 输出mkdir abc [root@localhost tmp]# echo "mkdir abc" mkdir abc [root@localhost tmp]# ls yw1 四、touch命令 做用:建立空白文件 例子:建立三个文件 a.txt b.doc c.ppt touch a.txt b.doc d.ppt [root@localhost ~]# ls a.txt b.doc d.ppt 例子:根建立文件1.txt touch /1.txt 例子:在根下建立三个文件1.txt 2.txt 3.txt [root@localhost ~]# touch /1.txt /2.txt /3.txt [root@localhost ~]# cd / [root@localhost /]# ls 1.txt 2.txt bin cgroup etc lib lost+found misc net proc sbin srv tmp var 1.xtx 3.txt boot dev home lib64 media mnt opt root selinux sys usr 补充:花括号展开 需求:三个目录a b c ,在三个目录中分别建立文件1.txt 2.txt 3.txt #mkdir a b c #touch a/1.txt a/2.txt a/3.txt #touch b/1.txt a/2.txt a/3.txt #touch c/1.txt a/2.txt a/3.txt 或者 #mkdir a b c #touch {a,b,c}/{1.txt,2.txt,3.txt} 或者 #mkdir a b c #touch {a,b,c}/{1,2,3}.txt #touch file.{doc,ppt,txt} = touch file.doc file.ppt file.txt #touch {1,2,3}.txt = touch 1.txt 2.txt 3.txt 例子:建立三个目录 a b c,在每一个目录下建立文件a.txt #mkdir a b c #touch a/a.txt b/a.txt c/a.txt 或者 touch {a,b,c}/a.txt 需求:建立一个文件,文件名是 年-月-日-小时:分钟:秒.log [root@localhost ~]# touch `date +%F-%H:%M:%S`.log [root@localhost ~]# touch `date +%F-%H:%M:%S`.log [root@localhost ~]# ls 2019-04-12-04-21:55.log 2019-04-12-04:22:35.log 2019-04-12-04:22:33.log 2019-04-12-04:22:36.log 需求:建立一个文件,文件名是 主机名-小时:分钟:秒 [root@localhost ~]# hostname zs [root@localhost ~]# touch `hostname`-`date +%H:%M:%S`.log [root@localhost ~]# ls zs-04:34:52.log 补充:linux 中的变量和输出 echo 补充:linux中的引号 单引号:'' 弱引用,会将变量中的内容原样输出 [root@localhost ~]# name="I love python" [root@localhost ~]# echo '$name' $name 双引号:"" 强引用,是将变量名替换成变量值 [root@localhost ~]# name="I love linux" [root@localhost ~]# echo "$name" I love linux 反引号:命令替换成命令的执行结果