linux基本命令
重要原则:一切皆文件,配置文件保存为纯文本格式;
命令:应用程序的执行入口文件,一个应用程序有多个执行入口。
命令格式:主体 选项1 选项2 参数1 参数2 (多个参数之间空格隔开,参数中间包括空格时使用引号,可以省略选项和参数,选项是以-开头,短选项:-,长选项:--)对应bash用户接口应用程序。window对应cmd用户接口应用程序。
GUI图形界面:对应KDE应用程序。在mini版中安装后就可以使用图形化界面。
基本命令:
1、ls(ls -a -l /etc/ 或 ls -al /etc);
-l:长格式显示(详细信息)
文件类型 权限 用户名 组名 文件大小(字节单位)最后一次访问时间 文件名
文件列表:d/-/l/b/c 目录/普通/链接/设备;
权限:每3位一组rwxr-xr-x -表示无权限
-a 显示所有文件,包含隐藏文件。
2、cd切换工作目录
如何学习命令:
[zyj@localhost ~]$ type cd cd is a shell builtin [zyj@localhost ~]$ type ls ls is aliased to `ls --color=auto
1、内置命令:bash自带命令。help查看命令如何使用。
[zyj@localhost ~]$ help cd cd: cd [-L|-P] [dir] 语法格式 []可选项 不加[]为必选项 Change the shell working directory. 用途 Change the current directory to DIR. The default DIR is the value of the HOME shell variable. The variable CDPATH defines the search path for the directory containing DIR. Alternative directory names in CDPATH are separated by a colon (:). A null directory name is the same as the current directory. If DIR begins with a slash (/), then CDPATH is not used. If the directory is not found, and the shell option `cdable_vars' is set, the word is assumed to be a variable name. If that variable has a value, its value is used for DIR. Options: -L force symbolic links to be followed -P use the physical directory structure without following symbolic links The default is to follow symbolic links, as if `-L' were specified. Exit Status: Returns 0 if the directory is changed; non-zero otherwise. [zyj@localhost ~]$
2、外部命令:内置命令之外的命令。通过man命令查看如何使用,按q退出,简化版中没有man命令,可以安装。
切换版面按空格,前一个版面按B,下箭头或J往下翻一行,往上翻一行用K或者上翻箭,/关键字 查找
[zyj@localhost ~]$man cp NAME cp - copy files and directories SYNOPSIS cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY 必选项 cp [OPTION]... -t DIRECTORY SOURCE...
- -help 与man类似,显示一部分帮助文档,当系统为中文时,显示中文帮助文档。
[root@localhost ~]# cp --help Usage: cp [OPTION]... [-T] SOURCE DEST or: cp [OPTION]... SOURCE... DIRECTORY or: cp [OPTION]... -t DIRECTORY SOURCE... Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY. Mandatory arguments to long options are mandatory for short options too. -a, --archive same as -dR --preserve=all --backup[=CONTROL] make a backup of each existing destination file -b like --backup but does not accept an argument --copy-contents copy contents of special files when recursive -d same as --no-dereference --preserve=links -f, --force if an existing destination file cannot be opened, remove it and try again (redundant if the -n option is used) -i, --interactive prompt before overwrite (overrides a previous -n option) -H follow command-line symbolic links in SOURCE -l, --link link files instead of copying -L, --dereference always follow symbolic links in SOURCE -n, --no-clobber do not overwrite an existing file (overrides a previous -i option) -P, --no-dereference never follow symbolic links in SOURCE -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
硬件时间:主干电源上的时间;hwclock
系统时间:操作系统CPU管理的时间片段
时间同步:hwclock -s
man hwclock -s, --hctosys Set the System Time from the Hardware Clock.
别名:改变后的命令,要使用原生命令,在命令前面加\
[zyj@localhost ~]$ type ls ls is aliased to `ls --color=auto' [zyj@localhost ~]$ ls Desktop Documents Downloads Music Pictures Public Templates Videos [zyj@localhost ~]$ \ls Desktop Documents Downloads Music Pictures Public Templates Videos
文件系统
rootfs:根文件系统/
/boot:系统启动相关的文件,如内核,initrd以及grub(bootloader)
/dev:设备文件,包括块设备、字符设备
/etc: 配置文件。
/root: 管理员的家目录
/lib: 库文件,静态库.a 动态库.dll .so
/midia: 挂载点目录,移动设备。
[root@localhost dev]# mount /dev/cdrom1 /media/
/opt:可选目录,第三方程序的安装目录。
/usr:安装第三方软件包
/lost+found:非正常情况下,将最后的数据写进去,防止数据丢失。
/tmp:临时文件目录,/var/tmp
/sbin:管理命令
/bin:可执行文件、用户命令
[root@localhost ~]# whereis rm
rm: /bin/rm /usr/share/man/man1/rm.1.gz /usr/share/man/man1p/rm.1p.gz
/home:用户家目录,在家目录下拥有最高的权限,存在自己的私有文件。
[zyj@localhost ~]$ pwd /home/zyj
/proc:伪文件系统,内核映射文件。
/sys:伪文件系统,硬件设备相关的属性映射文件。
路径:从某一个起点到达一个终点,中间所经过的节点列表。
绝对路径:以根目录为起点。/etc/sysconfig/network-scripts/ifcfg-eth0 windows下使用绝对路径,无法执行。
相对路径:以当前目录为起点。../etc/sysconfig/network-scripts/ifcfg-eth0 程序一致性推荐使用相对路径,使用的最多。
.代表当前目录, ..代表上层目录
操作文件
mkdir:创建目录。
[root@localhost home]# ls zyj [root@localhost home]# mkdir x [root@localhost home]# ls x zyj [root@localhost home]# mkdir x/y/z mkdir: cannot create directory `x/y/z': No such file or directory [root@localhost home]# mkdir -p x/y/z [root@localhost home]# ls x zyj [root@localhost home]# cd x [root@localhost x]# ls y [root@localhost home]# cd x/y/ [root@localhost y]# ls z [root@localhost y]# mkdir a b c [root@localhost y]# ls a b c z [root@localhost ~]# cd /home [root@localhost home]# ls x zyj [root@localhost home]# mkdir x/{w,s} [root@localhost home]# ls x zyj [root@localhost home]# cd x [root@localhost x]# ls s w y
touch:创建一个文件
[root@localhost x]# ls -l total 16 -rw-r--r--. 1 root root 0 Apr 15 04:32 1.txt drwxr-xr-x. 2 root root 4096 Apr 15 04:27 s drwxr-xr-x. 2 root root 4096 Apr 15 04:27 w drwxr-xr-x. 2 root root 4096 Apr 15 04:26 w,s drwxr-xr-x. 6 root root 4096 Apr 15 04:26 y [root@localhost x]# touch 1.txt [root@localhost x]# ls -l total 16 -rw-r--r--. 1 root root 0 Apr 15 04:44 1.txt drwxr-xr-x. 2 root root 4096 Apr 15 04:27 s drwxr-xr-x. 2 root root 4096 Apr 15 04:27 w drwxr-xr-x. 2 root root 4096 Apr 15 04:26 w,s drwxr-xr-x. 6 root root 4096 Apr 15 04:26 y
文件三个时间:
1、最后一次访问创建:
2、最后一次修改时间:内容的改动叫修改。
3、最后一次改变时间:文件(内容数据+元数据)的改动为改变。
文件两种数据:
1、元数据:除了内容数据之外的。
2、内容数据:文件内容本身
[root@localhost x]# stat 1.txt //stat查看文件信息 File: `1.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 145553 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2018-04-15 04:44:24.158407586 -0700 Modify: 2018-04-15 04:44:24.158407586 -0700 Change: 2018-04-15 04:44:24.158407586 -0700 [root@localhost x]# ls -l total 16 -rw-r--r--. 1 root root 0 Apr 15 04:44 1.txt drwxr-xr-x. 2 root root 4096 Apr 15 04:27 s drwxr-xr-x. 2 root root 4096 Apr 15 04:27 w drwxr-xr-x. 2 root root 4096 Apr 15 04:26 w,s drwxr-xr-x. 6 root root 4096 Apr 15 04:26 y [root@localhost x]# chown zyj:zyj 1.txt //改变文件的元数据 [root@localhost x]# ls -l total 16 -rw-r--r--. 1 zyj zyj 0 Apr 15 04:44 1.txt [root@localhost x]# stat 1.txt File: `1.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 145553 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 500/ zyj) Gid: ( 500/ zyj) Access: 2018-04-15 04:44:24.158407586 -0700 Modify: 2018-04-15 04:44:24.158407586 -0700 Change: 2018-04-15 04:52:53.878407275 -0700 [root@localhost x]# vi 1.txt //改变文件内容数据 [root@localhost x]# stat 1.txt File: `1.txt' Size: 5 Blocks: 8 IO Block: 4096 regular file Device: 802h/2050d Inode: 145555 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 500/ zyj) Gid: ( 500/ zyj) Access: 2018-04-15 04:54:26.026406598 -0700 Modify: 2018-04-15 04:54:26.027406623 -0700 Change: 2018-04-15 04:54:26.034406787 -0700 [root@localhost x]# ls -l total 20 -rw-r--r--. 1 zyj zyj 5 Apr 15 04:54 1.txt
rm删除文件,使用时注意慎用-f 强制删除不提示。rmdir删除目录,rm也可以删除目录。
[root@localhost /]# rm -rf / 删除根目录下所有的文件,类似与格式化
[root@localhost home]# ls x zyj [root@localhost home]# rm -rf x [root@localhost home]# ls zyj
cp copy文件或目录到一个目录下。只能在一个linux中复制。NFS网络文件共享可以实现网络copy
[root@localhost ~]# cp --help Usage: cp [OPTION]... [-T] SOURCE DEST or: cp [OPTION]... SOURCE... DIRECTORY or: cp [OPTION]... -t DIRECTORY SOURCE... Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY. [root@localhost ~]# cd /home/ [root@localhost home]# mkdir x [root@localhost home]# ls x zyj [root@localhost home]# cd x [root@localhost x]# cp /root/install.log . //复制到当前目录,使用cp的第二种语法 [root@localhost x]# ls install.log [root@localhost x]# cp /root/install.log ./my.log //第一种语法格式 [root@localhost x]# ls install.log my.log [root@localhost x]# cp /root/* . //复制root下的所有文件到当前目录下 [root@localhost x]# ls anaconda-ks.cfg install.log install.log.syslog my.log [root@localhost home]# cp -r x /opt //复制目录 -r递归 [root@localhost home]# ls /opt rh x
scp:socket cp 网络copy
[zyj@localhost home]$ scp --help usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] [[user@]host1:]file1 ... [[user@]host2:]file2 [zyj@localhost home]$ scp -r x/ root@192.168.57.129:/home //-r递归 将当前linux中的目录copy到另一台linux上的/home目录下。 The authenticity of host '192.168.57.129 (192.168.57.129)' can't be established. RSA key fingerprint is 49:92:35:6c:99:27:6c:ec:a7:65:be:a2:72:cf:a7:ff. Are you sure you want to continue connecting (yes/no)? y Please type 'yes' or 'no': yes Warning: Permanently added '192.168.57.129' (RSA) to the list of known hosts. root@192.168.57.129's password: my.log 100% 41KB 40.9KB/s 00:00 install.log.syslog 100% 9154 8.9KB/s 00:00 x/anaconda-ks.cfg: Permission denied install.log 100% 41KB 40.9KB/s 00:00 [root@localhost home]# scp root@192.168.57.129:/home/x/h.txt ./x //将远端主机的h.txt文件copy到本端设备的目录下。 The authenticity of host '192.168.57.129 (192.168.57.129)' can't be established. RSA key fingerprint is 49:92:35:6c:99:27:6c:ec:a7:65:be:a2:72:cf:a7:ff. Are you sure you want to continue connecting (yes/no)? y Please type 'yes' or 'no': yes Warning: Permanently added '192.168.57.129' (RSA) to the list of known hosts. root@192.168.57.129's password: h.txt 100% 12 0.0KB/s 00:00 [root@localhost home]# cd x [root@localhost x]# ls anaconda-ks.cfg h.txt install.log install.log.syslog my.log
mv: 重命名,移动文件
[root@localhost x]# ls anaconda-ks.cfg h.txt install.log install.log.syslog my.log [root@localhost x]# mv h.txt hello.txt //重命名 [root@localhost x]# ls anaconda-ks.cfg hello.txt install.log install.log.syslog my.log [root@localhost x]# ls anaconda-ks.cfg hello.txt install.log install.log.syslog my.log [root@localhost x]# mv hello.txt /home //移动 [root@localhost x]# cd .. [root@localhost home]# ls hello.txt x zyj [root@localhost home]# cd x [root@localhost x]# ls anaconda-ks.cfg install.log install.log.syslog my.log
修改时区:
[root@localhost ~]# cd /etc/sysconfig/ [root@localhost sysconfig]# vi clock [root@localhost sysconfig]# cd [root@localhost ~]# tzselect //时钟选择 Please identify a location so that time zone rules can be set correctly. Please select a continent or ocean. 1) Africa 2) Americas 3) Antarctica 4) Arctic Ocean 5) Asia 6) Atlantic Ocean 7) Australia 8) Europe 9) Indian Ocean 10) Pacific Ocean 11) none - I want to specify the time zone using the Posix TZ format. #? 5 Please select a country. 1) Afghanistan 18) Israel 35) Palestine 2) Armenia 19) Japan 36) Philippines 3) Azerbaijan 20) Jordan 37) Qatar 4) Bahrain 21) Kazakhstan 38) Russia 5) Bangladesh 22) Korea (North) 39) Saudi Arabia 6) Bhutan 23) Korea (South) 40) Singapore 7) Brunei 24) Kuwait 41) Sri Lanka 8) Cambodia 25) Kyrgyzstan 42) Syria 9) China 26) Laos 43) Taiwan 10) Cyprus 27) Lebanon 44) Tajikistan 11) East Timor 28) Macau 45) Thailand 12) Georgia 29) Malaysia 46) Turkmenistan 13) Hong Kong 30) Mongolia 47) United Arab Emirates 14) India 31) Myanmar (Burma) 48) Uzbekistan 15) Indonesia 32) Nepal 49) Vietnam 16) Iran 33) Oman 50) Yemen 17) Iraq 34) Pakistan #? 9 Please select one of the following time zone regions. 1) Beijing Time 2) Xinjiang Time #? 1 The following information has been given: China Beijing Time Therefore TZ='Asia/Shanghai' will be used. Local time is now: Sun Apr 15 22:43:32 CST 2018. Universal Time is now: Sun Apr 15 14:43:32 UTC 2018. Is the above information OK? 1) Yes 2) No #? 1 You can make this change permanent for yourself by appending the line TZ='Asia/Shanghai'; export TZ --------------修改时钟。 to the file '.profile' in your home directory; then log out and log in again. Here is that TZ value again, this time on standard output so that you can use the /usr/bin/tzselect command in shell scripts: Asia/Shanghai [root@localhost ~]# date Sun Apr 15 07:44:14 PDT 2018 [zyj@localhost ~]$ TZ='Asia/Shanghai'; export TZ [zyj@localhost ~]$ date Sun Apr 15 22:47:06 CST 2018 [zyj@localhost ~]$
小技巧:
1、两次tab键补全命令;
2、 编辑文件时,异常退出时,会生成一个.swp的隐藏文件,当再次编辑此文件时,会提示此文件为可读,需要删除隐藏的文件后会恢复正常。
3、clear命令用来清屏;
4、netstat查看网络连接;
6、printenv查看环境变量,如语言环境等。