您的位置:首页 > 运维架构 > Linux

Linux常用基础命令

2015-01-02 22:21 363 查看
Linux基础命令

-----------------目录部分-------------------

【pwd】显示当前所在的绝对目录

【cd】 切换目录

cd - 显示上一个工作目录

cd ~ 显示当前用户的家目录

cd . 显示当前目录

cd .. 显示当前目录的上一级目录

[root@centos ~]# cd /usr/
[root@centos usr]# ls
bin  etc  games  include  lib  libexec  local  my  sbin  share  src  tmp
[root@centos usr]# cd -
/root
[root@centos ~]# cd .
[root@centos ~]# cd ..
[root@centos /]# cd ~
[root@centos ~]# pwd
/root


【ls】 显示当前所在的目录文件列表

ls -a 显示所有文件(包含隐藏文件)

ls -l 与ll命令相同 以列表形式显示所有文件

ls -t 按时间排序显示文件

ls -m 横向输出文件名 并以','分割

ls -R 递归显示当前目录及其子目录

ls -S 以文件大小排序显示文件

root@centos ~]# ls -R
.:
anaconda-ks.cfg  install.log.syslog  lnmp-install.log
install.log      lnmp                vhost.sh
[root@centos ~]# ls -a
.                .bash_logout   .gconf              .lesshst          vhost.sh
..               .bash_profile  .gconfd             lnmp              .viminfo
anaconda-ks.cfg  .bashrc        install.log         lnmp-install.log
.bash_history    .cshrc         install.log.syslog  .tcshrc
[root@centos ~]# ls -t
lnmp-install.log  lnmp             install.log
vhost.sh          anaconda-ks.cfg  install.log.syslog
[root@centos ~]# ls -l
-rw-------. 1 root root    1383 11 7 05:25 anaconda-ks.cfg
-rw-r--r--. 1 root root   43838 11 7 05:25 install.log
-rw-r--r--. 1 root root   10033 11 7 05:23 install.log.syslog
-rwxr-xr-x. 1 root root    1930 11 7 08:00 lnmp
-rw-r--r--. 1 root root 1810073 11 7 08:01 lnmp-install.log
-rwxr-xr-x. 1 root root    5307 11 7 08:01 vhost.sh
[root@centos ~]# ls -m
anaconda-ks.cfg, install.log, install.log.syslog, lnmp, lnmp-install.log,
vhost.sh
[root@centos ~]# ls -Sl
1848
-rw-r--r--. 1 root root 1810073 11 7 08:01 lnmp-install.log
-rw-r--r--. 1 root root   43838 11 7 05:25 install.log
-rw-r--r--. 1 root root   10033 11 7 05:23 install.log.syslog
-rwxr-xr-x. 1 root root    5307 11 7 08:01 vhost.sh
-rwxr-xr-x. 1 root root    1930 11 7 08:00 lnmp
-rw-------. 1 root root    1383 11 7 05:25 anaconda-ks.cfg


【tree】以目录树 打印目录结构

【mkdir】创建目录

mkdir ./aaa 在当前目录下创建一个aaa目录(只能创建一层目录)

mkdir -p ./aaa/bbb/ccc 创建多个目录(可以创建多层目录)

[root@centos admin]# mkdir /aaa
[root@centos admin]# rm /aaa
rm: 无法删除"/aaa": 是一个目录
[root@centos admin]# rm -f /aaa
rm: 无法删除"/aaa": 是一个目录
[root@centos admin]# rm -rf /aaa
[root@centos admin]# mkdir ./aaa
[root@centos admin]# ls
aaa
[root@centos admin]# mkdir ./aaa/bbb/ccc
mkdir: 无法创建目录"./aaa/bbb/ccc": 没有那个文件或目录
[root@centos admin]# mkdir -p ./aaa/bbb/ccc
[root@centos admin]# ls
aaa
[root@centos admin]# tree
.
├── aaa
│   └── bbb
│       └── ccc


【rmdir】删除目录

rmdir 只能删除一个空目录

[root@centos admin]# rmdir ./aaa
rmdir: 删除 "./aaa" 失败: 目录非空

[root@centos admin]# rmdir ./aaa/bbb/ccc
[root@centos admin]# tree
.
├── aaa
│   └── bbb

rm也可删除目录(可以强制删除非空目录 请谨慎使用)
[root@centos admin]# rm /aaa
rm: 无法删除"/aaa": 是一个目录
[root@centos admin]# rm -f /aaa
rm: 无法删除"/aaa": 是一个目录
[root@centos admin]# rm -rf /aaa
[root@centos admin]#


------------------文件部分-------------------------

【touch】创建文件命令

touch 文件名

touch -d yyyymmdd 文件名 修改文件创建时间

[root@centos aaa]# ls
bbb
[root@centos aaa]# touch a.txt
[root@centos aaa]# ll
总用量 4
-rw-r--r-- 1 root root    0 12月 25 21:32 a.txt
drwxr-xr-x 2 root root 4096 12月 25 21:21 bbb
[root@centos aaa]# touch -d 20150102 a.txt
[root@centos aaa]# ll
总用量 4
-rw-r--r-- 1 root root    0 1月   2 2015 a.txt
drwxr-xr-x 2 root root 4096 12月 25 21:21 bbb


【cp】复制文件命令

cp 源文件 目标文件

cp -r 源文件 目标文件 //会复制整个源文件

[root@centos aaa]# cp -r ./ /var/c
[root@centos c]# cd /var/c
[root@centos c]# ll
总用量 8
-rw-r--r-- 1 root root    0 12月 25 21:42 a.txt
drwxr-xr-x 2 root root 4096 12月 25 21:42 bbb


当然我们还可以用 ./* 代表当前目录下的所有文件

再进行拷贝也可以

【mv】移动文件 或者 重命名文件

mv 源文件 目标文件

【cat】查看文件 从第一行开始往下查看

【tac】从最后一行开始往上查看

【file】 显示文件类型

file 文件名

[root@centos aaa]# file a.txt
a.txt: ASCII text


【more】一页一页查看文件内容

more 文件名

/字符串

?字符串

都可以用来搜索指定的字符串

b 或 ctrl + b 向上翻页

空格 或 回车 向下翻页

q退出

【less】同more

n 向下翻页

N 向上翻页

q 退出

【head】显示文件内容的头几行

head -n 数字 文件名

【tail】显示文件内容的尾几行

tail -n 数字 文件名

【man】手册使用查找命令

-------------------系统部分----------------------

【init】系统命令

参数:

init 0 关机

init 6 重启

【shutdown】关机/警告

shutdown -t 秒数 几秒后自动关机

shutdown -r 重启

shutdown -h 立即关机

-------------------命令起别名----------------------

【alias】这个需要修改配置文件

首先:切换到用户的家目录 cat ~

其次:编辑文件 .bash vi .bash_profile

再次:在文件中添加 你要修改的命令及其 别名

alias rm = '/bin/rm -f'

保存并退出:esc + !wq

source 一下:source .bash_profile

原理:系统在用户登录初始化时 会自动加载 .bash_profile文件

source 其实就是再次加载、执行一下该文件

他与 直接使用 ./XX.sh 执行的shell 脚本不同的是

他在全局范围内有效,shell只在关联目录内有效

-------------------查找基础命令---------------------

【find】

按时间查找

-atime 读或执行时更改文件的时间 access time 相当于lu -l

-mtime 写入文件时更改文件的时间 make time 相当于ls -l

-ctime 写入文件、更改权限、所有者、文件链接 相当于lc -l

或设置lnode时间戳而设置的更改时间 create time

一般使用的命令:

find ./ -mtime n 按时间查找 n天之内被修改的文件

-mtime +n n天之前 不包含第n天被修改的文件

-mtime -n n天之后 包含第n天

如 -mtime +3 今天是0103 那么就是查找 1231及之前的所有修改过的文件

-mtime -3 今天是0103 那么就是查找 0101-0103内被修改过的文件

find ./ -newer file1 新于文件file1的文件

find ./ -name filename 文件名包含filename的文件

[特殊作用命令]

find ./ -name filename -exec ls -l {} \;

[root@centos ~]# find ./ -name lnmp -exec  ls -l "{}" \;
-rwxr-xr-x. 1 root root 1930 11月  7 08:00 ./lnmp


比如我们要将查到的.conf文件都删除掉

find / -name "*.conf" -exec rm -rf {} \;

又比如我们要拷贝 查到的文件

find / -name "*.conf" -exec cp {} /home/my/ \;

重命名.txt 文件的后缀名改为.txtd

find ./ -name "*.txt" -exec mv "{}" "{}d" \;

解释:

-exec 参数后面跟的是command命令,它的终止是以;为结束标志的,

所以这句命令后面的分号是不可缺少的,

考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。

{} 花括号代表前面find查找出来的文件名。

【history】 查看历史执行过的命令

history 查看历史执行命令

history -c 清空历史执行命令

【echo】打印

echo $PATH 打印环境变量目录 以:分割

【grep】管道抓取命令

-c 只输出匹配行的计数

-i 不区分大小写(只适用于单字符)

-n 显示匹配的行及行号

-s 不显示不存在时的错误信息

-v 显示不包含匹配文本的所有行

-l 完整匹配行信息

[root@centos ~]# cat a.txt | grep h
5,6,h
7,8,9,h
10,h
[root@centos ~]# cat a.txt | grep -v h
1,n,2
3,4,n
[root@centos ~]# cat a.txt | grep -n h
3:5,6,h
4:7,8,9,h
5:10,h
[root@centos ~]#


下面这些命令 一般不独立使用 而是在管道后面使用

sort -r 反向排序

cut -d'分隔符' -f取第几段 多个字段用逗号分隔

uniq -c 计数 //一般在sort后面使用 否则统计可能无效

wc -l 统计出现结果行数

[root@centos ~]# ll | cut -d' ' -f5-13 | sort -s | uniq -c
1
1   10033 11月  7 05:23 install.log.syslog
1    1383 11月  7 05:25 anaconda-ks.cfg
1 1810073 11月  7 08:01 lnmp-install.log
1    1930 11月  7 08:00 lnmp
1   43838 11月  7 05:25 install.log
1    5307 11月  7 08:01 vhost.sh
[root@centos ~]# ll | cut -d' ' -f5-13 | sort -s | wc -l
7
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: