加载中...
avatar

linux常用命令

常用Linux命令,诸如cat、find、tr、xargs等。

cat:
用cat拼接文件

cat file1 file2 file3

压缩空白行

1
cat -s file

显示文件内的制表符
cat file
输出行号
cat -n file

find:
找出当前目录及其子目录的所有文件和文件夹
find base_path
find . -print
根据文件名和正则表达式找文件
find / -name filename
find filepath -name “.txt” -print
find /home/users -path “
.txt” -print
否定参数

find . ! -name "*.txt" -print

设置查找目录深度
find . -maxdepth 1 -type f -print
find . -mindepth 2 -type f -print
根据文件类型搜索
find . -type d -print #查找目录
find . -type f -print #查找文件
find . -type l -print #查找符号链接
find . -type c -print #查找字符设备
find . -type b -print #查找块设备
find . -type s -print #查找套接字
find . -type p -print #查找Fifo
根据时间进行搜索
find . -type f -atime -7 -print#查找7天内被访问过的所有文件
find . -type f -mtime -7 -print#查找7天内被修改过的所有文件
find . -type f -ctime -7 -print#查找7天内变化时间的所有文件
同理:
-amin访问时间
-mmin修改时间
-cmin变化时间
根据文件大小进行搜索
find . -type f -size +2k#查找大于2k的文件
find . -type f -size -2k#查找小于2k的文件
find . -type f -size 2k#查找等于2k的文件
删除匹配文件
find . -type f -name “.swp” -delete
根据文件权限和所有权查找
find . -type f -perm 655 -print#找出权限为655的文件
find / -type f -user mysql -print#找出所有mysql的文件
find . -type f -name “
.php” ! -perm 644 -print#找出所有执行权限不是644的php文件
结合find执行命令和动作
find . -type f -user root -exec chown mysql {};#将root的文件权限交给mysql
find . -type f -name “.c” -exec cat {} ;>all_c_files.txt #将所有c文件拼接起来保存到all_c_files.txt中
find . -type f -mtime +10 -name “
.txt” -exec cp {} OLD ;#将10天前的文件复制到OLD中
-exec不能接多个命令,只能跟一个命令,但是我们可以把命令写入shell脚本中,然后-exec执行这个脚本
-exec ./commands.sh {} ;
让find跳过指定目录
find devel/source_path ( -name “.git” -prune ) -o ( -type f -print )#打印不包括在.git目录中的所有文件的名称和路径

xargs:
她擅长将标准输入数据转换为命令行参数。能够将stdin数据转换为特定命令行参数。xargs也可以将单行或者多行文本输入转换成其他格式。格式:
command |xargs
将多行输入转换为单行输出
cat example.sh |xargs
将单行输入转换为多行输出
cat example.sh |xargs -n 3#将example分三行显示
用自定义分隔符分割参数
echo “splitXsplitXsplitXsplit” |xargs -d x
echo “splitXsplitXsplitXsplit” |xargs -d x -n 2#将分割后的数据分两行输出
结合find命令
find . -type f -name “.txt” -print0 | xargs -0 rm -f#删除所有txt文件,xargs -0将\0作为输入定界符
find source_code_dir_path -type f -name “
.c” -print0 | xargs -0 wc -l#统计c程序文件中代码的总行数

tr:
用tr进行转换
tr [option] set1 set2
echo “HELLO WHO IS THIS” | tr ‘A-Z’ ‘a-z’#将大写替换为小写

文章作者: 蕾米亚
文章链接: http://omimo.ga/2015/fe4ef317.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 彭彭和丁满
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论