Linux常用命令笔记

1. tar 解压缩xz文件

    flying-bird@flyingbird:~/books$ ll        
    总用量 71500  
    drwxrwxr-x  4 flying-bird flying-bird     4096  5月 22 19:39 ./  
    drwxr-xr-x 38 flying-bird flying-bird     4096  5月 22 19:16 ../  
    drwxrwxr-x  4 flying-bird flying-bird     4096  5月 22 19:38 linux/  
    -rw-r--r--  1 flying-bird flying-bird 73195712  8月 20  2013 linux-3.10.7.tar.xz  
    drwxrwxr-x  2 flying-bird flying-bird     4096  4月 16 19:28 Python/  
    flying-bird@flyingbird:~/books$ tar -xvf linux-3.10.7.tar.xz   
    linux-3.10.7/  
    linux-3.10.7/.gitignore  


2. unrar解压rar文件

flying-bird@flyingbird:~/books/Tcl$ ll
总用量 20104
drwxrwxr-x 2 flying-bird flying-bird     4096  6月  9 19:53 ./
drwxrwxr-x 8 flying-bird flying-bird     4096  6月  9 19:53 ../
-rw-r--r-- 1 flying-bird flying-bird 11000000  6月  9 19:48 Tcl_TK编程权威指南.part1.rar
-rw-r--r-- 1 flying-bird flying-bird  9572860  6月  9 19:51 Tcl_TK编程权威指南.part2.rar
flying-bird@flyingbird:~/books/Tcl$ unrar e ./Tcl_TK编程权威指南.part1.rar 

UNRAR 4.00 beta 3 freeware      Copyright (c) 1993-2010 Alexander Roshal


Extracting from ./Tcl_TK编程权威指南.part1.rar

Extracting  Tcl_TK编程权威指南.pdf                              53%

Extracting from ./Tcl_TK编程权威指南.part2.rar

...         Tcl_TK编程权威指南.pdf                              OK 
All OK
flying-bird@flyingbird:~/books/Tcl$ 


选项e是解压缩到当前目录,且是把所有文件都解压缩到当前目录,不再有目录层次结构。如果rar仅包括一个文件,则e是合适的。


更多时候,用 unrar x file.rar the_dest_dir。这时把file.rar解压缩到the_dest_dir目录下面,且保持之前到目录结构。


3. find 查找文件

flying-bird@flyingbird:/usr/include$ find . -name "errno.h"
./linux/errno.h
./asm-generic/errno.h
./errno.h
./i386-linux-gnu/asm/errno.h
./i386-linux-gnu/sys/errno.h
./i386-linux-gnu/bits/errno.h
flying-bird@flyingbird:/usr/include$ 

4. find 查找包含特定字符串的文件

flying-bird@flyingbird:/usr/include$ find . -name "*.h" | xargs grep "SIGABRT"
./asm-generic/signal.h:#define SIGABRT		 6
./i386-linux-gnu/asm/signal.h:#define SIGABRT		 6
./i386-linux-gnu/bits/signum.h:#define	SIGABRT		6	/* Abort (ANSI).  */
flying-bird@flyingbird:/usr/include$ 

5. man 在线系统

用man查找某个命令或某个函数相关的信息。

man 1 stat的执行结果:

STAT(1)                                                    User Commands                                                   STAT(1)

NAME
       stat - display file or file system status

SYNOPSIS
       stat [OPTION]... FILE...

DESCRIPTION
       Display file or file system status.

       -L, --dereference
              follow links

       -f, --file-system
              display file system status instead of file status

       -c  --format=FORMAT
              use the specified FORMAT instead of the default; output a newline after each use of FORMAT



man 2 stat的执行结果:

STAT(2)                                              Linux Programmer's Manual                                             STAT(2)

NAME
       stat, fstat, lstat - get file status

SYNOPSIS
       #include <sys/types.h>
       #include <sys/stat.h>
       #include <unistd.h>

       int stat(const char *path, struct stat *buf);
       int fstat(int fd, struct stat *buf);
       int lstat(const char *path, struct stat *buf);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       lstat():
           _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
           || /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200112L

DESCRIPTION

man命令后面的1&2&3等,可以执行man man命令来了解。如下:

MAN(1)                                                  Manual pager utils                                                  MAN(1)

NAME
       man - an interface to the on-line reference manuals

SYNOPSIS
       man  [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-m system[,...]] [-M path] [-S list] [-e exten‐
       sion] [-i|-I] [--regex|--wildcard] [--names-only] [-a] [-u] [--no-subpages] [-P  pager]  [-r  prompt]  [-7]  [-E  encoding]
       [--no-hyphenation] [--no-justification] [-p string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] [[section] page ...] ...
       man -k [apropos options] regexp ...
       man -K [-w|-W] [-S list] [-i|-I] [--regex] [section] term ...
       man -f [whatis options] page ...
       man  -l [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-P pager] [-r prompt] [-7] [-E encoding] [-p
       string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] file ...
       man -w|-W [-C file] [-d] [-D] page ...
       man -c [-C file] [-d] [-D] page ...
       man [-hV]

DESCRIPTION
       man is the system's manual pager. Each page argument given to man is normally the name of a program, utility  or  function.
       The  manual  page  associated with each of these arguments is then found and displayed. A section, if provided, will direct
       man to look only in that section of the manual.  The default action is to search in all of the available sections,  follow‐
       ing a pre-defined order and to show only the first page found, even if page exists in several sections.

       The table below shows the section numbers of the manual followed by the types of pages they contain.

       1   Executable programs or shell commands
       2   System calls (functions provided by the kernel)
       3   Library calls (functions within program libraries)
       4   Special files (usually found in /dev)
       5   File formats and conventions eg /etc/passwd
       6   Games
       7   Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
       8   System administration commands (usually only for root)
       9   Kernel routines [Non standard]

       A manual page consists of several sections.

6.  ulimit 设置资源使用门限

如设置core文件大小、调用栈大小等:

flying-bird@flyingbird:~/examples$ ulimit -c
0
flying-bird@flyingbird:~/examples$ ulimit -s
8192
flying-bird@flyingbird:~/examples$ ulimit -s unlimited
flying-bird@flyingbird:~/examples$ ulimit -s
unlimited
flying-bird@flyingbird:~/examples$ 

关于core,请参考 C代码的coredump


7. sudo 提升用户权限

程序执行时,如果需要访问系统资源,可能会提示(错误码13),即Permission denied。——此时需要用sudo运行程序。

示例:在Linux系统的机器上查看MBR数据 (续)一文中,我们已经遇到过这种使用方法。

flying-bird@flyingbird:~/examples/cpp/read_mbr$ g++ main.cpp file-utils.cpp mbr.cpp -I.
flying-bird@flyingbird:~/examples/cpp/read_mbr$ ./a.out 
Open file failed: Permission denied.
flying-bird@flyingbird:~/examples/cpp/read_mbr$ vi main.cpp
flying-bird@flyingbird:~/examples/cpp/read_mbr$ g++ main.cpp file-utils.cpp mbr.cpp -I.
flying-bird@flyingbird:~/examples/cpp/read_mbr$ ./a.out 
Open file failed(13): Permission denied.
flying-bird@flyingbird:~/examples/cpp/read_mbr$ sudo ./a.out 
boot loader:

 Addr  00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

0x0000 EB 63 90 00 02 8E D7 BC 00 7A BB A0 07 8B CE 8E 
0x0010 DB 8E C3 F3 A4 EA 5D 00 A0 07 10 00 01 00 00 7A 
0x0020 00 00 00 00 00 00 00 00 00 00 07 66 8B 55 08 B4 

上面的错误打印,对应于main.cpp的如下修改:

flying-bird@flyingbird:~/examples/cpp/read_mbr$ cat main.cpp
#include <cstdio>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "mbr.h"

int main() 
{
    int fd = -1;
    const unsigned int SECTOR_SIZE = 512;
    unsigned char buf[SECTOR_SIZE];
    ssize_t read_count;
    const char* DEV_NAME = "/dev/sda";

    fd = open(DEV_NAME, O_RDONLY);
    if (fd == -1) {
        printf("Open file failed(%d): %s.\n", errno, strerror(errno));
        return -1;
    }

    read_count = read(fd, buf, SECTOR_SIZE);
    if (read_count != SECTOR_SIZE) {
        printf("Read error: %d\n", read_count);
        close(fd);
        return -1;
    }

    struct mbr mbr = {0};

    get_mbr(buf, mbr);

    close(fd);
    return 0;
}


flying-bird@flyingbird:~/examples/cpp/read_mbr$ 

以上代码中的strerror()等,请参考 通过strerror获取详细的错误信息

8. md5/sha1sum 计算SHA1消息签名

flying-bird@flyingbird:~/examples/cpp/sha1$ echo "hello" > hello1.txt
flying-bird@flyingbird:~/examples/cpp/sha1$ sha1sum -b hello1.txt 
f572d396fae9206628714fb2ce00f72e94f2258f *hello1.txt
flying-bird@flyingbird:~/examples/cpp/sha1$ echo -n "hello" > hello2.txt
flying-bird@flyingbird:~/examples/cpp/sha1$ sha1sum -b hello2.txt 
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d *hello2.txt
flying-bird@flyingbird:~/examples/cpp/sha1$ 

计算SHA1的C代码,请参考计算SHA1


Mac OS X下面:

1)md5的用法:md5 file_name

2)sha1的用法:shasum file_name —— 这个和Linux有些区别。

9. hexdump 显示文件的二进制形式

flying-bird@flyingbird:~/examples/vi$ hexdump -C ./test.txt 
00000000  54 68 65 20 66 69 72 73  74 20 6c 69 6e 65 2e 0d  |The first line..|
00000010  0a 54 68 65 20 73 65 63  6f 6e 64 20 6c 69 6e 65  |.The second line|
00000020  2e 0d 0a 0d 0a                                    |.....|
00000025
flying-bird@flyingbird:~/examples/vi$ 

10. 查看每个分区的空间占用情况

flying-bird@flyingbird:~$ df -hl
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda5        84G  4.2G   75G    6% /
udev            999M  4.0K  999M    1% /dev
tmpfs           202M  912K  201M    1% /run
none            5.0M     0  5.0M    0% /run/lock
none           1006M  156K 1006M    1% /run/shm
/dev/sda7       134G  3.8G  124G    3% /home
/dev/sdb6        20G   19G  1.5G   93% /media/SOFTWARE
/dev/sdb5        15G   14G  2.0G   88% /media/EBOOK
/dev/sdb1        16G   15G  1.3G   92% /media/PHOTO
/dev/sdb7        25G   23G  1.7G   94% /media/English
flying-bird@flyingbird:~$ 

11. 查看每个分区的inode占用情况

flying-bird@flyingbird:~$ df -i
文件系统         Inode 已用(I) 可用(I) 已用(I)% 挂载点
/dev/sda5      5545984  242706 5303278       5% /
udev            215754     602  215152       1% /dev
tmpfs           219409     548  218861       1% /run
none            219409       3  219406       1% /run/lock
none            219409       7  219402       1% /run/shm
/dev/sda7      8912896   98136 8814760       2% /home
/dev/sdb6            0       0       0        - /media/SOFTWARE
/dev/sdb5            0       0       0        - /media/EBOOK
/dev/sdb1            0       0       0        - /media/PHOTO
/dev/sdb7      1829880    5000 1824880       1% /media/English
flying-bird@flyingbird:~$ 

12. 查看每个目录的空间占用情况

flying-bird@flyingbird:~/books$ du -h --max-depth=1 .
573M	./linux-3.10.7
89M	./Python
1.1G	./linux
1.8G	.
flying-bird@flyingbird:~/books$ du -h --max-depth=1 ..
108K	../.fontconfig
16K	../.sudoku
52K	../.sunpinyin
。。。。。。。
3.0M	../.local
764K	../examples
8.0G	..
flying-bird@flyingbird:~/books$ du -h --max-depth=1 ../examples
240K	../examples/png
4.0K	../examples/git
12K	../examples/java
332K	../examples/cpp
164K	../examples/python
8.0K	../examples/vi
764K	../examples
flying-bird@flyingbird:~/books$ 

13. 修改hosts文件

flying-bird@flyingbird:~/android$ su
密码: 
root@flyingbird:/home/flying-bird/android# cd /etc
root@flyingbird:/etc# ll
总用量 1196
drwxr-xr-x 137 root root    12288  6月  1 11:04 ./
drwxr-xr-x  23 root root     4096  5月 27 18:55 ../
drwxr-xr-x   3 root root     4096  3月 26 21:08 acpi/
-rw-r--r--   1 root root     2981  4月 24  2012 adduser.conf
...........
-rw-r--r--   1 root root       92  4月 19  2012 host.conf
-rw-r--r--   1 root root       11  3月 24 14:32 hostname
-rw-r--r--   1 root root      225  3月 24 14:32 hosts
-rw-r--r--   1 root root      580  4月 27  2012 hosts.allow
-rw-r--r--   1 root root      880  4月 27  2012 hosts.deny
...........
drwxr-xr-x   2 root root     4096  4月 27  2012 xml/
drwxr-xr-x   2 root root     4096  3月 26 21:09 xul-ext/
-rw-r--r--   1 root root      349  1月 30  2012 zsh_command_not_found
root@flyingbird:/etc# cat hosts
127.0.0.1	localhost
127.0.1.1	flyingbird

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
root@flyingbird:/etc# vi hosts
root@flyingbird:/etc# cat ./hosts
127.0.0.1	localhost
127.0.1.1	flyingbird
74.125.31.136 dl-ssl.google.com

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
root@flyingbird:/etc# 

14. ln建立软链接

flying-bird@flyingbird:~/examples$ ll *.txt
ls: 无法访问*.txt: 没有那个文件或目录
flying-bird@flyingbird:~/examples$ echo "the source file" > source.txt
flying-bird@flyingbird:~/examples$ ll *.txt
-rw-rw-r-- 1 flying-bird flying-bird 16  6月  5 19:42 source.txt
flying-bird@flyingbird:~/examples$ mkdir testln
flying-bird@flyingbird:~/examples$ cd testln
flying-bird@flyingbird:~/examples/testln$ ln -s ../source.txt ./target.txt
flying-bird@flyingbird:~/examples/testln$ ll
总用量 8
drwxrwxr-x 2 flying-bird flying-bird 4096  6月  5 19:42 ./
drwxrwxr-x 9 flying-bird flying-bird 4096  6月  5 19:42 ../
lrwxrwxrwx 1 flying-bird flying-bird   13  6月  5 19:42 target.txt -> ../source.txt
flying-bird@flyingbird:~/examples/testln$ cat target.txt
the source file
flying-bird@flyingbird:~/examples/testln$ 

15. 7z

首先安装7z软件,在软件中心搜索7z,可以找到7zip。安装即可。

命令行:7z x -othe_dest_directory file_to_be_unzip.7z


16. scp

scp用来在远程计算机和本地计算机之间拷贝文件。
本地拷贝到远程:
scp local_file_name remote_user_name@remote_ip_addr:remote_directory
scp local_file_name remote_user_name@remote_ip_addr:remote_file_name
scp -r local_directory remote_user_name@remote_ip_addr:remote_directory

远程拷贝到本地:
scp remote_user_name@remote_ip_addr:remote_file_name local_file_name

执行命令后要求输入远程机器的密码。

另外本地需要关闭对远程机器的连接,否则会出现: Resource temporarily unavailable.


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值