Linux 命令 網上總結

 

一、常用命令

1、查看操作系統

> lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:    16.04
Codename:    xenial

centos:

[root@localhost ~]# cat /etc/issue
CentOS release 5.5 (Final)

Kernel r on an m

如何查看已安裝的CentOS版本信息

2、下載,安裝、解壓

> apt-get install build-essential
> wget ftp://ftp.openldap.org/pub/OpenLDAP/openldap-release/openldap-2.4.31.tgz

> tar -zxf openldap-2.4.31.tgz

3、修改root密碼

切換root用戶

su root

Ubuntu的
默認root密碼是隨機的,即每次開機都有一個新的root密碼。我們可以在終端輸入命令 sudo passwd,然後輸入當前用戶的密碼:

vagrant@homestead:/usr/local/src/db-5.1.29.NC/build_unix$ sudo passwd
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
vagrant@homestead:/usr/local/src/db-5.1.29.NC/build_unix$

4、刪除文件夾及文件命令

// 刪除文件夾實例:
rm -rf /var/log/httpd/access
將會刪除/var/log/httpd/access目錄以及其下所有文件、文件夾

刪除文件使用實例:
rm -f /var/log/httpd/access.log
將會強制刪除/var/log/httpd/access.log這個文件

5、查看軟件文件安裝路徑

whereis

在linux中文件與軟件一般都是安裝在到/usr/share/usr/local中了,如果我們需要查看軟件安裝路徑linux爲我們提供了查看命令,whereis 就可以幫我查找文件安裝路徑在哪裏了。

比如你想查找eclipse文件,那麼就:

# whereis eclipse

會顯示:

eclipse: /usr/bin/eclipse /usr/lib/eclipse /usr/share/eclipse

6、查詢運行文件所在路徑

1.如果你只要查詢文件的運行文件所在地址,直接用下面的命令就可以了(還是以Oracle爲例):

which oracle

結果會顯示:

/usr/bin/oracle

以上只適合安裝路徑加入PATH

如果是源碼安裝的話,你可以使用-prefix指定安裝路徑,這樣,你可以很輕鬆知道軟件的安裝路徑;

2.如果是rpm包安裝的話,可以使用rpm -qal查詢一下

rpm -qf /路徑/程序名 可以查到一個程序屬於哪個rpm包,然後rpm -ql那個包可以看到那個包的所有文件和路徑等等。還可以解開成一個cpio文件等等之類的,仔細研究一下rpm的參數吧。
完全可以做到任何你想做的。

源碼的安裝一般由3個步驟組成:配置(configure)、編譯(make)、安裝(make install),具體的安裝方法一般作者都會給出文檔,這裏主要討論配置(configure)。
Configure是一個可執行腳本,它有很多選項,使用命令./configure –help輸出詳細的選項列表,如下:

-bash-3.00# ./configure --help

Usage: configure [options] [host]

Options: [defaults in brackets after descriptions]

Configuration:

--cache-file=FILE cache test results in FILE

--help print this message

--no-create do not create output files

--quiet, --silent do not print `checking...' messages

--version print the version of autoconf that created configure

Directory and file names:

--prefix=PREFIX install architecture-independent files in PREFIX

[/usr/local]

--exec-prefix=EPREFIX install architecture-dependent files in EPREFIX

[same as prefix]

--bindir=DIR user executables in DIR [EPREFIX/bin]

7、查看服務器本機IP

Ubuntu,Unix

ifconfig

CentOS:

ip addr

clipboard.png

windows

ipconfig

8.find 命令彙總

find -name april*                      在當前目錄下查找以april開始的文件
find -name april* fprint file        在當前目錄下查找以april開始的文件,並把結果輸出到file中
find -name ap* -o -name may* 查找以ap或may開頭的文件
find /mnt -name tom.txt -ftype vfat 在/mnt下查找名稱爲tom.txt且文件系統類型爲vfat的文件
find /mnt -name t.txt ! -ftype vfat   在/mnt下查找名稱爲tom.txt且文件系統類型不爲vfat的文件
find /tmp -name wa* -type l           在/tmp下查找名爲wa開頭且類型爲符號鏈接的文件
find /home -mtime -2                 在/home下查最近兩天內改動過的文件
find /home   -atime -1                  查1天之內被存取過的文件
find /home -mmin   +60                  在/home下查60分鐘前改動過的文件
find /home -amin +30                  查最近30分鐘前被存取過的文件
find /home -newer tmp.txt             在/home下查更新時間比tmp.txt近的文件或目錄
find /home -anewer tmp.txt            在/home下查存取時間比tmp.txt近的文件或目錄
find /home -used -2                  列出文件或目錄被改動過之後,在2日內被存取過的文件或目錄
find /home -user cnscn                列出/home目錄內屬於用戶cnscn的文件或目錄
find /home -uid +501                 列出/home目錄內用戶的識別碼大於501的文件或目錄
find /home -group cnscn              列出/home內組爲cnscn的文件或目錄
find /home -gid 501                   列出/home內組id爲501的文件或目錄
find /home -nouser                    列出/home內不屬於本地用戶的文件或目錄
find /home -nogroup                   列出/home內不屬於本地組的文件或目錄
find /home   -name tmp.txt   -maxdepth 4 列出/home內的tmp.txt 查時深度最多爲3層
find /home -name tmp.txt -mindepth 3 從第2層開始查
find /home -empty                     查找大小爲0的文件或空目錄
find /home -size +512k               查大於512k的文件
find /home -size -512k               查小於512k的文件
find /home -links +2                 查硬連接數大於2的文件或目錄
find /home -perm 0700                查權限爲700的文件或目錄
find /tmp -name tmp.txt -exec cat {} /;
find /tmp -name tmp.txt -ok rm {} /;

find   / -amin   -10       # 查找在系統中最後10分鐘訪問的文件
find   / -atime -2         # 查找在系統中最後48小時訪問的文件
find   / -empty              # 查找在系統中爲空的文件或者文件夾
find   / -group cat        # 查找在系統中屬於 groupcat的文件
find   / -mmin -5         # 查找在系統中最後5分鐘裏修改過的文件
find   / -mtime -1        #查找在系統中最後24小時裏修改過的文件
find   / -nouser             #查找在系統中屬於作廢用戶的文件
find   / -user   fred       #查找在系統中屬於FRED這個用戶的文件

查當前目錄下的所有普通文件
--------------------------------------------------------------------------------

# find . -type f -exec ls -l {} /;
-rw-r--r--    1 root     root        34928 2003-02-25 ./conf/httpd.conf
-rw-r--r--    1 root     root        12959 2003-02-25 ./conf/magic
-rw-r--r--    1 root     root          180 2003-02-25 ./conf.d/README
查當前目錄下的所有普通文件,並在- e x e c選項中使用ls -l命令將它們列出


=================================================
在/ l o g s目錄中查找更改時間在5日以前的文件並刪除它們:
$ find logs -type f -mtime +5 -exec -ok rm {} /;


=================================================
查詢當天修改過的文件
[root@book class]# find ./ -mtime -1 -type f -exec ls -l {} /;


=================================================
查詢文件並詢問是否要顯示
[root@book class]# find ./ -mtime -1 -type f -ok ls -l {} /;
< ls ... ./classDB.inc.php > ? y
-rw-r--r--    1 cnscn    cnscn       13709 1月 12 12:22 ./classDB.inc.php
[root@book class]# find ./ -mtime -1 -type f -ok ls -l {} /;
< ls ... ./classDB.inc.php > ? n
[root@book class]#

=================================================
查詢並交給awk去處理
[root@book class]# who | awk '{print $1"/t"$2}'
cnscn   pts/0

=================================================
awk---grep---sed

[root@book class]# df -k | awk '{print $1}' | grep -v 'none' | sed s"///dev////g"
文件系統
sda2
sda1
[root@book class]# df -k | awk '{print $1}' | grep -v 'none'
文件系統
/dev/sda2
/dev/sda1

1)在/tmp中查找所有的*.h,並在這些文件中查找“SYSCALL_VECTOR",最後打印出所有包含"SYSCALL_VECTOR"的文件名

A) find /tmp -name "*.h" | xargs -n50 grep SYSCALL_VECTOR
B) grep SYSCALL_VECTOR /tmp/*.h | cut   -d':' -f1| uniq > filename
C) find /tmp -name "*.h" -exec grep "SYSCALL_VECTOR" {} /; -print

2)find / -name filename -exec rm -rf {} /;
find / -name filename -ok rm -rf {} /;

3)比如要查找磁盤中大於3M的文件:
find . -size +3000k -exec ls -ld {} ;

9、yum命令

yum (Yellowdog Updater Modified)是一個集與查找,安裝,更新和刪除程序的Linux軟件。它運行在RPM包兼容的Linux發行版本上,如:RedHat, Fedora, SUSE, CentOS, Mandriva。

今天我在用CentOS 5.5的yum命令安裝的時候出現如下錯誤:

[root@malu2 ~]# yum install vim
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.hosting90.cz
* extras: mirror.hosting90.cz
* updates: mirror.hosting90.cz
Setting up Install Process
No package vim available.
Nothing to do

對於此錯誤,我一般習慣性先去更新一下yum倉庫:

#yum -y update

再次運行安裝,發現錯誤依舊,看來這種猜包名的方法不適用;所以接下來可以通過yum的查找參數來列出vim相關的包名:

[root@malu2 yum.repos.d]# yum search vim
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirror.hosting90.cz
* extras: mirror.hosting90.cz
* updates: mirror.hosting90.cz
===================== Matched: vim ===================
vim-X11.x86_64 : The VIM version of the vi editor for the X Window System.
vim-common.x86_64 : The common files needed by any version of the VIM editor.
vim-enhanced.x86_64 : A version of the VIM editor which includes recent
: enhancements.
vim-minimal.x86_64 : A minimal version of the VIM editor.

通過查看,我們可以看到,yum列出了詳細的vim相關的包名,這樣就可以順利的安裝vim了:

#yum -y install vim-enhanced.x86_64

以上命令就能執行成功了。當然不僅僅vim安裝,其他程序比如MySQL我們也同樣只要先進行yum查找,再通過匹配詳細包名來進行安裝,就不會出現上述錯誤了

3)如果還是不行,應該是 EPEL未被啓動,且指示的 rpm包不可用
具體原因:使用yum搜索某些rpm包,找不到包是因爲CentOS是RedHat企業版編譯過來的,去掉了所有關於版權問題的東西。安裝EPEL後可以很好的解決這個問題。EPEL(Extra Packages for Enterprise Linux )即企業版Linux的擴展包,提供了很多可共Centos使用的組件,安裝完這個以後基本常用的rpm都可以找到。
下載與安裝EPEL的rpm文件包
1.下載:

// 根據不同版本下載,我的是centos 6.8,所以,下載第一個鏈接epel-release-6-8.noarch.rpm
# wget http://mirrors.ustc.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm
# wget http://mirrors.ustc.edu.cn/fedora/epel/5/i386/epel-release-5-4.noarch.rpm

2.安裝:

#rpm -ivh epel-release-5-4.noarch.rpm

安裝完畢後,可以使用yum安裝軟件。
注意:

  1. epel的版本要與centos的版本相對應,否則安裝失敗,即centos5版本要對應epel5版本。
  2. rpm安裝完後,rpm -ql epel*,確提示未安裝,此時要導入rpm的簽名信息
#rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 (root身份執行)

10. 各linux版本重啓apache命令

Slackware Linux命令:

  /etc/rc.d/rc.httpd restart

ubuntu、Debian 系統命令:

  /etc/init.d/apache2 restart

Fedora 、Redhat、CentOS系統重啓Apache命令:

  /etc/init.d/httpd restart

  或

  service httpd restart(CentOS 成功)

11、Linux下好用的編輯器gedit

以前我都是用vi/vim 編輯器編輯文件,但是這樣的環境很難操作,不能粘貼和複製,開發效率非常慢,還需要許多的命令,很不友好,忽然發現geidt這款編輯器非常棒,我們可以使用這款編輯器就像是在windows下使用txt編輯器一樣方便.

安裝:

> yum install gedit

使用,其實和vim的用法一樣:

> gedit php_log.log

12、配置Apache訪問路徑

// 修改訪問路徑virtual-hosts.conf

[root@baidu extra]# pwd
/usr/local/apache/conf/extra

編輯virtual-hosts.conf配置文件

<VirtualHost *:80>
    DocumentRoot "/usr/local/apache/htdocs"
    ServerName sso.com
    ErrorLog "sso.com-error_log"
    CustomLog "sso.com-access_log" common
<Directory "/usr/local/apache/htdocs">
     Options Indexes FollowSymLinks
     AllowOverride All
     Order deny,allow
     Allow From all
    </Directory>
</VirtualHost>

13、linux下重命名文件或文件夾

linux下重命名文件或文件夾的命令 mv 既可以重命名,又可以移動文件或文件夾.

例子:將目錄A重命名爲B

mv A B

例子:將/a目錄移動到/b下,並重命名爲c

mv /a /b/c

其實在文本模式中要重命名文件或目錄的話也是很簡單的,我們只需要使用mv命令就可以了,比如說我們要將一個名爲abc的文件重命名爲1234就可以這樣來寫:mv abc 1234,但是要注意的是,如果當前目錄下也有個1234的文件的話,我們的這個文件是會將它覆蓋的

14、修改Linux系統時間

先修改時區,然後再調整具體時間

[root@camel Asia]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cp: overwrite ‘/etc/localtime’? y
[root@camel Asia]# date
Thu Jul 27 18:50:49 CST 2017
[root@camel Asia]# date
Thu Jul 27 18:51:25 CST 2017
[root@camel Asia]# 

date命令不僅可以顯示系統當前時間,還可以用它來修改系統時間,下面簡單的介紹下,方便需要的朋友
命令格式爲:

date -s 時間字符串

例如只修改系統的日期,不修改時間(時分秒)
date -s 2012-08-02
或只修改時間不修改日期

date -s 10:08:00

當然也可以同時修改日期和時間
date -s "2012-05-18 04:53:00"
注意:由於日期和時間之間有空格,所以必須用引號引起來,否則命令會報錯。
上述修改只是修改了linux的系統時間,CMOS中的時間可能還沒有改變,所以爲了保險,需要使用 clock -w 把當前系統時間寫入到CMOS中。
系統時間和CMOS時間的關係。系統時間是由linux操作系統來維護的;CMOS時間是CMOS芯片保存的時間。系統啓動時,操作系統將從CMOS讀出時間記錄爲系統時間,同時操作系統也會自動每隔一段時間將系統時間寫入CMOS中。如果使用date命令修改系統時間後馬上重啓電腦,操作系統還沒有將系統時間同步到CMOS,這樣開機後就還是沒有修改前的時間了,所以爲了保險起見,最還還是手動使用命令 clock 將系統時間同步到CMOS中。

15、shell併發執行

使用curl模擬併發請求網頁

test.sh

#!/bin/bash

#betin time
begin=$(date +%s)

#批量處理
count=100
for(( i = 0; i < $count; i++ ))
do
{
  /usr/bin/curl http://172.18.82.21/xampps/web/login_test/index.php
}&
done

#結束時間
end=$(date +%s)
spend=$(expr $end - $begin)
echo "花費時間爲$spend秒"

執行腳本

// 執行腳本
> bash test.sh

16、Ubuntu apt-get安裝包報錯

Ubuntu Unable to parse package file [duplicate]

// 刪除apt軟件緩存: 
sudo rm -r /var/lib/apt/lists/*

// 更新apt軟件列表: 
sudo apt-get update

17、安裝LAMP 環境

在Ubuntu下裝了一下php的環境,也就是裝了一下MySQL、PHP、Apache.

#安裝mysql

sudo apt-get install mysql-server

#這個完成後會有一個要求輸入root用戶密碼的部分

sudo apt-get install mysql-client

#安裝apache

sudo apt-get install apache2

#安裝php

sudo apt-get install php5

#安裝連接關係(直接都配置好了,windows下改這個改那個的情況完全不用擔心)

sudo apt-get install libapache2-mod-auth-mysql

sudo apt-get install php5-mysql

然後就是檢測成果了:

在瀏覽器輸入localhost看到如下界面就說明Apache安裝成功了

在終端輸入

mysql -username –password

成功登陸就是mysql成功率。

檢驗php由於權限問題稍微麻煩一點,Apache的的默認安裝路徑爲/var/www/。我們需要到其下建立一個php文件,但是用戶是沒有權限的,首先我們要獲得權限:

su root 
chown username /var/www

然後寫個php文件輸出phpinfo

文件如下:

<?php
phpinfo();
?>

18、查看php進程

首先,用ps查看進程,方法如下:

$ ps -ef

或者:

$ ps -aux

clipboard.png

此時如果我想殺了test.php的進程就在終端輸入:

$ kill -s 9 8098

其中-s 9 制定了傳遞給進程的信號是9,即強制、儘快終止進程。各個終止信號及其作用見附錄。

1827則是上面ps查到的test.php的PID。

簡單吧,但有個問題,進程少了則無所謂,進程多了,就會覺得痛苦了,無論是ps -ef 還是ps -aux,每次都要在一大串進程信息裏面查找到要殺的進程,看的眼都花了。

進階篇:

改進1:

把ps的查詢結果通過管道給grep查找包含特定字符串的進程。管道符“|”用來隔開兩個命令,管道符左邊命令的輸出會作爲管道符右邊命令的輸入。

$ ps -ef | grep firefox
smx 1827 1 4 11:38 ? 00:27:33 /usr/lib/firefox-3.6.18/firefox-bin
smx 12029 1824 0 21:54 pts/0 00:00:00 grep --color=auto firefox

這次就清爽了。然後就是

$kill -s 9 1827

還是嫌打字多?

詳細文章:linux下殺死進程(kill)的N種方法

19.php判斷腳本是否已經在執行

在定時腳本中,如果判斷腳本已經在進程中執行,則不再執行

// 傳入腳本路徑,也可以傳入腳本路徑+參數
function check_is_run($command) {
    if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
        return true;
    } else {
        $handle = popen("ps -ef | grep php 2>&1", 'r');
        $cmd_cont = '';
        while(!feof($handle)) {
            $buffer = fgets($handle);
            $cmd_cont .= $buffer;
        }
        pclose($handle);
        if (strpos($cmd_cont, $command) === false){
            return false;
        }else{
            return true;
        }
    }
}

20.在 Linux 下執行 php 腳本

在 Linux 下執行 php 腳本時通常是可以使用以下命令:

先通過which命令來找到PHP的執行路徑:

which php 
/usr/bin/php
#/usr/bin/php/ test.php

爲簡化輸入可把此php執行程序 copy 到系統命令目錄下:

#cp /usr/bin/php /usr/sbin/

驗證:輸入以下命令,如正確獲取 php 版本,說明操作成功。

#php -v

這樣,以後再執行PHP腳本時可直接採用以下輸入方式即可。

#php a.php

21.進入目錄默認訪問index.php 或index.html目錄

進入目錄讓默認訪問index.php 或index.html目錄
找到apache的配置文件 httpd.conf

[root@ldap /]# find -name httpd.conf
./root/httpd-2.2.11/docs/conf/httpd.conf
./usr/local/apache/conf/original/httpd.conf
./usr/local/apache/conf/httpd.conf
[root@ldap /]# cd /usr/local/apache/conf/
[root@ldap conf]# cat httpd.conf

修改Apache配置文件

<IfModule dir_module>
    DirectoryIndex index.html test.php 
</IfModule>

clipboard.png

22.Linux 系統定時備份

備份是不可忽略的一環。如果我們的系統因爲不同原因宕機,此時,冗餘備份就顯得非常重要。

系統中比較重要的目錄有 /etc/,/home,root,/var/spool/mail 等,你想要在每天2:00am 進行備份,且備份數據保存到 /backup 內,並使用 tar 將備份數據打包,那該如何處理呢?

通常我們會使用 shell script 來進行備份數據的彙總,然後將該腳本添加到定時任務中,如下例:

[root@localhost ~] # mkdir /root/bin; vim /root/bin/backup.sh
#!/bin/bash
backdir="/etc /home /root /var/spool/mail"
basedir=/backup
# [ ] 部分是判斷表達式,-d 表示判斷是否是目錄(directory)
[ ! -d "$basedir" ] && mkdir $basedir
backfile=$basedir/backup.tar.gz
tar -zcvf $backfile $backdir

[root@localhost ~] # vim /etc/crontab
0 2 * * * root sh /root/bin/backup.sh

23、查看文件大小

查看文件的大小,一般是ls 下面會顯示,你要看具體的大小,可以用ls -lh來顯示,M會顯示出來的

ls -lh

clipboard.png

24、生成一個大文件

createBigFile.php

<?php

$url = "http://www.jianshu.com/p/3884a6b7a780";
$content = file_get_contents($url);


// write
for($i = 0; $i <= 10000; $i++)
{

  file_put_contents("big_file.txt", $content, FILE_APPEND);
}

該腳本可以在服務器下生成 500M 的文件

# /usr/local/php/bin/php createBigFile.php

將該文件通過 curl 傳輸,可以測試佔用的寬帶及網速:

<?php
$time1=microtime(true);

$post = array(
    "excel"=>"@big_file.txt",//這裏是要上傳的文件,key與後臺處理文件對應
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://120.12.11.9:9999/upload/receive.php");

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);


$a=curl_exec($ch);

var_dump($a);

curl_close($ch);

$b=round($a,2);

$size='size='.$b.'MB<br>';
echo $size;

$time2=microtime(true);
$time3=$time2-$time1;

echo 'diff='.round($time3,2).'<br>';

echo 'rate='.round(($b/$time3),2).'MB/S';

?>

查看各個進程使用的寬帶情況

# 查看各個進程使用的寬帶情況
# iftop -i eth1

25、查看開放端口

命令一:

netstat -a 查看所有服務端口

命令二:

yum install nmap  // 安裝nmap軟件包
nmap +ip地址       // nmap 127.0.0.1

linux開放服務端口的設置

/etc/sysconfig/iptables

vi iptables

加入如下內容

-A INPUT -p tcp -m tcp --dport 6001 -j ACCEPT

然後保存退出,重啓服務

/etc/init.d/iptables restarts

查看端口是否開放

/sbin/iptables -L -n

26、du命令查看磁盤空間

du(disk usage)命令可以計算文件或目錄所佔的磁盤空間。沒有指定任何選項時,它會測量當前工作目錄與其所有子目錄,分別顯示各個目錄所佔的快數,最後才顯示工作目錄所佔總快數。
有時候搜索出來的結果太多了(譬如,我從根目錄開始搜索),一直在刷屏,如果我只想查出最大的12個文件夾,怎麼辦呢?此時就要藉助head命令來顯示了
du -hm --max-depth=1 | sort -nr | head -12

 

 

查找大於800M的文件
find . -type f -size +800M

將文件信息都顯示出來

find . -type f -size +800M  -print0 | xargs -0 ls -l
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章