linux 常用命令 dd/ln/cat/echo/grep/tar/wc/find/xargs/ssh/scp/strace/strings

来源:互联网 发布:淘宝模特拍照24姿势 编辑:程序博客网 时间:2024/04/30 02:02

ln、cat、echo 的常用example:

ln -s [需要被链接的source] [需要新创建的快捷方式target]cat > filename << EOF  // 新建&覆盖文件 或者 Ctrl+D 结束cat >> filename <<EOF  // 追加到文件末尾 appendcat -n filename  // 显示行号echo "some text" > filenameecho ‘some text’ >> filename

单引号与双引号的区别,双引号可以展开变量,可用于某些需要转义的特殊字符(例如创建带空格的文件夹),二单引号内的文字只能解释为字符串:

i5@localhost:~$s=sometexti5@localhost:~$echo "$s"sometexti5@localhost:~$echo '$s'$si5@localhost:~$mkdir "a test folder"

grep 的常用 example:

grep -H -n -i "somestr" a.txt  // 使用 -H 选项可以显示文件名,使用 -n 选项可以显示行号, -i 忽略大小写// -A n 表示显示匹配行以及其后的 n 行;-B n 表示显示匹配行以及之前的 n 行。例如,在匹配行的前后分别额外显示两行:grep -A 2 -B 2 -H -n  "somestr" a.txtgrep -E -w 'vivek|raj[0-9]' a.txt  // -E 表示使用正则表达式(--extended-regexp), 匹配 vivek 或者 raj0 至 raj9; -w 表示全字匹配grep -r "some string"  // 在当前目录下查找所有的文件包含字符串 "some string" 的文件要注意第一点的是,grep -r 与 通配符的配合。 grep -r 不能够接通配符,但是能够接正则表达式。例如,要搜索 libxxx.so.xxx 这种文件,不能够用 grep -r "lib*.so.*"因为 "." 表示匹配任何字符, "*" 在正则表达式中是表示一个或者多个的之前的模式的意思。因此,需要写成如下形式(反斜杠是转义的意思,其他需要转义的字符: * . ? + $ ^ [ ] ( ) { } | \ /):grep -r "lib.*\.so\."

tar 的常用example:

# 压缩打包tar pczf myarchive.tar.gz /home/guowei/mydocuments    [p] 这个选项表示 “preserve”,它指示 tar 在归档文件中保留文件属主和权限信息。如果不需要保存属性可省略之。    [c] 表示创建。要创建文件时不能缺少这个选项。    [z] z 选项启用 gzip 压缩。    [f] file 选项告诉 tar 创建一个归档文件;如果没有这个选项 tar 会把打包好的文件输出发送到标准输出,那屏幕会是一堆乱码;或者打印compressed data not written to a terminal.    如果要保存绝对路径则加上 -P 选项。否则默认都是相对路径。解压的时候都是相对于当前路径的,不能解压到绝对路径中。如果要保持用户和组信息,需要加 --ower 和 --group, 例如:tar -cpPvf orocos.tar /opt/orocos --owner=0 --group=0  # 这里只能用 UID 和 GID 数字。

Linux doesn’t use internally owners and groups names but numbers - UIDs and GIDs. Users and groups names are mapped from contents of /etc/passwd and /etc/group files for convenience of user. see link: http://unix.stackexchange.com/questions/61004/force-the-owner-and-group-for-the-contents-of-a-tar-file

如果要修改打包的路径,加上 -C 选项:
http://stackoverflow.com/questions/18681595/tar-a-directory-but-dont-store-full-absolute-paths-in-the-archive

# 解压,unpacktar xzvf myarchive.tar.gz    [x] x 表示提取,提取 tar 文件时这个命令不可缺少。    [z] z 选项告诉 tar 要解压的归档文件是 gzip 格式; 如果是bzip2则使用 -j如果解压的时候要保持用户和权限信息,需要加 sudo 和 -p 选项。# 列出压缩包中的内容tar -jtvf xxx.tar    [t] t 表示列出的意思。

wc 的常用example:

wc 命令用来完成文本统计工作,通过使用不同的选项,它可以统计文件中的字节数(-c),字符数(-m),单词数(-w)与行数(-l// 统计somestr出现的次数(行数)grep  -i "somestr" a.txt | wc -l  

find 的常用example:

find ./ -iname "*somestr?.*"   # 以文件名查找,通配符,大小写无关find ./ -name "*somestr?.*" -or -name "anothername"  #多个文件,通配符find ./ -type f -exec grep -i "some content" {} \;   #注意,{}+空格+\: 空格一个也不能错; 搜索普通文件(f:普通文件,d:文件夹),并且查找其中大小无关字符串 "some content"find ./* -exec touch {} \;  # 修改时间戳,例如make的时候,警告:XXX文件的修改时间在将来1.4e+09问题。find ./ -type f | xargs grep "some conten"  # 同上,可用于查找系统中包含某些字符串的文件,很方便。# 查找多个文件,find 与或非: -o 或;-a 与;-not 非find . -maxdepth 1 -type f | xargs grep 'XXX'  # 指定目录深度# 还有两个比较有用的选项:-print   # print the full file name on the standard output, followed by a newline.-print0  # print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses).

比较完整的 find 例子: http://alvinalexander.com/unix/edu/examples/find.shtml

ssh 和 scp

ssh username@192.168.1.5// 远程拷贝,-r 可用于复制目录scp -r /paht/to/file username@192.168.1.5:/paht/to/file

strace 简介

strace 常用来跟踪进程执行时的系统调用和所接收的信号,可以跟踪到一个进程产生的系统调用,包括参数,返回值,执行消耗的时间,因此可以用来debug,或者作为性能分析的工具。// 例子:$  strace lsexecve("/bin/ls", ["ls"], [/* 21 vars */]) = 0brk(0)                                  = 0x8c31000access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)mmap2(NULL, 8192, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb78c7000access("/etc/ld.so.preload", R_OK)      = -1 ...// 以上每一行都是一条系统调用,等号左边是系统调用的函数名及其参数,右边是该调用的返回值$ strace -e open ls  // 只显示 open 系统调用的信息$ strace -e trace=open,read ls /home  // 显示指定的多个系统调用 open,read$  sudo strace -p 1725  // 指定进程的 pid$ strace -c ls /home  // -c 统计每一系统调用的所执行的时间,次数和出错的次数等$ strace -ttT ls  // -tt 在输出中的每一行前加上时间信息,微秒级(前); -T 显示每一调用所耗的时间(后尖括号),如下:22:00:40.313902 close(1)                = 0 <0.000007>22:00:40.313951 munmap(0x7f95eb276000, 4096) = 0 <0.000013>22:00:40.313988 close(2)                = 0 <0.000005>22:00:40.314061 exit_group(0)           = ?22:00:40.314239 +++ exited with 0 +++

strings

输出文件中可打印的字符,包括可执行文件,函数库等二进制文件;可用于查看可执行文件中的写死的文件路径,打开的文件名等等信息,亦可用于逆向工程。还有 ltrace、nm 等命令可查看可执行程序 (没有strip过的) 中的函数信息。strings a.out  // 打印 可执行文件 中的字符串strings -f a.out  // 在每一个字符串前面加上文件名,这个很有用,例如如下find例子:find ./lib -name "*.so*" | xargs strings -f | grep "blabla~"  // 找到 所有 .so 动态库中包含 "blabla" 字符串的库,并显示文件名。

dd

不同于 cp 是在文件系统层面的复制,dd命令是更low-level的复制(字节层面的复制,可以复制文件,分区表,其他数据等等),因此dd一般用来创建和备份镜像文件。

example:

# 将一个硬盘备份至另一个硬盘,见下文释义:dd if=/dev/sda of=/dev/sdb bs=32K conv=noerror,sync// 注意,这里的 bs 大小对拷贝速度有很大影响,一般取 bs=8M/128K/32K/4K 等等。默认好像是 512 Byte(不加任何suffixes表示Byte), see "man dd"// man 手册中推荐查看进度的拷贝方法:$ dd if=/dev/zero of=/dev/null& pid=$!$ kill -USR1 $pid; sleep 1; kill $pid// 截断文件dd if=/dev/null of=/file/to/truncate seek=1 bs=123456  # truncate file to 123456 bytes  // dd --help可知,seek=N 表示跳过of文件中的N个bs的数据块,因此这条命令表示截断文件为123456 bytes大小。

Here, ‘if’ stands for input file , ‘of’ stands for output file and ‘bs’ stands for the block size (number of bytes to be read/write at a time). The conversion parameter ‘noerror’ allows the tool to continue to copy the data eventhough it encounter any errors. The sync option allows to use synchronized I/O.

The above command will copy all the data from the disk /dev/sda to /dev/sdb. ‘dd’ doesn’t know anything about the filesystem or partitions- it will just copy everything from /dev/sda to /dev/sdb. So, this will clone the disk with the same data on same partition.

关于dd显示进度:

watch -n 5 pkill -USR1 ^dd$  // 正则表达式匹配 ^dd$ 的 pid,然后发送信号

link: http://linoxide.com/linux-command/linux-dd-command-create-1gb-file/

1 0
原创粉丝点击