基本概念
- 目录:文件夹
- 根目录:所有文件最上级目录"/"
- 子目录:位于其他目录下层的目录【/home/tarena】
- 父目录:子目录的上层目录".."
- 当前目录:"."标记
- 主目录:"~"标记,位于/home下,目录名和用户名相同
- 路径:
- 绝对路径
- 相对路径
Linux常见命令
格式:命令 【-选项】 【参数】(中间带空格)
- 清屏:clear
- 打印当前目录:pwd
- 改变当前目录:cd 【制定的目标目录】
cd /home/tarena 切换到/home/tarena目录下 cd .. 进入上级目录 cd ./tarena 进入当前目录下的tarena目录里面 cd ~ 进入主目录 cd / 进入根目录
- 显示目录内容:ls
- ls -a,显示全部内容,包括隐藏文件
- ls -l,显示详细信息,包括文件属性
ls /home/tarenacd 查看/home/tarena目录下的内容 ls -l /home/tarena 查看/home/tarena目录里面内容的详细信息 ls /usr/include/stdio.h 查看此文件是否存在 ls -l /usr/include/stdio.h查看此文件的详细信息 ls 查看当前目录下的内容 ls -l 查看当前目录下内容的详细信息 ls ..查看上一级目录里面内容的详细信息 ls -l .. 查看上一级目录里面内容的详细信息 ls stdio.h 查看当前目录下是否存在stdio.h文件 ls -l stdio.h 查看当前目录下stdio.h文件的详细信息 ls ~查看主目录里面的内容 ls -l ~查看主目录里面内容的详细信息 ls / 查看根目录里面的内容 ls -l / 查看根目录里面内容的详细信息
- 创建空文件:touch 【文件】
touch /home/tarena/hello.txt在/home/tarna目录下创建空文件hello.txt touch hello.txt 在当前目录下创建空文件hello.txt touch ~/hollo2.txt在主目录下创建空文件hello2.txt
- 创建空目录:mkdir
- 如果需要一次创建多级目录,需要使用-p选项:mkdir -p 目录1/目录2/目录3
mkdir /home/tarena/hello 在/home/tarena目录下创建空目录hello mkdir hello1 在当前目录下创建空目录hello1 mkdir ~/hello2在主目录下创建空目录hello2 mkdir -p hello3/hello4/hello5
- 修改权限chmod
- 符号模式
- chmod u/g/o/a +/-/= r/w/x 文件
- u/g/o/a:属主/属组/其他/所有
- +/-/= :增加/减去/设置
- r/w/x:读取/写入/执行
- 符号模式
chmod a+x hello.txt chmod u-r hello.txt chmod a=rwx hello.txt
-
- 数字模式
- chmod 644 myfile
- 将权限用三位数字表示,有左到右依次表示对应属主,同组和其他,
- 每一位都是从4(读)/2(写)/1(执行)三个数中选出若干个取和
- chmod 644 myfile
- 数字模式
chmod 777 hello.txt chmod 644 hello.txt chmod544 hello.txt
- 拷贝:cp
- 拷贝文件:cp 【源文件】 【目标文件】
- 原名拷贝文件:cp 【源文件】【存在目录】
- 拷贝目录:cp -fr 【源目录】 【存在目录】
- 原名拷贝目录:cp -fr 【 源目录】 【存在目录】
mkdir /home/tarena/cptest cd /home/tarena/cptest/ touch hello.txt mkdir hello ls cp hello.txt hello.txt//拷贝hello.txt生成一个新文件hello1.txt cp -fr hello hello1//拷贝hello.txt到一个新的目录hello1 cp hello.txt hello //拷贝hello.txt文件到hello目录里去 cp -fr hello hello1 //拷贝hello目录到hello1目录里 cp -fr hello1 ~/hello2 //拷贝hello1目录到主目录/home/tarena目录里面去并且命名为hello2目录
- 更名和移动:mv
- 更名文件:mv 【源文件】 【目标文件】
- 移动文件:mv 【源文件】 【存在目录】
- 更名文件:mv 【源目录】 【目标目录】
- 移动目录:mv 【源目录】 【存在目录】
mkdir /home/tarena/mvtest cd /home/tarena/mvtest touch hello.txt mkdir hello ls mv hello.txt hello.txt ls mv hello.txt hello1 ls ls hello1 mkdir hello2 ls mv hello2 hello1 ls ls hello1
- 删除:rm
- 删除文件:rm 文件1 文件2 ……文件n
- 删除目录:rm -fr 目录1 目录2 ……目录n
rm /home/tarenacd/hello.txt cd /home/tarena rm -fr mvtest ls /home/tarena/hello.txt ls /home/tarena/cptest sudo rm -fr //使用管理员权限强制删除
- echo命令
- echo 信息:将信息输出到显示器上
- echo 信息 > 文件:将信息输出到文件中保存但是先把文件的内容清空后再保存
- echo 信息 >> 文件:将信息保存到文件的尾部,文件原先内容依旧保留
echo belist touch /home/tarena/hello.txt echo belist > belist1 cat hello.txt echo belist2 > hello.txt cat hello.txt echo belist3 >> hello.txt cat hello.txt echo 1234 >> hello.txt cat hello.txt echo belist4 >hello.txt
- 创建链接:ln命令
- 硬链接:ln 【目标文件】 【链接文件】
- 软链接:ln -s 【目标文件】 【链接文件】
- 显示文件
- cat 【文件】:显示文件内容
- more 【文件】:分屏显示文件内容
- head -n 【文件】:显示文件前n行(缺省10行)内容
- tail -n 【文件】:显示文件后n行(缺省10行)内容
- tail -f 【文件】:随文件增长,显示其追加内容
cat /user/include/stdio.h //全部显示 more /user/include/stdio.h //分屏显示,回车走一行,空格走一屏,q退出 head -15 /usr/include/stdio.h 只看文件的前15行 tail -15 /user/include/stdio.h touch /home/tarena/hello.txt tail -f /home/tarena/hello.txt //此刻停止不动,实验完毕可以ctrl+c强制退出 echo 12345 >> /home/tarena/hello.txt//然后去终端观察有何不同 ctrl+shift+c可以在一个终端中创建另一个终端,切换按alt+数字(终端编号)
- 查找文件或目录:find 【目录 】【文件】
- 查找特定目录下,满足特定条件的文件或目录。
- -name 【文件或目录名】
- -perm 【权限】
- -user 【属主】
- -group 【属组】
- -ctime/atime/mtime -n/+n
- -type d/f/l/p/b/c
- -size nc
- 查找特定目录下,满足特定条件的文件或目录。
cd /home/tarena find . -name hello.txt 在当前目录下找一个hello.txt的文件 find /user/include -name stdio.h 到/user/include目录下找stdio.h文件 find . -user tarena -group tarena -name hello.txt find . -perm 664 -user tarena -group tarena -name hello.txt find . -mtime +10 -perm 664 -user tarena -group tarena -name hell.txt find . -mtime +10 -size 100c -perm 664 -user tarena -group tarena -name hello.txt
- 查找内容:grep 【选项】【正则表达式】【文件/目录】
- -n:同时显示匹配行上下的n行
- -c:只显示匹配的行数,不显示匹配行的内容
- -h:当在多个文件中查找时,不显示文件名前缀
- -i:忽略大小写
- -v:显示不匹配行
- -r:在目录及其子目录中查找
- -w:将表达式作为一个单词查找
cd /home/tarena touch hello.txt echo 1234 >> hello.txt echo abc1234 >> hello.txt echo you've >> hello.txt echo printf >> hello.txt echo printf1234 >> hello.txt cat hello.txt grep -Rn "1234" hello.txt grep -Rn "printf" hello.txt grep -Rnw "printf" hello.txt grep -Rn "1234" * //在当前目录下的所有文件中搜索1234 grep -Rn "1234" *.txt //在当前目录下所有以.txt结尾的文件中搜索1234 grep -Rn "main" /user/include //到/user/include目录下搜索main grep -Rn "main" /user/include/*.h //到/user/include目录下,然后在所有以.h结尾的文件中搜索main
- 打包解包:tar
- 打包:
- tar -jcvf A.tar.bz2 A
- tar -zcvf A.tar.gz A
- 解包
- tar -xvf A.tar.bz2
- tar -xvf A.tar.gz
- 打包:
mkdir -p /home/tarena/tartest/hello cd /home/tarena/tartest/hello touch hello.txt echo 1234>> hello.txt cd .. ls tar -jcvf hello.tar.bz2 hello tar -zcvf hello.tar.gz hello ls rm -fr hello tar -xvf hello.tar.bz2 rm -fr hello tar -xvf hello.tar.gz cat hello/hello.txt
文件详细信息
- - rwx rw- r-- 1 root root 6 jul 25 11:48 myfile
- - --- --- --- - ---- ---- - ----------- -----
- A B. C. D. E. F. G. H I. J
- A:文件类型,d(目录)/-(普通)/1(软链接)
- B:属主权限,r(读)/w(写)/x(执行)/-(无)
- C:同组权限,r(读)/w(写)/x(执行)/-(无)
- D:其他权限,r(读)/w(写)/x(执行)/-(无)
- E:硬链接数,文件的别名
- F:属主的名称
- G:属组的名称
- H:字节数
- I:最后的修改时间
- J:文件名
- 针对目录的写权限:在该目录下增删字目录或文件
- 针对目录的执行权限:访问该目录下的内容
文件通配符
- *
- 通配若干任意字符:ls*.txt
- ?
- 通配 一个任意字符:ls file_?.txt
- []
- 通配一个在特定字符集中的字符:ls file_[a-c].txt
mdkir /home/tarena/file cd /home/tarena/file touch file_1.txt touch file_2.txt touch file_a.txt touch file_11.txt touch file_b.txt touch file_aa.txt ls *.txt ls file_?.txt ls file_[a-z].txt ls file_[0-9][0-9].txt
I/O重定向和管道
- >
- 输出重定向:echo hello > a.txt
- <
- 输入重定向:cat < a.txt > b.txt
- >>
- 追加:echo world >> b.txt
- |
- 管道符:将前一个命令的输出作为后一个命令的输入
- ls -l /etc | more
- ;
- 命令行分隔符,在一个命令行中分隔多个命令
- cal;pwd;date
- \
- 续行符,继续在下一行输入命令
cat \ /etc/passwd \ |\ grep \ tarena