Linux基本命令

  • 切换用户
[root@localhost ~]# su hadoop
[hadoop@localhost root]$ su root
Password:
[root@localhost ~]#

  • 显示当前目录:pwd
  • 进入上次目录:cd -
  • 按列表形式查看目录:ls -l 等价于 ll
  • 查看别名:alias
  • 查看所有文件:ls -la/ls -al /ls -l -a(-l :以列表形式显示 -h:人性化 -a:显示.开头的文件)
  • 创建文件夹:mkdir
  • 创建文件夹:touch
  • 查看文件:cat echo:追加内容 一个>是覆盖,两个>>是追加
[hadoop@localhost a]$ echo helloworld >> test.txt
[hadoop@localhost a]$ ls
test.txt
[hadoop@localhost a]$ ls -l
total 4
-rw-rw-r--. 1 hadoop hadoop 22 Dec 15 01:53 test.txt
[hadoop@localhost a]$ cat test.txt
helloworld
helloworld
[hadoop@localhost a]$ echo nihao > test.txt
[hadoop@localhost a]$ cat test.txt
nihao
[hadoop@localhost a]$
  • 复制文件:
[hadoop@localhost a]$ ls
test.txt
[hadoop@localhost a]$ cp test.txt test.txt1
[hadoop@localhost a]$ ls
test.txt  test.txt1
[hadoop@localhost a]$ cat test.txt1
nihao
[hadoop@localhost a]$
[hadoop@localhost a]$ ls
1.txt  2.txt
[hadoop@localhost a]$ mkdir temp
[hadoop@localhost a]$ ls
1.txt  2.txt  temp
[hadoop@localhost a]$ cp *.txt temp/
[hadoop@localhost a]$ ls
1.txt  2.txt  temp
[hadoop@localhost a]$ cd temp
[hadoop@localhost temp]$ ls
1.txt  2.txt
[hadoop@localhost temp]$

  • 删除文件
[hadoop@localhost a]$ rm test.txt
[hadoop@localhost a]$ ls
test.txt1
[hadoop@loc
  • 删除目录(强行递归删除)
[hadoop@localhost a]$ rm -rf temp
[hadoop@localhost a]$ ls
1.txt  2.txt
[hadoop@localhost a]$

  • 移动
[hadoop@localhost a]$ ls
[hadoop@localhost a]$ echo hello >1.txt
[hadoop@localhost a]$ ls
1.txt
[hadoop@localhost a]$ mkdir temp
[hadoop@localhost a]$ ls
1.txt  temp
[hadoop@localhost a]$ mv 1.txt temp
[hadoop@localhost a]$ ls
temp
[hadoop@localhost a]$ cd temp
[hadoop@localhost temp]$ ls
1.txt
[hadoop@localhost temp]$


  • 文本模式切换:ctrl+alt+f6
  • 激活鼠标:ctrl+alt
  • 切换到桌面模式:ctrl+alt+f1
  • 切换到新的文本模式:ctrl+alt+f5
  • nano文本编辑器   CTRL + o 保存文件  CTRL+x  退出 CTRL+k 剪切 CTRL+ u撤销(复制一行)
  • more:查看文件分屏显示
  • tail:查看文件后10行
  • find:查找所有文件
[root@localhost ~]# find . > c.txt(重定向新建文件)
[root@localhost ~]# more c.txt



[root@localhost ~]# find . | more效果和上面一样,通过管道符,将前面的输出作为后面的输入,可以不停的叠加
  • man:查看详细的帮助信息
  • more指定显示的行数:
    [root@localhost ~]# find . | more -3
    

  • head
[root@localhost ~]# cat test.txt
1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# head test.txt
1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# head -5 test.txt//查看前5行
1
2
3
4
5
[root@localhost ~]# head --lines=3 test.txt//查看前三行
1
2
3
[root@localhost ~]# head --lines=-2 test.txt//除去后两行
1
2
3
4
5
6
7
8
9
10
[root@localhost ~]#


[root@localhost ~]# head -n 2 test.txt//查看前两行
1
2
[root@localhost ~]#
[root@localhost ~]# cat test.txt
1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# tail test.txt//查看后10行
3
4
5
6
7
8
9
10
11
12
[root@localhost ~]# tail -3 test.txt//查看后3行
10
11
12
[root@localhost ~]# tail --lines=5 test.txt//查看后5行
8
9
10
11
12
[root@localhost ~]# head --lines=-4 test.txt//去掉后4行
1
2
3
4
5
6
7
8
  • 单词统计
[root@localhost ~]# echo hello word > a.txt
[root@localhost ~]# cat a.txt
hello word
[root@localhost ~]# wc -c a.txt //字符
11 a.txt
[root@localhost ~]# wc -l a.txt //行数
1 a.txt
[root@localhost ~]# wc -w a.txt//单词数
2 a.txt
[root@localhost ~]# wc -c -l -w a.txt//字符 行数 单词数
 1  2 11 a.txt
[root@localhost ~]# wc -clw a.txt
 1  2 11 a.txt
[root@localhost ~]# wa a.txt
bash: wa: command not found...
[root@localhost ~]# wc a.txt
 1  2 11 a.txt
[root@localhost ~]#

  • 查看主机名
[root@localhost ~]# hostname
localhost.localdomain

  • 查看系统名称,内核,处理器,操作i系统
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# uname -s
Linux
[root@localhost ~]# uname -n
localhost.localdomain
[root@localhost ~]# uname -r
3.10.0-327.el7.x86_64
[root@localhost ~]# uname -v
#1 SMP Thu Nov 19 22:10:57 UTC 2015
[root@localhost ~]# uname -m
x86_64
[root@localhost ~]# uname -p
x86_64
[root@localhost ~]# uname -i
x86_64
[root@localhost ~]# uname -o
GNU/Linux
[root@localhost ~]#

  • 查看文件类型  file  XXXX
  • 文件原地压缩
[root@localhost aa]# ll
total 4
-rw-r--r--. 1 root root  6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root 21 Dec 16 18:54 bb
[root@localhost aa]# gzip a.txt//压缩文件
[root@localhost aa]# ll
total 4
-rw-r--r--. 1 root root 32 Dec 16 18:52 a.txt.gz
drwxr-xr-x. 2 root root 21 Dec 16 18:54 bb
[root@localhost aa]# gzip -d a.txt.gz//解压文件
[root@localhost aa]# ll
total 4
-rw-r--r--. 1 root root  6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root 21 Dec 16 18:54 bb
[root@localhost aa]# gzip -r bb//递归压缩文件夹
[root@localhost aa]# ll
total 4
-rw-r--r--. 1 root root  6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root 21 Dec 16 18:54 bb
[root@localhost aa]# gzip -dr bb//递归解压文件夹
[root@localhost aa]# ll
total 4
-rw-r--r--. 1 root root  6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root 18 Dec 16 18:56 bb
[root@localhost aa]#

  • 原地解压文件
[root@localhost aa]# gzip a.txt
[root@localhost aa]# ll
total 4
-rw-r--r--. 1 root root 32 Dec 16 18:52 a.txt.gz
drwxr-xr-x. 2 root root 18 Dec 16 18:56 bb
[root@localhost aa]# gunzip a.txt.gz
[root@localhost aa]# ll
total 4
-rw-r--r--. 1 root root  6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root 18 Dec 16 18:56 bb
[root@localhost aa]# gzip -r bb
[root@localhost aa]# ll
total 4
-rw-r--r--. 1 root root  6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root 21 Dec 16 19:00 bb
[root@localhost aa]# gunzip -r bb
[root@localhost aa]# ll
total 4
-rw-r--r--. 1 root root  6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root 18 Dec 16 19:00 bb
[root@localhost aa]#

  • 文件/文件夹归档
[root@localhost aa]# ll
total 16
-rw-r--r--. 1 root root     6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root    18 Dec 16 19:00 bb
-rw-r--r--. 1 root root 10240 Dec 16 19:16 my.tar
[root@localhost aa]# tar -cf my.tar a.txt//将文件a.txt归档在my.tar中
[root@localhost aa]# ll
total 16
-rw-r--r--. 1 root root     6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root    18 Dec 16 19:00 bb
-rw-r--r--. 1 root root 10240 Dec 16 19:18 my.tar
[root@localhost aa]# tar -xvf my.tar//抽取文件a.txt
a.txt
[root@localhost aa]# rm a.txt
rm: remove regular file 鈥榓.txt鈥? y
[root@localhost aa]# ll
total 12
drwxr-xr-x. 2 root root    18 Dec 16 19:00 bb
-rw-r--r--. 1 root root 10240 Dec 16 19:18 my.tar
[root@localhost aa]# tar -xvf my.tar
a.txt
[root@localhost aa]# ll
total 16
-rw-r--r--. 1 root root     6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root    18 Dec 16 19:00 bb
-rw-r--r--. 1 root root 10240 Dec 16 19:18 my.tar
[root@localhost aa]#

  • 文件夹归档
[root@localhost aa]# tar -cf my.tar bb//归档文件夹bb
[root@localhost aa]# ll
total 16
-rw-r--r--. 1 root root     6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root    18 Dec 16 19:00 bb
-rw-r--r--. 1 root root 10240 Dec 16 19:22 my.tar
[root@localhost aa]# rm -f bb
rm: cannot remove 鈥榖b鈥? Is a directory
[root@localhost aa]# rm -r bb
rm: descend into directory 鈥榖b鈥? y
rm: remove regular file 鈥榖b/a.txt鈥? y
rm: remove directory 鈥榖b鈥? y
[root@localhost aa]# ll
total 16
-rw-r--r--. 1 root root     6 Dec 16 18:52 a.txt
-rw-r--r--. 1 root root 10240 Dec 16 19:22 my.tar
[root@localhost aa]# tar -xvf my.tar//抽取文件夹bb
bb/
bb/a.txt
[root@localhost aa]# ll
total 16
-rw-r--r--. 1 root root     6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root    18 Dec 16 19:00 bb
-rw-r--r--. 1 root root 10240 Dec 16 19:22 my.tar
[root@localhost aa]
  • xargs:多行变单行 按照空格符代替回车符号
[root@localhost bb]# ll
total 0
[root@localhost bb]# cd ..
[root@localhost aa]# ll
total 16
-rw-r--r--. 1 root root     0 Dec 16 19:44 1.jgp
-rw-r--r--. 1 root root     0 Dec 16 19:44 2.jsp
-rw-r--r--. 1 root root     0 Dec 16 19:44 3.txt
-rw-r--r--. 1 root root     6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root     6 Dec 16 19:46 bb
-rw-r--r--. 1 root root 10240 Dec 16 19:22 my.tar
[root@localhost aa]# find . |grep txt |cp `xargs` bb//查看aa文件夹下的所有文件 ,过滤出txt文件结尾的,拷贝到文件夹bb  ``反引号是强制命令解析
[root@localhost aa]# cd bb
[root@localhost bb]# ll
total 4
-rw-r--r--. 1 root root 0 Dec 16 19:47 3.txt
-rw-r--r--. 1 root root 6 Dec 16 19:47 a.txt
[root@localhost bb]#

  • 命令的嵌套
[root@localhost bb]# echo 192.168.109.1 > s.txt
[root@localhost bb]# ll
total 8
-rw-r--r--. 1 root root  0 Dec 16 19:47 3.txt
-rw-r--r--. 1 root root  6 Dec 16 19:47 a.txt
-rw-r--r--. 1 root root 14 Dec 16 19:59 s.txt
[root@localhost bb]# ping `cat s.txt`
PING 192.168.109.1 (192.168.109.1) 56(84) bytes of data.
64 bytes from 192.168.109.1: icmp_seq=1 ttl=128 time=0.557 ms
64 bytes from 192.168.109.1: icmp_seq=2 ttl=128 time=0.334 ms
64 bytes from 192.168.109.1: icmp_seq=3 ttl=128 time=11.0 ms
64 bytes from 192.168.109.1: icmp_seq=4 ttl=128 time=0.625 ms
^C
--- 192.168.109.1 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3125ms
rtt min/avg/max/mdev = 0.334/3.134/11.022/4.555 ms
[root@localhost bb]#

  • 查看命令所在路径
[root@localhost home]# which cat
/usr/bin/cat
[root@localhost home]# which nano
/usr/bin/nano

  • 显示环境变量,区分大小写
[root@localhost home]# echo $PATH
/root/.bashrc/anaconda3/bin:/usr/local/maven352/bin:/root/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/spark2.2/bin:/usr/local/spark2.2/sbin:/usr/local/jdk1.8/bin:/root/bin
[root@localhost home]#

  • bin/sbin:binary /super binary 二进制可执行文件 sbin用于存放重要性更高级的文件
  • 文件权限
chmod权限的修改只能owner或者root才能够修改  
r:文件:读取内容 //(read)
 文件夹:查看文件内容  
w:文件:编辑文件//write
 文件夹:增删文件
x:文件:运行程序//excute
 文件夹:进入该目录

修改linux文件权限命令:chmod

u:user 拥有者
g:group  组
o:other 其他用户
ugo=a=all  所有用户和组

r:4
w:2
x:1

chmod [who] [+ | - | =] [mode] 文件名¼

  命令中各选项的含义为:

  操作对象who可是下述字母中的任一个或者它们的组合:

  u 表示“用户(user)”,即文件或目录的所有者。

  g 表示“同组(group)用户”,即与文件属主有相同组ID的所有用户。

  o 表示“其他(others)用户”。

  a 表示“所有(all)用户”。它是系统默认值。

  操作符号可以是:

  + 添加某个权限。

  - 取消某个权限。

  = 赋予给定权限并取消其他所有权限(如果有的话)。

  设置mode所表示的权限可用下述字母的任意组合:

  r 可读。

  w 可写。

  x 可执行。

[root@localhost ~]# mkdir aaa
[root@localhost ~]# ls
aaa        anaconda-ks.cfg  Documents  Music     Public     test.txt
anaconda3  Desktop          Downloads  Pictures  Templates  Videos
[root@localhost ~]# ll
total 12
drwxr-xr-x.  2 root root    6 Dec 15 05:17 aaa
drwxr-xr-x. 22 root root 4096 Nov 29 01:48 anaconda3
-rw-------.  1 root root 2741 Nov 23 20:24 anaconda-ks.cfg
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Desktop
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Documents
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Downloads
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Music
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Pictures
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Public
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Templates
-rw-r--r--.  1 root root   21 Dec 12 05:46 test.txt
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Videos
[root@localhost ~]# chmod u-w aaa//user取出write权限
[root@localhost ~]# ll
total 12
dr-xr-xr-x.  2 root root    6 Dec 15 05:17 aaa
drwxr-xr-x. 22 root root 4096 Nov 29 01:48 anaconda3
-rw-------.  1 root root 2741 Nov 23 20:24 anaconda-ks.cfg
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Desktop
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Documents
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Downloads
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Music
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Pictures
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Public
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Templates
-rw-r--r--.  1 root root   21 Dec 12 05:46 test.txt
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Videos
[root@localhost ~]#
[root@localhost aa]# chmod ugo+rwx a.txt//给user/group/other添加read/write/execute权限
[root@localhost aa]# ll
total 16
-rw-r--r--. 1 root root     0 Dec 16 19:44 1.jgp
-rw-r--r--. 1 root root     0 Dec 16 19:44 2.jsp
-r--r--r--. 1 root root     0 Dec 16 19:44 3.txt
-rwxrwxrwx. 1 root root     6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root    42 Dec 16 19:59 bb
-rw-r--r--. 1 root root 10240 Dec 16 19:22 my.tar
[root@localhost aa]#
[root@localhost aa]# chmod 621 a.txt//通过数字修改权限
[root@localhost aa]# ll
total 16
-rw-r--r--. 1 root root     0 Dec 16 19:44 1.jgp
-rw-r--r--. 1 root root     0 Dec 16 19:44 2.jsp
-r--r--r--. 1 root root     0 Dec 16 19:44 3.txt
-rw--w---x. 1 root root     6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root    42 Dec 16 19:59 bb
-rw-r--r--. 1 root root 10240 Dec 16 19:22 my.tar
[root@localhost aa]#


[root@localhost aa]# chmod g=r a.txt //直接赋值
[root@localhost aa]# ll
total 16
-rw-r--r--. 1 root root     0 Dec 16 19:44 1.jgp
-rw-r--r--. 1 root root     0 Dec 16 19:44 2.jsp
-r--r--r--. 1 root root     0 Dec 16 19:44 3.txt
-rw-r----x. 1 root root     6 Dec 16 18:52 a.txt
drwxr-xr-x. 2 root root    42 Dec 16 19:59 bb
-rw-r--r--. 1 root root 10240 Dec 16 19:22 my.tar
[root@localhost aa]#

  • 修改文件的拥有者和组 chown  (change own)
[root@localhost ~]# cd /home
[root@localhost home]# ll
total 12
drwx------. 15 cr121  cr121  4096 Dec 15 04:31 cr121
drwx------. 17 hadoop hadoop 4096 Dec 15 01:52 hadoop
-rw-r--r--.  1 root   root     39 Dec 12 07:01 test.txt
[root@localhost home]# chown hadoop:hadoop test.txt
[root@localhost home]# ll
total 12
drwx------. 15 cr121  cr121  4096 Dec 15 04:31 cr121
drwx------. 17 hadoop hadoop 4096 Dec 15 01:52 hadoop
-rw-r--r--.  1 hadoop hadoop   39 Dec 12 07:01 test.txt
[root@localhost home]#

  • 递归修改用户组和用户  chown -R root:root xxx
[root@localhost ~]# chown -R hadoop:hadoop aaa
[root@localhost ~]# ll
total 12
dr-xr-xr-x.  3 hadoop hadoop   16 Dec 15 05:26 aaa
drwxr-xr-x. 22 root   root   4096 Nov 29 01:48 anaconda3
-rw-------.  1 root   root   2741 Nov 23 20:24 anaconda-ks.cfg
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Desktop
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Documents
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Downloads
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Music
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Pictures
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Public
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Templates
-rw-r--r--.  1 root   root     21 Dec 12 05:46 test.txt
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Videos
[root@localhost ~]# cd aaa
[root@localhost aaa]# ll
total 0
drwxr-xr-x. 3 hadoop hadoop 16 Dec 15 05:27 bbb
[root@localhost aaa]# cd bbb
[root@localhost bbb]# ll
total 0
drwxr-xr-x. 2 hadoop hadoop 6 Dec 15 05:27 ccc
[root@localhost bbb]#

  • 递归修改权限   chmod  -R 777 xxx (修改为读写操作权限)
[root@localhost ~]# chmod -R 777 aaa
[root@localhost ~]# ll
total 12
drwxrwxrwx.  3 hadoop hadoop   16 Dec 15 05:26 aaa
drwxr-xr-x. 22 root   root   4096 Nov 29 01:48 anaconda3
-rw-------.  1 root   root   2741 Nov 23 20:24 anaconda-ks.cfg
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Desktop
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Documents
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Downloads
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Music
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Pictures
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Public
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Templates
-rw-r--r--.  1 root   root     21 Dec 12 05:46 test.txt
drwxr-xr-x.  2 root   root      6 Nov 29 00:47 Videos
[root@localhost ~]# cd aaa
[root@localhost aaa]# ll
total 0
drwxrwxrwx. 3 hadoop hadoop 16 Dec 15 05:27 bbb
[root@localhost aaa]# cd bbb
[root@localhost bbb]# ll
total 0
drwxrwxrwx. 2 hadoop hadoop 6 Dec 15 05:27 ccc
[root@localhost bbb]#

  • 创建连接文件
-    文件
d   目录
l    link链接
b   block块设备
c   charactor  字符文件

硬链接:两个文件实时备份,修改的时候处于同步状态,删除的时候不同步
硬链接两个文件占用相同的磁盘空间,一般采用符号链接‘
ln  xxx  xxx_link
硬链接不能为为文件夹创建链接,只能为文件创建链接
[root@localhost aa]# ll
total 4
-rw-r--r--. 1 root root 6 Dec 15 05:45 test.txt
[root@localhost aa]# ln test.txt test1.txt
[root@localhost aa]# ll
total 8
-rw-r--r--. 2 root root 6 Dec 15 05:45 test1.txt
-rw-r--r--. 2 root root 6 Dec 15 05:45 test.txt
[root@localhost aa]# echo xxx>>test.txt
[root@localhost aa]# ll
total 8
-rw-r--r--. 2 root root 10 Dec 15 05:48 test1.txt
-rw-r--r--. 2 root root 10 Dec 15 05:48 test.txt
[root@localhost aa]# echo yyy>>test1.txt
[root@localhost aa]# ll
total 8
-rw-r--r--. 2 root root 14 Dec 15 05:48 test1.txt
-rw-r--r--. 2 root root 14 Dec 15 05:48 test.txt
[root@localhost aa]#
































posted @ 2017-12-17 15:39  crr121  阅读(175)  评论(0编辑  收藏  举报