美文网首页
Linux tool

Linux tool

作者: abrocod | 来源:发表于2016-06-16 08:52 被阅读35次

    Reference


    Bash Shell Shortcut

    • 在 bash 里,使用 Ctrl-R 而不是上下光标键来查找历史命令。
    • 在 bash里,使用 Ctrl-W 来删除最后一个单词,使用 Ctrl-U 来删除一行。请man bash后查找Readline Key Bindings一节来看看bash的默认热键,比如:Alt-. 把上一次命令的最后一个参数打出来,而Alt-* 则列出你可以输入的命令。

    System Directory Structure

    • /bin : All the executable binary programs (file) required during booting, repairing, files required to run into single-user-mode, and other important, basic commands 需要在[单用户模式]可用的必要命令([可执行文件]);面向所有用户,例如:cat, ls, cp.
    • /boot : Holds important files during boot-up process, including Linux Kernel.
    • /dev : Contains device files for all the hardware devices on the machine e.g., cdrom, cpu, etc
    • /etc : Contains Application’s configuration files, startup, shutdown, start, stop script for every individual program.
    • /home : Home directory of the users. Every time a new user is created, a directory in the name of user is created within home directory which contains other directories. For Mac, user files are stored in Users.
    • /lib : The Lib directory contains kernel modules and shared library images required to boot the system and run commands in root file system.
    • /media : Temporary mount directory is created for removable devices viz., media/cdrom.
    • /mnt : Temporary mount directory for mounting file system.
    • /opt : Optional is abbreviated as opt. Contains third party application software. Viz., Java, etc.
    • /proc : A virtual and pseudo file-system which contains information about running process with a particular Process-id aka pid.
    • /root : This is the home directory of root user and should never be confused with ‘/
    • /run : This directory is the only clean solution for early-runtime-dir problem.
    • /sbin : Contains binary executable programs, required by System Administrator, for Maintenance. 必要的系统二进制文件,例如: init、 ip、 mount。
    • /srv : Service is abbreviated as ‘srv‘. This directory contains server specific and service related files.
    • /sys : Modern Linux distributions include a /sys directory as a virtual filesystem, which stores and allows modification of the devices connected to the system.
    • /tmp :System’s Temporary Directory, Accessible by users and root. Stores temporary files for user andsystem, till next boot.
    • /usr : Contains executable binaries, documentation, source code, libraries for second level program.
    • /var : Stands for variable. The contents of this file is expected to grow. This directory contains log, lock,spool, mail and temp files.

    Some further explanation

    • bin is different from lib. bin is for binary file, while lib or Library is for library (like dynamic/static linking library).

    • /usr – User Programs
      Contains binaries, libraries, documentation, and source-code for second level programs.

      • /usr/bin contains binary files for user programs. If you can’t find a user binary under /bin, look under /usr/bin. For example: at, awk, cc, less, scp. 非必要可执行文件(在单用户模式中不需要);面向所有用户。
      • /usr/sbin contains binary files for system administrators. If you can’t find a system binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd, useradd, userdel. 非必要的系统二进制文件,例如:大量[网络服务]的[守护进程]。
      • /usr/lib contains libraries for /usr/bin and /usr/sbin
      • /usr/local contains users programs that you install from source. For example, when you install apache from source, it goes under /usr/local/apache2. 本地数据的第三层次, 具体到本台主机。通常而言有进一步的子目录, 例如:bin/, lib/, share/.
    • /lib – System Libraries
      Contains library files that supports the binaries located under /bin and /sbin
      Library filenames are either ld* or lib.so.
      For example: ld-2.11.1.so, libncurses.so.5.7

    • /etc – Configuration Files
      Contains configuration files required by all programs.
      This also contains startup and shutdown shell scripts used to start/stop individual programs.
      For example: /etc/resolv.conf, /etc/logrotate.conf

    • /bin – User Binaries
      Contains binary executables.
      Common linux commands you need to use in single-user modes are located under this directory.
      Commands used by all the users of the system are located here.
      For example: ps, ls, ping, grep, cp.

    • /sbin – System Binaries
      Just like /bin, /sbin also contains binary executables.
      But, the linux commands located under this directory are used typically by system aministrator, for system maintenance purpose.
      For example: iptables, reboot, fdisk, ifconfig, swapon

    • /etc/ssh/

    Other important directories under user directory:

    • /Users/abrocod/.ssh
    • /Users/abrocod/.aws

    Common Shell Command


    Simple Command Collection

    • which
    • whereis
    • whoami
    • date
    • printenv
    • env
    • alias
    alias ls='ls --color=auto'
    alias grep='grep --color=auto'
    

    sudo -i # run as root user
    sudo !!
    sudo -E

    ls

    -l :列出长数据串,包含文件的属性与权限数据等
    -a :列出全部的文件,连同隐藏文件(开头为.的文件)一起列出来(常用)
    -d :仅列出目录本身,而不是列出目录的文件数据
    -h :将文件容量以较易读的方式(GB,kB等)列出来
    -R :连同子目录的内容一起列出(递归列出),等于该目录下的所有文件都会显示出来

    du, df

    check directory size and check the whole disk space utility
    du -sh
    du -h
    df -h

    grep

    grep [-acinv] [--color=auto] '查找字符串' filename

    -a :将binary文件以text文件的方式查找数据
    -c :计算找到‘查找字符串’的次数
    -i :忽略大小写的区别,即把大小写视为相同
    -v :反向选择,即显示出没有‘查找字符串’内容的那一行

    ------- 例如:-------
    取出文件/etc/man.config中包含MANPATH的行,并把找到的关键字加上颜色:
    grep --color=auto 'MANPATH' /etc/man.config
    把ls -l的输出中包含字母file(不区分大小写)的内容输出:
    ls -l | grep -i file

    • search for pattern

    grep "pattern" -r *

    cut

    clean file:
    cut -c3-73 sample_thumbid.txt > clean_tbnail.txt

    find

    find [PATH] [option] [action]
    与时间有关的参数:
    -mtime n : n为数字,意思为在n天之前的“一天内”被更改过的文件;
    -mtime +n : 列出在n天之前(不含n天本身)被更改过的文件名;
    -mtime -n : 列出在n天之内(含n天本身)被更改过的文件名;
    -newer file : 列出比file还要新的文件名
    例如:
    find /root -mtime 0 # 在当前目录下查找今天之内有改动的文件

    与用户或用户组名有关的参数:
    -user name : 列出文件所有者为name的文件
    -group name : 列出文件所属用户组为name的文件
    -uid n : 列出文件所有者为用户ID为n的文件
    -gid n : 列出文件所属用户组为用户组ID为n的文件
    例如:
    find /home/ljianhui -user ljianhui # 在目录/home/ljianhui中找出所有者为ljianhui的文件

    与文件权限及名称有关的参数:
    -name filename :找出文件名为filename的文件
    -size [+-]SIZE :找出比SIZE还要大(+)或小(-)的文件
    -tpye TYPE :查找文件的类型为TYPE的文件,TYPE的值主要有:一般文件(f)、设备文件(b、c)、
    目录(d)、连接文件(l)、socket(s)、FIFO管道文件(p);
    -perm mode :查找文件权限刚好等于mode的文件,mode用数字表示,如0755;
    -perm -mode :查找文件权限必须要全部包括mode权限的文件,mode用数字表示
    -perm +mode :查找文件权限包含任一mode的权限的文件,mode用数字表示
    例如:
    find / -name passwd # 查找文件名为passwd的文件
    find ./ -name '.o'
    find ./ -name "
    .o" -exec rm {} ;
    find . -perm 0755 # 查找当前目录中文件权限的0755的文件
    find . -size +12k # 查找当前目录中大于12KB的文件,注意c表示byte

    cp

    -a :将文件的特性一起复制
    -p :连同文件的属性一起复制,而非使用默认方式,与-a相似,常用于备份
    -i :若目标文件已经存在时,在覆盖时会先询问操作的进行
    -r :递归持续复制,用于目录的复制行为
    -u :目标文件与源文件有差异时才会复制

    cp -a file1 file2 #连同文件的所有特性把文件file1复制成文件file2
    cp file1 file2 file3 dir #把文件file1、file2、file3复制到目录dir中

    mv

    -f :force强制的意思,如果目标文件已经存在,不会询问而直接覆盖
    -i :若目标文件已经存在,就会询问是否覆盖
    -u :若目标文件已经存在,且比目标文件新,才会更新

    mv file1 file2 file3 dir # 把文件file1、file2、file3移动到目录dir中
    mv file1 file2 # 把文件file1重命名为file2

    rm

    -f :就是force的意思,忽略不存在的文件,不会出现警告消息
    -i :互动模式,在删除前会询问用户是否操作
    -r :递归删除,最常用于目录删除,它是一个非常危险的参数

    rm -i file # 删除文件file,在删除之前会询问是否进行该操作
    rm -fr dir # 强制删除目录dir中的所有文件

    file

    file filename
    例如:
    file ./test

    tar

    -c :新建打包文件
    -t :查看打包文件的内容含有哪些文件名
    -x :解打包或解压缩的功能,可以搭配-C(大写)指定解压的目录,注意-c,-t,-x不能同时出现在同一条命令中
    -j :通过bzip2的支持进行压缩/解压缩
    -z :通过gzip的支持进行压缩/解压缩
    -v :在压缩/解压缩过程中,将正在处理的文件名显示出来
    -f filename :filename为要处理的文件
    -C dir :指定压缩/解压缩的目录dir

    压缩:tar -zcvf filename.tar.gz 要被处理的文件或目录名称
    查询:tar -ztvf filename.tar.gz
    解压:tar -zxvf filename.tar.gz -C 欲解压缩的目录

    tar -zcvf test.tar.gz *
    Note: -f option must follow the filename !!!

    -- if the file is just .tar, not .tar.gz or .tar.bz2, then use:
    tar -xvf filename.tar -C ./dir

    -- if the file is just a .zip
    unzip file.zip -d destination_folder

    zip

    zip squash.zip file1 file2 file3
    zip -r squash.zip dir1 # zip recursively, -r
    unzip squash.zip

    use less squash.zip to look at the content of zip file
    or use 'unzip -l squash.zip', which is the same as less

    ln

    ln cc ccAgain :硬连接;删除一个,将仍能找到;
    ln -s cc ccTo :符号链接(软链接);删除源,另一个无法使用;(后面一个ccTo 为新建的文件)

    ln -s TARGET LINK_NAME

    • Find the link after softlink
    ls -l `which java`
    lrwxr-xr-x  1 root  wheel  74 Sep  1 15:27 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
    

    chgrp, chown

    chgrp [-R] dirname/filename
    -R :进行递归的持续对所有文件和子目录更改
    例如:
    chgrp users -R ./dir # 递归地把dir目录下中的所有文件和子目录下所有文件的用户组修改为users

    groups <username> -- check which group user belongs to

    -- chown
    chown owner-user file
    chown owner-user:owner-group file
    chown owner-user:owner-group directory
    chown options owner-user:owner-group file

    e.g:
    chown vivek demo.txt # change owner
    chown vivek:vivek demo.txt # change owner and group
    chown :ftp demo.txt # change only group
    chown -R vivek /home/vivek # recursively change

    open file in Ubuntu

    You can use:
    gnome-open file2open.xxx (xxx = some file extension).
    With this command gnome will invoke the default app for "xxx"

    ----------- for example evince if you want to open pdf ------------
    Or specifically:
    evince file2open.pdf

    Or (default for KDE):
    okular file2open.pdf

    process management

    ps

    ps [aux]
    ps aux | grep 'chrome' | less

    pstree

    top

    Top will give you a realtime view of the system and only show the number of processes which will fit on the screen.

    kill

    kill [signal] <PID>
    e.g:
    kill 6978
    kill -1 6978 // kill/quit the process gently
    kill -9 6978 // kill the process with brutal force

    lsof

    If you know what port the process is running you can type: lsof -i:<port>. For instance, lsof -i:8080, to list the process (pid) running on port 8080. Then kill the process with kill <pid>

    tail

    The command tail -f will display the last 10 lines of a file, and then continuously wait for new lines, and display them as they appear.

    $ tail -f /var/log/messages

    If you want to see more than ten lines at the outset, specify the new number (say 50 lines) like this:

    $ tail -50 -f /var/log/messages


    相关文章

      网友评论

          本文标题:Linux tool

          本文链接:https://www.haomeiwen.com/subject/hudmdttx.html