Linux tool

2016-06-16  本文已影响35人  abrocod

Reference


Bash Shell Shortcut


System Directory Structure

Some further explanation

Other important directories under user directory:


Common Shell Command


Simple Command Collection

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

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

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


上一篇 下一篇

猜你喜欢

热点阅读