Bash,Vim,gdb&git常用命令

 

Bash


目录

pwd  //查看当前目录
mkdir dir1 dir2  //创建目录
tree dir1
mv test1.cpp test2.cpp dir1 dir  //移动文件/目录到目录dir


rm -r dir         删除目录
cp -r dir1 dir2 复制目录
文件信息

ll –t //列出详细信息,按时间排序
ls -R(ecursive) 目录树 -l 详细信息 -t 时间排序 -S 大小排序 -X 扩展名排序 -r(everse) 逆序 –h(uman) 大小以G/M为单位 –m 以M为单位 -a 列出包括隐藏文件


wc -l *.txt //看文件行数
du -sm * | sort -n //统计当前目录下文件及文件夹大小(以M为单位),并按大小排序

touch test1.cpp test2.cpp //将每个文件的访问时间和修改时间改为当前时间

Change the mode of each FILE to MODE.
With --reference, change the mode of each FILE to that of RFILE.

文件内容

cat file  //看文件内容
more file //从开始看,空格下一屏
tail -n 20 file //看最后n行,默认十行
tail -f file  //看日志时用,会刷新

压缩文件

tar –f 生成文件 目标文件
-f(ile) 必选 
-c creat压缩(默认)
-x eXtra解压
-j 使用压缩算法-bzip算法bz2
-z 使用压缩算法-gzip算法gz
–a(uto)  使用归档后缀名来决定压缩程序

tar -f archive.tar foo bar  # 从文件 foo 和 bar 创建归档文件 archive.tar。

tar -zf archive.tar.gz foo bar # 从文件 foo 和 bar 创建归档文件 archive.tar.gz。
tar -xf archive.tar          # 展开归档文件 archive.tar 中的所有文件。
tar –tf –v(erbose) archive.tar    # test-label 详细列举归档文件 archive.tar 中的所有文件。


*.gz          gzip程序压缩的文件

*.bz2        bzip2程序压缩的文件(比gzip效果好,但只能针对一个文件来压缩)

*.tar          tar程序打包的数据,没有压缩(使用tar打包成一个文件,可以解决bzip2不能压缩多个文件的问题)

*.tar.gz     tar程序打包的数据,并经过gzip的压缩

*.tar.bz2   tar程序打包的数据,并经过bzip2的压缩


zip -r ./src.zip ./*  压缩文件
unzip text.zip -d . 解压到当前目录
unzip -v text.zip  查看压缩文件目录,但不解压

bzip2 text.txt –f(orce) overwrite existing output files //压缩文件
bunzip2 text.txt.bz2 –k(eep)  keep (don't delete) input files //解压文件
bzcat *.bz2 或者 bunzip2 –c *.bz2 //解压文件到 stdout

拷贝文件

cp –i(nteractive) -r idirectory odirectory 拷贝文件夹i到文件夹o,覆盖前询问
scp -r work@XXXX:~/maxent_test/ ./st  主机之间安全拷贝文件,通过ssh协议传输

sz和rz传小文件大概10kb/s还行,大文件太慢了

下载文件 wget url 下载 -q 关闭输出
移动文件 mv /usr/local/arm/arm/* /usr/local/arm/
管道 ls | wc -l  将前一个命令的输出作为后一个命令的输入

> < 重定向
<< 追加

echo string >> file 追加文件

进程和任务

nohup cmd & 后台执行任务cmd


jobs 查看(当前终端?不晓得)所有运行任务
ps x 查看所有进程信息

renice [-10,+10] -p pid 设置进程优先级-10最高
grep

egrep "Test|Best arg|Final Eval Result"  letter/svm/*

grep -r(ecursive) -(in)v(ert) -(line-)n(umber) string file
grep "aaa" sample 文件下查找'aaa'
grep -r "aaa" .  目录下查找
grep -v "grep" sample 文件下查找非'grep'
grep -n "aaa" sample 显示行号

 

明确要求搜索子目录:grep -r
或忽略子目录:grep -d sk?ip?

grep magic /usr/src/linux/Documentation/* | less 许多输出,将其转到‘less’上阅读

grep -i pattern files :不区分大小写地搜索。默认情况区分大小写,
grep -l pattern files :只列出匹配的文件名,
grep -L pattern files :列出不匹配的文件名,
grep -C number pattern files :匹配的上下文分别显示[number]行,
grep pattern1 | pattern2 files :显示匹配 pattern1 或 pattern2 的行,
grep pattern1 files | grep pattern2 :显示既匹配 pattern1 又匹配 pattern2 的行。

grep -w pattern files :只匹配整个单词,而不是字符串的一部分
grep man * 会匹配 ‘Batman’、‘manic’、‘man’等,
grep '\<man' * 匹配‘manic’和‘man’,但不是‘Batman’,
grep '\<man\>' 只匹配‘man’,而不是‘Batman’或‘manic’等其他的字符串。
'^':指匹配的字符串在行首,
'$':指匹配的字符串在行尾,

find

find . -name '*.html' -exec grep 'mailto:' {} \;

find  \. -name \*.py -type f -exec echo {} \;   查找文件
-name /*.py
-type d(ir)/f(ile)
-size 1k 1k内文件
-exec

 

find path -option [ -print -exec -ok ...]
path ~=/home
-exec command {} \; 将查到的文件执行command操作,{} 和 \;之间有空格
-ok 和-exec相同,只不过在操作前要询用户
find -name "*.h"
     -prune 忽略某个目录
     -type b/d/c/p/l/f 查是块设备b、目录d、字符设备c、管道p、符号链接l、普通文件f
     -follow 如果遇到符号链接文件,就跟踪链接所指的文件
find /tmp -name "*.h" -exec grep "str" {} \; -print 在返回的文件list中查找str
find / -name '*.c'    -ok rm -rf {} \; 所有目录下查找并强制删除
find . -size +3000k   -exec ls -ld {} \;

-mmin nmkdir
查找系统中最后N分钟被改变文件数据的文件
-mtime n
查找系统中最后n*24小时被改变文件数据的文件
-mtime -n/+n
查找n天内或n天前修改的文件
-newer f1 !f2
查更改时间比f1新但比f2旧的文件
-amin n
查找系统中最后N分钟访问的文件
-atime n
查找系统中最后n*24小时访问的文件
-cmin n
查找系统中最后N分钟被改变文件状态的文件
-ctime n
查找系统中最后n*24小时被改变文件状态的文件

Shell命令行

tab       // 补全
#          //注释
ctrl+a   //home
ctrl+e //end

ctrl+l 清屏
reset 清屏(处理卡在半个字符的情况)


history -n 100|grep svn //查找历史命令记录
ctrl+r svn //一直往前查找

ctrl+c 取消命令

其他

man MD5

man3 MD5


ln -s 软链接 source target


su 切换到root账户

 

Vim


退出

:q  退出
:q! 不保存退出
:wq 保存并退出

保存

:w 保存
:w filename 另存为
:(#,#) w filename 另存(两行间内容)为

插入 :r filename 提取磁盘文件并将其插入到当前光标位置

o       在光标下方打开新的一行并将光标置于新开的行首,进入插入模式。
O 在光标上方打开新的一行并将光标置于新开的行首,进入插入模式。
a       可以在光标所在位置之后插入文本。
A 可以在光标所在行的行末之后插入文本。

复制粘贴

yy 复制
yw 复制单词

p 粘贴到下一行(将最后一次删除的内容置入光标之后)
P 粘贴到上一行
删除 x 删除

[number]   d    object ||  d    [number]   object
dw 从当前光标当前位置直到单字/单词末尾,包括空格
de 从当前光标当前位置直到单字/单词末尾,不包括空格
d$ 从当前光标当前位置直到当前行末
dd 删除整个当前行
行尾

:%s/\n//g     删除换行符

J 连接该行与下行,删除行尾的换行符 

:join     合并多行

撤销 u 撤消最后执行的(一次)命令
U 撤消在一行中所做的改动
CTRL-r 欲撤消以前的撤消命令,恢复以前的操作结果
查找

/+字符串 在当前文件中查找该字符串    
?+字符串  逆向查找字符串

:set ic     忽略大小写ignore case
:set hls is 高亮查找hlsearch 和 incsearch
% 可以查找配对的括号 )、]、}
n 下一个
Shift-n(N) 上一个

替换

:s/old/new    本行首个替换(在一行内替换头一个字符串 old 为新的字符串 new )
:s/old/new/g 本行全行替换(在一行内替换所有的字符串 old 为新的字符串 new)
:%s/old/new/g 全文全行替换(在文件内替换所有的字符串 old 为新的字符串 new)
:%s/old/new/gc 全文全行替换,询问用户确认每个替换
:#,#s/old/new/g 在两行内全行替换所有的字符串 old 为新的字符串 new
:.,$s/sgd/lbfgs/g   本行到末行全行替换
:n,$s/sgd/lbfgs/g   第n行到末行全行替换

Insert模式 r* 替换光标所在位置的字符
R*** 进入替换模式,直至按 <ESC> 键退出替换模式而进入正常模式。

[number]   c    object ||  c    [number]   object
cw** 不仅仅是替换了一个单词,也让您进入文本插入状态
c$ 替换从当前光标当前位置直到当前行末
特殊字符 :set list 显示以“$”表示的换行符和以“^I”表示的制表符
:set nolist 退出<list mode>
信息

CTRL-g 页面最底部出现状态信息行,显示文件名、总行数、行号。
:set nu 显示行号
:set nonu 隐藏行号

光标

G 使得当前光标直接跳转到文件最后一行
#G  跳转到#行(输入行号时,行号是不会在屏幕上显示出来的)

外部命令 :!+shell命令  如:!rm filename
可视化

ctrl+v   可视化
shift+v 复制多行
shift+i  注释多行

配置

Vim的功能特性要比vi多得多,但大部分功能都没有缺省激活。为了启动更多的功能,您得创建一个vimrc文件。

  1. 开始编辑vimrc文件,这取决于您所使用的操作系统∶

     :edit ~/.vimrc        这是Unix系统所使用的命令
     :edit $VIM/_vimrc        这是Windows系统所使用的命令

  2. 接着导入vimrc范例文件∶

     :read $VIMRUNTIME/vimrc_example.vim

  3. 保存文件,命令为∶

     :write

  在下次您启动vim的时候,编辑器就会有了语法高亮的功能。您可以继续把您喜
  欢的其它功能设置添加到这个vimrc文件中。

http://praxis.scholarslab.org/tutorials/bash/

 

gdb


调试:

g++ first.cpp -g

gdb a.out

只生成.o文件:

g++ first.cpp -c

 

gdb bin/svm_train_tool
set args train_demo.libsvm train_model

 

 

info func 显示所有的函数名称。
info local 显示当函数中的局部变量信息。
info prog 显示被调试程序的执行状态。
info var 显示所有的全局和静态变量名称。


重新编译需要make clean再make

GDB常用命令介绍与实例演示
b(reak)/r(un)/c(ontinue)/n(ext)/s(tep)/p(rint)

break test.c:34 if (x & y) == 1
break myfunc if i % (j + 3) != 0
break 44 if strlen(mystring) == 0

until
无参的until命令恢复程序的运行,并执行完循环的其余部分
until官方定义:执行程序,知道到达当前循环体外的下一行源代码
实际上,由于高级语言到机器语言在顺序上并不一定完全对应,until在到达内存地址比当前高的机器指令后就会停止
用法:
until [filename:]line_number
until [filename:]function
以上两种用法在到达指定的行号或者函数后即停止。


step:程序执行到断点时中断执行,可以用s指令进行单步执行进某一函数,如果已经进入了某函数,而想退出该函数返回到它的调用函数中,可使用命令finish
watch: 使你能监视一个变量的值而不管它何时改变, 当表达式的值被改变时GDB就使程序停止,还有rwatch是使程序暂停
clear:使用clear命令你可以删除指定位置的断点,如:clear FUNCTION, clear LINENUM,也可以使用delete命令通过断点号来指定要删去的断点或观察点,如果没有指定参数则删去程序中所有的断点
make: 使你能不退出gdb就可以重新产生可执行文件

kill: 终止正在调试的程序
shell:使你能不退出gdb就可以执行shell命令
info args ------------------>显示函数的参数名及值
(gdb) info b                 ----------------------->查看断点的设置信息

 

查看CORE信息与实例
bt:打印当前的函数调用栈的所有信息
f:n是栈中的层编号。比如:frame 1,表示栈的第二层
up:表示向栈的上面移动n层,可以不打n,表示向上移动一层
down:向栈的下面移动n层,可以不打n,表示向下移动一层
frame 或 f :会打印出这些信息,栈的层编号,当前的函数名,函数参数值,函数所在文件及行号,函数执行到的语句。
info f :这个命令会打印出更为详细的当前栈层的信息,只不过,大多数都是运行时的内内地址。比如:函数地址,调用函数的地址,被调用函数的地址,目前的函数是由什么样的程序语言写成的、函数参数地址及值、局部变量的地址等等。如:
info args:打印出当前函数的参数名及其值。
info locals:打印出当前函数中所有局部变量及其值。
info catch:打印出当前的函数中的异常处理信息
gdb example3 core.28543
(gdb) bt ------------------------>查看CORE信息
(gdb) f 1 ----------------------->切换到第一层frame
(gdb) info f -------------------->显示frame1的详细信息
(gdb) info args ------------------>显示函数的参数名及值

多线程程序夯住的检测与调试实例
info:用来显示你程序的状态,可通过help info查看较详细的帮助信息
attach:命令为attach?PID,这个命令把一个已经运行的进程(在gdb外启动)连接入gdb,以便调试。PID是进程号,当gdb接到attach命令后第一件事就是停止进程的运行
detach:与attach相对应,不多解释
thread:命令为thread?THREADNO,把线程号为THREADNO的线程设为当前线程。命令行参数THREADNO是gdb内定的线程号。
info threads:查看gdb内设置的线程号
thread apply THREADNO where:查看某个线程的属性
p pthread_mutex_t_name:可查看此锁目前的所属线程


借助gdb完成单元测试与实例
1通过set去设置变量的值,使被测程序在gdb内走期望内的分支
2通过what is 查看变量的属性
3通过set var设置变量的值

 

Git


https://help.github.com/

http://gitimmersion.com/

http://gitref.org/

http://cheat.errtheblog.com/s/git

http://git-scm.com/videos

https://help.github.com/articles/what-are-other-good-resources-for-using-git-or-github

 

Set Up Git git config --global user.name "Your Name Here"
git config --global user.email "your_email@example.com"
Create A Repo #mkdir ~/reponame
#cd ~/reponame
#git init
touch test
git add test
#add this change to the git’s staging area.
git status
git add .
#adding in all the changes to the files in the current directory and below
git commit –m 'first commit' 
#git remote add origin http://github.com/username/reponame.git
#git pull origin master
git push origin master
Fork A Repo git clone https://github.com/username/Yahoo_LDA.git
#When a repository is cloned, it has a default remote called origin that points to your fork on GitHub, not the original repository it was forked from.
git remote add upstream https://github.com/sudar/Yahoo_LDA.git
git fetch upstream
# Pulls in changes not present in your local repository, without modifying your files
git merge upstream/master
# Merges any changes fetched into your working files
Create Branches

git branch mybranch
# Creates a new branch called "mybranch"
git checkout mybranch
# Makes "mybranch" the active branch
git checkout -b mybranch <existing-branch> #equal to the previous 2 lines

Installing Git HTML help

sudo mkdir -p /usr/local/git/share/doc
cd /usr/local/git/share/doc
sudo git clone git://git.kernel.org/pub/scm/git/git-htmldocs.git git-doc
git help --web commit
#git config --global help.format web

cd /usr/local/git/share/doc/git-doc
sudo git pull
#Updating is a simple matter of pulling:

History

git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short

--pretty="..." defines the format of the output.
%h is the abbreviated hash of the commit
%s is the comment
%d are any decorations on that commit (e.g. branch heads or tags)
%ad is the author date
%an is the author name
--graph informs git to display the commit tree in an ASCII graph layout
--date=short keeps the date format nice and short

Aliases Add the following to the .gitconfig file in your $HOME directory.

[alias]
  co = checkout
  ci = commit
  st = status
  br = branch
  hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
  type = cat-file -t
  dump = cat-file -p

Getting Old Versions

git checkout <hash>
git checkout master
#Return the latest version in the master branch

 

git tag v1
# Now you can refer to the current version of the program as v1.

UNDOING LOCAL CHANGES (BEFORE STAGING/ADDING)

git checkout master
git checkout hello.rb
#Use the checkout command to checkout the repository’s version of the hello.rb file.

UNDOING STAGED CHANGES (BEFORE COMMITTING)

git reset HEAD hello.rb

# This clears the staging area of the change we just staged.The reset command (by default) doesn’t change the working directory.

posted on 2012-07-06 00:43  小唯THU  阅读(1169)  评论(0编辑  收藏  举报

导航