当前位置:首页 >> 脚本专栏

Linux 日常常用指令及应用小结

最近搞了一个阿里ECS,CentOS7,涉及到一些基本的Linux指令,在这里总结一下,在搭环境中常用的一些指令,熟悉这些指令就基本能够使用CentOS进行日常操作了。

   更多的可以参考系统自带的  “Cammand --help” ,很实用。

  目录相关指令

 /*cd指令 
 *跳至到XX目录下,从Xshell远程登陆进去的目录是/root
 *cd .. 返回上一层目录
 */
[root@Lettiy ~]# cd /usr/local
/*ls
 显示当前目录下的所有文件
*/
[root@Lettiy local]# ls
aegis bin etc games include lib lib64 libexec sbin share src
/*mkdir
  新建,可以是目录,可以是文件
*/
[root@Lettiy ~]# mkdir mytest
[root@Lettiy ~]# ls
mytest
[root@Lettiy mytest]# mkdir text.txt
[root@Lettiy mytest]# ls
text.txt

  文件处理指令(移动,删除,复制<cp 基本操作类似于mv>)

/*mv
 可用于改名、也可用于移动
*/
[root@Lettiy mytest]# mv text.txt newname.txt
[root@Lettiy mytest]# ls
newname.txt
[root@Lettiy mytest]# mv newname.txt newdir
[root@Lettiy mytest]# ls
newdir
[root@Lettiy mytest]# cd newdir
[root@Lettiy newdir]# ls

newname.txt

/*rm
 用于删除文件
 普通删除文件用rm -f xx
 普通目录删除 rm -rf xx
 批量删除同一名字 rm -v xx* (此处*类似于通配符)
*/
[root@Lettiy newdir]# ls
new1 new2 new3 new4 newname.txt
[root@Lettiy newdir]# rm -f newname.txt
[root@Lettiy newdir]# ls
new1 new2 new3 new4
[root@Lettiy newdir]# rm -rf new4.txt
[root@Lettiy newdir]# ls
new1 new2. new3
[root@Lettiy newdir]# rm -rf -v new*
removed directory: ‘new1'
removed directory: ‘new2'
removed directory: ‘new3'

     文件下载与解压

/*wget
 文件下载
 wget url即可
*/
[root@Lettiy newdir]# wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.79/bin/apache-tomcat-7.0.79.tar.gz
--2017-08-13 23:35:56-- http://mirrors.hust.edu.cn/apache/tomcat/tomcat-7/v7.0.79/bin/apache-tomcat-7.0.79.tar.gz
Resolving mirrors.hust.edu.cn (mirrors.hust.edu.cn)... 202.114.18.160
Connecting to mirrors.hust.edu.cn (mirrors.hust.edu.cn)|202.114.18.160|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 8975395 (8.6M) [application/octet-stream]
Saving to: ‘apache-tomcat-7.0.79.tar.gz
100%[================================================================================>] 8,975,395 19.3KB/s in 4m 36s
2017-08-13 23:40:33 (31.8 KB/s) - ‘apache-tomcat-7.0.79.tar.gz' saved [8975395/8975395]

/*tar
 对于tar.gz文件进行解压,解压为rpm文件再安装
 tar -zxvf 
*/
[root@Lettiy newdir]# ls
apache-tomcat-7.0.79.tar.gz
[root@Lettiy newdir]# tar -zxvf apache-tomcat-7.0.79.tar.gz
apache-tomcat-7.0.79/bin/catalina.sh
apache-tomcat-7.0.79/bin/configtest.sh
apache-tomcat-7.0.79/bin/daemon.sh
apache-tomcat-7.0.79/bin/digest.sh
……
[root@Lettiy newdir]# ls
apache-tomcat-7.0.79 apache-tomcat-7.0.79.tar.gz

文件安装与卸载

CentOS集成了yum,可配置源(repository)进行安装

/*yum 安装/卸载
 yum install software
 yum remove software
*/
[root@Lettiy newdir]# yum install postgresql
/*查看yum可安装的软件包,可配合grep进行关键字查询,例如‘java'*/
[root@Lettiy newdir]# yum list
[root@Lettiy newdir]# yum list|grep 'java'

/*rpm
 刚刚解压tar得到的rpm文件则需要使用rpm
 安装rpm -ivh
 删除rpm -e
 查看已经安装rpm -qa
*/
[root@Lettiy newdir]# rpm -ivh software.rpm

  文件更改/查看

/*文件查看
 cat指令、more指令、vi指令三者都可以实现查看
*/
[root@Lettiy newdir]# cat /etc/profile
# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
pathmunge () {
 case ":${PATH}:" in
  *:"$1":*)
   ;;
  *)
   if [ "$2" = "after" ] ; then
    PATH=$PATH:$1
   else
    PATH=$1:$PATH
   fi
 esac
}
if [ -x /usr/bin/id ]; then
 if [ -z "$EUID" ]; then
  # ksh workaround
  EUID=`/usr/bin/id -u`
  UID=`/usr/bin/id -ru`
 fi
 USER="`/usr/bin/id -un`"
 LOGNAME=$USER
 MAIL="/var/spool/mail/$USER"
fi
# Path manipulation
if [ "$EUID" = "0" ]; then
 pathmunge /usr/sbin
 pathmunge /usr/local/sbin
else
 pathmunge /usr/local/sbin after
 pathmunge /usr/sbin after
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
 export HISTCONTROL=ignoreboth
else
 export HISTCONTROL=ignoredups
fi
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
 umask 002
else
 umask 022
fi
for i in /etc/profile.d/*.sh ; do
 if [ -r "$i" ]; then
  if [ "${-#*i}" != "$-" ]; then 
   . "$i"
  else
   . "$i" >/dev/null
  fi
 fi
done
unset i
unset -f pathmunge
export JAVA_HOME=/usr/develop/java/jdk1.8.0_144
export PATH=$JAVA_HOME/bin/:$PATH
export CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar
export CATALINA_HOME=/usr/develop/tomcat/apache-tomcat-8.5.20
/*vi
 vi directory
 如果目录下存在则打开
 如果不存在则新建一个空文件 
*/

如果要修改,进入按 I  ,即可进入insert模式,进行更改;

保存:先ESC,然后输入:

wq:保存退出

q!:不保存退出

主要用于修改配置文件 ,例如:etc/proflie

 端口和进程监控常用

/*ps
  检测软件是否运行
  或查看正在运行的进程
  ps -ef|grep 'name' 
  例如:检测tomcat的运行状况
*/
[root@Lettiy newdir]# ps -ef|grep 'tomcat'
root   19785   1 0 Aug12 "htmlcode">
  yum install lrzsz

   然后使用rz sz即可上传下载。

总结

以上所述是小编给大家介绍的Linux 日常常用指令及应用小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!