常用的linux命令

  1. tar
    创建一个新的tar文件
    tarcvfarchivename.tardirname/tar tar xvf archive_name.tar
    查看tar文件
    $ tar tvf archive_name.tar
    更多示例:The Ultimate Tar Command Tutorial with 10 Practical Examples
  2. grep
    在文件中查找字符串(不区分大小写)
    grepithedemofile grep -A 3 -i “example” demo_text
    在一个文件夹中递归查询包含指定字符串的文件
    $ grep -r “ramesh” *
    更多示例:Get a Grip on the Grep! – 15 Practical Grep Command Examples
  3. find
    查找指定文件名的文件(不区分大小写)
    findinameMyProgram.c find -iname “MyProgram.c” -exec md5sum {} \;
    查找home目录下的所有空文件
    $ find ~ -empty
    更多示例:Mommy, I found it! — 15 Practical Linux Find Command Examples
  4. ssh
    登录到远程主机
    sshljsmithremotehost.example.comssh ssh -v -l jsmith remotehost.example.com
    显示ssh客户端版本
    $ ssh -V
    更多示例:5 Basic Linux SSH Client Commands
  5. sed
    当你将Dos系统中的文件复制到Unix/Linux后,这个文件每行都会以\r\n结尾,sed可以轻易将其转换为Unix格式的文件,使用\n结尾的文件
    seds/. //’ filename
    反转文件内容并输出
    sedn1!G;h;pfilename sed ‘/./=’ thegeekstuff.txt | sed ‘N; s/\n/ /’
    更多示例:Advanced Sed Substitution Examples
  6. awk
    删除重复行
    awk!( 0 in array) { array[0]; print}’ temp  
    打印/etc/passwd中所有包含同样的uid和gid的行
    awk -F ':' ' 3= 4’ /etc/passwd
    打印文件中的指定部分的字段
    awk '{print2,$5;}’ employee.txt
    更多示例:8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
  7. vim
    打开文件并跳到第10行
    vim+10filename.txt vim +/search-term filename.txt
    以只读模式打开文件
    $ vim -R /etc/passwd
    更多示例:How To Record and Play in Vim Editor
  8. diff
    比较的时候忽略空白符
    $ diff -w name_list.txt name_list_new.txt
  9. sort
    以升序对文件内容排序
    sortnames.txt sort -r names.txt
    以第三个字段对/etc/passwd的内容排序
    $ sort -t: -k 3n /etc/passwd | more
  10. export
    输出跟字符串oracle匹配的环境变量
    export|grepORCALEdeclarexORACLEBASE=/u01/app/oracledeclarexORACLEHOME=/u01/app/oracle/product/10.2.0declarexORACLESID=meddeclarexORACLETERM=xterm export ORACLE_HOME=/u01/app/oracle/product/10.2.0
  11. xargs
    将所有图片文件拷贝到外部驱动器
    ls.jpg|xargsn1icp/externalharddrive/directoryjpd find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz
    下载文件中列出的所有url对应的页面
    $ cat url-list.txt | xargs wget –c
  12. ls
    以易读的方式显示文件大小(显示为MB,GB…)
    lslhrwr1rameshteamdev8.9MJun1215:27archlinux.txt.gz ls -ltr
    在文件名后面显示文件类型
    $ ls -F
    更多示例:Unix LS Command: 15 Practical Examples
  13. pwd
    输出当前工作目录
  14. cd
    cd -可以在最近工作的两个目录间切换
    使用shopt -s cdspell可以设置自动对cd命令进行拼写检查
    更多示例:6 Awesome Linux cd command Hacks
  15. gzip
    创建一个*.gz的压缩文件
    gziptest.txt.gz gzip -d test.txt.gz
    显示压缩的比率
    $ gzip -l *.gz
    compressed uncompressed ratio uncompressed_name
    23709 97975 75.8% asp-patch-rpms.txt
  16. bzip2
    创建*.bz2压缩文件
    $ bzip2 test.txt
    解压*.bz2文件
    bzip2 -d test.txt.bz2
    更多示例:BZ is Eazy! bzip2, bzgrep, bzcmp, bzdiff, bzcat, bzless, bzmore examples
  17. uzip
    解压*.zip文件
    unziptest.zip.zip unzip -l jasper.zip
    Archive: jasper.zip
    Length Date Time Name
    ——– —- —- —-
    40995 11-30-98 23:50 META-INF/MANIFEST.MF
    32169 08-25-98 21:07 classes_
    15964 08-25-98 21:07 classes_names
    10542 08-25-98 21:07 classes_ncomp
  18. shutdown
    关闭系统并立即关机
    shutdownhnow10 shutdown -h +10
    重启
    shutdownrnow shutdown -Fr now
  19. ftp
    ftp命令和sftp命令的用法基本相似连接ftp服务器并下载多个文件
    $ ftp IP/hostname
    ftp> mget *.html
    显示远程主机上文件列表
    ftp> mls *.html -
    /ftptest/features.html
    /ftptest/index.html
    /ftptest/othertools.html
    /ftptest/samplereport.html
    /ftptest/usage.html
    更多示例:FTP and SFTP Beginners Guide with 10 Examples
  20. crontab
    查看某个用户的crontab入口
    $ crontab -u john -l
    设置一个每十分钟执行一次的计划任务
    /10 * * * /home/ramesh/check-disk-space
    更多示例:Linux Crontab: 15 Awesome Cron Job Examples
  21. service
    service命令用于运行System V init脚本,这些脚本一般位于/etc/init.d文件下,这个命令可以直接运行这个文件夹里面的脚本,而不用加上路径
    查看服务状态
    servicesshstatus service –status-all
    重启服务
    $ service ssh restart
  22. ps
    ps命令用于显示正在运行中的进程的信息,ps命令有很多选项,这里只列出了几个
    查看当前正在运行的所有进程
    psef|moreH ps -efH | more
  23. free
    这个命令用于显示系统当前内存的使用情况,包括已用内存、可用内存和交换内存的情况
    默认情况下free会以字节为单位输出内存的使用量
    freetotalusedfreesharedbufferscachedMem:3566408158022019861880203988902960/+buffers/cache:4732723093136Swap:400017604000176使gGBmMBkKBb free -g
    total used free shared buffers cached
    Mem: 3 1 1 0 0 0
    -/+ buffers/cache: 0 2
    Swap: 3 0 3
    如果你想查看所有内存的汇总,请使用-t选项,使用这个选项会在输出中加一个汇总行
    ramesh@ramesh-laptop:~$ free -t
    total used free shared buffers cached
    Mem: 3566408 1592148 1974260 0 204260 912556
    -/+ buffers/cache: 475332 3091076
    Swap: 4000176 0 4000176
    Total: 7566584 1592148 5974436
  24. top
    top命令会显示当前系统中占用资源最多的一些进程(默认以CPU占用率排序)如果你想改变排序方式,可以在结果列表中点击O(大写字母O)会显示所有可用于排序的列,这个时候你就可以选择你想排序的列
    Current Sort Field: P for window 1:Def
    Select sort field via field letter, type any other key to return

    a: PID = Process Id v: nDRT = Dirty Pages count
    d: UID = User Id y: WCHAN = Sleeping in Function
    e: USER = User Name z: Flags = Task Flags
    ……..
    如果只想显示某个特定用户的进程,可以使用-u选项
    $ top -u oracle
    更多示例:Can You Top This? 15 Practical Linux Top Command Examples

  25. df
    显示文件系统的磁盘使用情况,默认情况下df -k 将以字节为单位输出磁盘的使用量
    dfkFilesystem1KblocksUsedAvailableUse/dev/sda12953040032331042479723212/dev/sda2120367992501715966408206044使h使 df -h
    Filesystem Size Used Avail Capacity iused ifree %iused Mounted on
    /dev/disk0s2 232Gi 84Gi 148Gi 37% 21998562 38864868 36% /
    devfs 187Ki 187Ki 0Bi 100% 648 0 100% /dev
    map -hosts 0Bi 0Bi 0Bi 100% 0 0 100% /net
    map auto_home 0Bi 0Bi 0Bi 100% 0 0 100% /home
    /dev/disk0s4 466Gi 45Gi 421Gi 10% 112774 440997174 0% /Volumes/BOOTCAMP
    //[email protected]/public 2.7Ti 1.3Ti 1.4Ti 48% 0 18446744073709551615 0% /Volumes/public
    使用-T选项显示文件系统类型
    $ df -T
    Filesystem Type 1K-blocks Used Available Use% Mounted on
    /dev/sda1 ext4 29530400 3233120 24797216 12% /
    /dev/sda2 ext4 120367992 50171596 64082060 44% /home
  26. kill
    kill用于终止一个进程。一般我们会先用ps -ef查找某个进程得到它的进程号,然后再使用kill -9 进程号终止该进程。你还可以使用killall、pkill、xkill来终止进程
    $ ps -ef | grep vim
    ramesh 7243 7222 9 22:43 pts/2 00:00:00 vim

kill972434WaystoKillaProcesskill,killall,pkill,xkill27.rm rm -i filename.txt
在文件名中使用shell的元字符会非常有用。删除文件前先打印文件名并进行确认
rmifile rm -r example
28. cp
拷贝文件1到文件2,并保持文件的权限、属主和时间戳
cppfile1file2file1file2file2 cp -i file1 file2
29. mv
将文件名file1重命名为file2,如果file2存在则提示是否覆盖
mvifile1file2使fv便 mv -v file1 file2
30. cat
你可以一次查看多个文件的内容,下面的命令会先打印file1的内容,然后打印file2的内容

$ cat file1 file2

-n命令可以在每行的前面加上行号

$ cat -n /etc/logrotate.conf
    1   /var/log/btmp {
    2       missingok
    3       monthly
    4       create 0660 root utmp
    5       rotate 1
    6   }
  1. mount
    如果要挂载一个文件系统,需要先创建一个目录,然后将这个文件系统挂载到这个目录上
# mkdir /u01
# mount /dev/sdb1 /u01

也可以把它添加到fstab中进行自动挂载,这样任何时候系统重启的时候,文件系统都会被加载
/dev/sdb1 /u01 ext2 defaults 0 2
32. chmod
chmod用于改变文件和目录的权限
给指定文件的属主和属组所有权限(包括读、写、执行)
chmodug+rwxfile.txt chmod g-rwx file.txt
修改目录的权限,以及递归修改目录下面所有文件和子目录的权限
chmodRug+rwxfile.txt7ChmodCommandExamplesforBeginners33.chownchownoracledb chown oracle:dba dbora.sh
使用-R选项对目录和目录下的文件进行递归修改
chownRoracle:dba/home/oracle34.passwdpasswd使 passwd
超级用户可以用这个命令修改其他用户的密码,这个时候不需要输入用户的密码

# passwd USERNAME

passwd还可以删除某个用户的密码,这个命令只有root用户才能操作,删除密码后,这个用户不需要输入密码就可以登录到系统

# passwd -d USERNAME
  1. mkdir
    在home目录下创建一个名为temp的目录
    mkdir /temp使p mkdir -p dir1/dir2/dir3/dir4/
  2. ifconfig
    ifconfig用于查看和配置Linux系统的网络接口
    查看所有网络接口及其状态
    ifconfiga使updown ifconfig eth0 up

ifconfigeth0downIfconfig:7ExamplesToConfigureNetworkInterface37.unameuname uname -a
Linux john-laptop 2.6.32-24-generic #41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linux
38. whereis
当你不知道某个命令的位置时可以使用whereis命令,下面使用whereis查找ls的位置
whereislsls:/bin/ls/usr/share/man/man1/ls.1.gz/usr/share/man/man1p/ls.1p.gzwhereis使B/tmplsmk whereis -u -B /tmp -f lsmk
lsmk: /tmp/lsmk
39. whatis
wathis显示某个命令的描述信息
$ whatis ls
ls (1) - list directory contents

whatisifconfigifconfig(8)configureanetworkinterface40.locatelocate使updatedbcrontab locate crontab
/etc/anacrontab
/etc/crontab
/usr/bin/crontab
/usr/share/doc/cron/examples/crontab2english.pl.gz
/usr/share/man/man1/crontab.1.gz
/usr/share/man/man5/anacrontab.5.gz
/usr/share/man/man5/crontab.5.gz
/usr/share/vim/vim72/syntax/crontab.vim
41. man
显示某个命令的man页面
mancrontabmanman man SECTION-NUMBER commandname
man页面一般可以分为8种命令类型
1.用户命令
2.系统调用
3.c库函数
4.设备与网络接口
5.文件格式
6.游戏与屏保
7.环境、表、宏
8.系统管理员命令和后台运行命令
例如,我们执行whatis crontab,你可以看到crontab有两个命令类型1和5,所以我们可以通过下面的命令查看命令类型5的man页面
$ whatis crontab
crontab (1) - maintain crontab files for individual users (V3)
crontab (5) - tables for driving cron

man5crontab42.tailtail10 tail filename.txt
你可以使用-n选项指定要显示的行数
tailnNfilename.txt使fCTRLC tail -f log-file
更多示例:3 Methods To View tail -f output of Multiple Log Files in One Terminal
43. less
这个命名可以在不加载整个文件的前提下显示文件内容,在查看大型日志文件的时候这个命令会非常有用
lesshugelogfile.loglessCTRL+FforwardonewindowCTRL+BbackwardonewindowUnixLessCommand:10TipsforEffectiveNavigation44.susu使 su - USERNAME
用另外一个用户名执行一个命令下面的示例中用户john使用raj用户名执行ls命令,执行完后返回john的账号
[john@dev-server]$ su - raj -c ‘ls’

[john@dev-server] 使shell su -s ‘SHELLNAME’ USERNAME
45. mysql
mysql可能是Linux上使用最广泛的数据库,即使你没有在你的服务器上安装mysql,你也可以使用mysql客户端连接到远程的mysql服务器
连接一个远程数据库,需要输入密码
mysqlurootph192.168.1.2 mysql -u root -p
你也可以在命令行中输入数据库密码,只需要在-p后面加上密码作为参数,可以直接写在p后面而不用加空格
46. yum
使用yum安装apache
yuminstallhttpdapache yum update httpd
卸载/删除apache
$ yum remove httpd
47. rpm
使用rpm安装apache

# rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm

更新apache

# rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm

卸载/删除apache

# rpm -ev httpd

更多示例:RPM Command: 15 Examples to Install, Uninstall, Upgrade, Query RPM Packages
48. ping
ping一个远程主机,只发5个数据包
$ ping -c 5 gmail.com
更多示例:Ping Tutorial: 15 Effective Ping Command Examples
49. date
设置系统日期

# date -s "01/31/2010 23:59:53"

当你修改了系统时间,你需要同步硬件时间和系统时间

# hwclock –systohc
# hwclock --systohc –utc
  1. wget
    使用wget从网上下载软件、音乐、视频
    $ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz
    下载文件并以指定的文件名保存文件
    $ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701

你可能感兴趣的:(常用的linux命令)