linux基本命令
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux基本命令相关的知识,希望对你有一定的参考价值。
建立新目录:mkdir
格式为 mkdir [目录名]
mkdir 可以建立多层目录
[[email protected] ~]$ mkdir C
[[email protected] C]$ mkdir -p c1/c2/c3
建立新文档 :touch
[[email protected] C]$ touch pri
[[email protected] C]$ ls
c1 pri
设置文件的访问权限 chmod
格式:用户符号+(-)权限 文件名
u:拥有者 g:组 o:其他
给u加权限:
[[email protected] C]$ chmod u+r+w pri
-rw-rw-r--. 1 caibei caibei 0 May 14 04:55 pri
给u减权限:
[[email protected] C]$ chmod u-r pri
[[email protected] C]$ ll
total 4
--w-rw-r--. 1 caibei caibei 0 May 14 04:55 pri
也可以利用八进制表示权限,0表示没有,1表示具有这个权限
给u,g,o加上所有的权限:
[[email protected] C]$ chmod 777 pri
[[email protected] C]$ ll
total 4
-rwxrwxrwx. 1 caibei caibei 0 May 14 04:55 pri
改变文件拥有者 chown
注:只有root才有权限使用
将pri文件改成caibei拥有:
[[email protected] C]$ chown root pri
chown: changing ownership of `pri‘: Operation not permitted
[[email protected] C]$ su
Password:
[[email protected] C]# chown caibei pri
[[email protected] C]# ll
total 4
drwxrwxr-x. 3 caibei caibei 4096 May 14 04:44 c1
-rwxrwxrwx. 1 caibei caibei 0 May 14 04:55 pri
改变文件的所属组 chgrp
[[email protected] C]# chgrp caibei pri
[[email protected] C]# ll
total 4
drwxrwxr-x. 3 caibei caibei 4096 May 14 04:44 c1
-rwxrwxrwx. 1 caibei caibei 0 May 14 04:55 pri
查看或者修改掩码 umask
新建文件夹默认权限:0666-掩码
新建目录默认权限:0777-掩码
[[email protected] C]# umask 0422
[[email protected] C]# ll
total 4
drwxrwxr-x. 3 caibei caibei 4096 May 14 04:44 c1
-rwxrwxrwx. 1 caibei caibei 0 May 14 04:55 pri
-rw-r--r--. 1 caibei root 0 May 14 05:32 pro
[[email protected] C]# umask
0422
文件和目录的显示 ls
ls -a 显示全部文件名包括以.开头隐藏的文件名
ls -i 列出文件inode号码
ls -F 在每个文件名后附上一个字符说明文件类型,‘*‘可执行普通文件,‘/‘目录,‘@’链接
[[email protected] C]# ls -a
. .. c1 pri pro
[[email protected] C]# ls -i
787323 c1 787332 pri 787304 pro
[[email protected] C]# ls -F
c1/ pri* pro
变换目录 cd
格式;cd 文件名
cd.. 返回上级目录
cd ~ 返回自己家
cd - 返回刚刚的目录
[[email protected] C]# cd
[[email protected] ~]# cd..
bash: cd..: command not found
[[email protected] ~]# cd ~
时间 date
格式: date +%Y_%m_%d_%H:%M:%S
[[email protected] ~]# date
Sat May 14 05:59:19 PDT 2016
[[email protected] ~]# date +%Y_%m_%d_%H:%M:%S
2016_05_14_06:00:19
时间戳 :从开始到现在总秒数
[[email protected] ~]# date +%s
1463231013
[[email protected] ~]# date +%Y:%m:%d -d @1463231013
2016:05:14
删除空目录 rmdir
-p 连上层目录一起删除
[[email protected] ~]# mkdir p
[[email protected] ~]# rmdir p
删除文件 rm
rm -r 删除目录及以下所有文件
rm -i 删除时会询问
rm -f 强制删除
[[email protected] ~]# rm -i c
rm: remove regular empty file `c‘?
复制文件或者目录 cp
格式; cp 源文件 目录
cp 源文件 源文件
[[email protected] ~]# cp c c1
[[email protected] ~]# touch b
[[email protected] ~]# cp c b
cp: overwrite `b‘?
返回文件名和目录名 dirname basename
格式:basename 文件名
[[email protected] ~]# basename c
c
[[email protected] ~]# dirname c
.
移动文件或目录或者更名 mv
mv -f强制移动
mv -i会询问是否移动
[[email protected] ~]# mv c c2
16.显示文件内容 more
[[email protected] ~]# count=0; while [ $count -lt 100 ];do echo "$count">>file;
> let count++; done
17.查看文件 less
格式:less 文件名
[[email protected] ~]# less file
18.日历 cal
[[email protected] ~]# cal
May 2016
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
19.查看文件头几行 head
[[email protected] ~]# head file
0
1
2
3
4
5
6
7
8
9
20.查看文件类型 file
[[email protected] ~]# file file
file: ASCII text
21.查看文件位置 which
whereis:
[[email protected] ~]# which file
/usr/bin/file
[[email protected] ~]# whereis file
file: /usr/bin/file /usr/share/file /usr/share/man/man1/file.1.gz /usr/share/man/man1p/file.1p.gz
22.find 寻找特定文件路径
find . -name 文件名
.表示当前目录 /表示根目录
find / -mtime n 按n天以前修改查找文件
find / -atime n 按访问文件时间查找文件
find / -ctime n 按文件创建时间查找
[[email protected] ~]# find . -name file
./file
[[email protected] ~]# find / -name file
/root/file
/usr/share/file
/usr/bin/file
/usr/src/kernels/2.6.32-431.el6.i686/include/config/pnfs/file
/usr/src/kernels/2.6.32-431.el6.i686/include/config/security/file
/usr/src/kernels/2.6.32-431.el6.i686/include/config/file
/selinux/class/file
/selinux/initial_contexts/file
/file
find -print:将匹配的文件输出
[[email protected] ~]$ find -print
.
./c
./c/3
./c/.file
./c/2
./c/1
./c/b
./c/pei
./c/pri
./test
find -user username:按username查找
[[email protected] ~]$ find -user caibei
.
./c
./test
./.pulse
find -nouser :按没有username查找
[[email protected] ~]$ find -nouser caibei
find: paths must precede expression: caibei
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
find -newer f1 !f2 查找比f1新比f2旧的文件
[[email protected] ~]$ find -newer test.c !c
find -newer test.c chown root pri
find: paths must precede expression: chown
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
find -type b 按照文件的类型查找
[[email protected] ~]$ find -type d
.
./c
./c/pei
./.pulse
./Videos
./.gnote
./.gnote/addins
./.gnome2
./.gnome2/keyrings
./.gnome2/gedit
./.gnome2/panel2.d
./.gnome2/panel2.d/default
./.gnome2/panel2.d/default/launchers
./.gnome2/nautilus-scripts
./.dbus
./.dbus/session-bus
./Templates
./Music
./.config
./.config/gtk-2.0
./.config/gnome-session
./.config/gnome-session/saved-session
./.config/gnome-disk-utility
./.config/gnome-disk-utility/ata-smart-ignore
./.gstreamer-0.10
./.mozilla
./.mozilla/plugins
./.mozilla/extensions
./Pictures
./Documents
./.local
./.local/share
./.local/share/applications
./.local/share/gvfs-metadata
./.local/share/Trash
./.local/share/Trash/expunged
./.local/share/Trash/info
./.local/share/Trash/files
./.gconf
./.gconf/desktop
./.gconf/desktop/gnome
./.gconf/desktop/gnome/accessibility
find -size n 查找文件是n块的文件
[[email protected] ~]$ find -size 2
./.pulse/f844858f676102d9b6631da400000039-card-database.tdb
./.xsession-errors
./.config/user-dirs.dirs
./.local/share/gvfs-metadata/home
./.viminfo
./.gconf/apps/gnome-terminal/profiles/Default/%gconf.xml
./.gconf/apps/panel/applets/window_list/prefs/%gconf.xml
./.bash_history
find _depth 使文件在查找子目录前先查找本目录
[[email protected] ~]$ find -depth
./c/3
./c/.file
./c/2
./c/1
./c/b
./c/pei
./c/pri
./c
./test
find -follow 遇到符号链接文件,就跟踪链接所指的文件
[[email protected] ~]$ find -follow
.
./c
./c/3
./c/.file
./c/2
./c/1
./c/b
./c/pei
./c/pri
./test
find -mount 查找文件的时候不跨越文件系统mount点
[[email protected] ~]$ find -mount
.
./c
./c/3
./c/.file
./c/2
./c/1
./c/b
./c/pei
./c/pri
./test
以上是关于linux基本命令的主要内容,如果未能解决你的问题,请参考以下文章