第一天
1.什么是内建命令(内置命令)
所谓 Shell 内建命令,就是由 Bash 自身提供的命令,而不是文件系统中的某个可执行文件。
该命令并不是某个外部文件,只要在 Shell 中你就一定可以运行这个命令。
可以使用 type 来确定一个命令是否是内建命令:
[root@localhost ~]# type cd
cd is a Shell builtin
内建命令会比外部命令执行得更快,执行外部命令时不但会触发磁盘 I/O,还需要 fork 出一个单独的进程来执行,执行完成后再退出。而执行内建命令相当于调用当前 Shell 进程的一个函数
2.deb包是什么包
1.rmp包
RMP 是 LINUX 下的一种软件的可执行程序,你只要安装它就可以了。这种软件安装包通常是一个RPM包(Redhat Linux Packet Manager,就是Redhat的包管理器),后缀是.rpm。
RPM是Red Hat公司随Redhat Linux推出了一个软件包管理器,通过它能够更加轻松容易地实现软件的安装。
2.deb包
deb 是 Unix 系统(其实主要是 Linux )下的安装包,基于 tar 包,因此本身会记录文件的权限(读/写/可执行)以及所有者/用户组。由于 Unix 类系统对权限、所有者、组的严格要求,而 deb 格式安装包又经常会涉及到系统比较底层的操作,所以权限等的设置尤其重要。
deb 包本身有三部分组成:数据包,包含实际安装的程序数据,文件名为 data.tar.XXX;安装信息及控制脚本包,包含 deb 的安装说明,标识,脚本等,文件名为 control.tar.gz;最后一个是 deb 文件的一些二进制数据,包括文件头等信息,一般看不到,在某些软件中打开可以看到。
deb 本身可以使用不同的压缩方式。tar 格式并不是一种压缩格式,而是直接把分散的文件和目录集合在一起,并记录其权限等数据信息。之前提到过的 data.tar.XXX,这里 XXX 就是经过压缩后的后缀名。deb 默认使用的压缩格式为 gzip 格式,所以最常见的就是 data.tar.gz。常有的压缩格式还有 bzip2 和 lzma,其中 lzma 压缩率最高,但压缩需要的 CPU 资源和时间都比较长。
data.tar.gz包含的是实际安装的程序数据,而在安装过程中,该包里的数据会被直接解压到根目录(即 / ),因此在打包之前需要根据文件所在位置设置好相应的文件/目录树。
而 control.tar.gz 则包含了一个 deb 安装的时候所需要的控制信息。
一般有 5 个文件:control,用了记录软件标识,版本号,平台,依赖信息等数据;
preinst,在解包data.tar.gz 前运行的脚本;
postinst,在解包数据后运行的脚本;
prerm,卸载时,在删除文件之前运行的脚本;
postrm,在删除文件之后运行的脚本;在 Cydia 系统中,Cydia 的作者 Saurik 另外添加了一个脚本,extrainst_,作用与 postinst 类似。
3.源码安装
-
查看软件的动态链接库
使用ldd命令即可查看指定软件所使用的全部动态函数库。 ldd /usr/bin/passwd
-
linux源码安装步骤
Linux源码安装步骤
- 获取源码
将软件的源码下载至/usr/local/,并解压。 - 查看INSTALL与README文件
解压后查看INSTALL与README文件,这两个文件中详细介绍了本软件的安装方法和注意事项。 - 创建Makefile文件
执行configure命令,生成Makefile文件。 - 编译
执行make clean;make命令将源码编译成二进制文件。
PS:make clean命令用来清除上一次编译生成的目标文件。这个步骤可有可无,但为了确保编译的成功,还是加上为好。防止由于软件中含有残留的目标文件导致编译失败。 - 安装
执行make install命令将上一步编译好的二进制文件安装到指定的目录中去。
- 获取源码
4.bash和shell
shell是最开始unix的命令解释器,bash是后来衍生出来的一个版本
什么是Shell?
shell是你(用户)和Linux(或者更准确的说,是你和Linux内核)之间的接口程序。你在提示符下输入的每个命令都由shell先解释然后传给Linux内核。
shell 是一个命令语言解释器(command-language interpreter)。拥有自己内建的 shell 命令集。此外,shell也能被系统中其他有效的Linux 实用程序和应用程序(utilities and application programs)所调用。
不论何时你键入一个命令,它都被Linux shell所解释。一些命令,比如打印当前工作目录命令(pwd),是包含在Linux bash内部的(就象DOS的内部命令)。其他命令,比如拷贝命令(cp)和移动命令(rm),是存在于文件系统中某个目录下的单独的程序。而对用户来说,你不知道(或者可能不关心)一个命令是建立在shell内部还是一个单独的程序。
shell 首先检查命令是否是内部命令,不是的话再检查是否是一个应用程序,这里的应用程序可以是Linux本身的实用程序,比如ls 和 rm,也可以是购买的商业程序,比如 xv,或者是公用软件(public domain software),就象 ghostview。然后shell试着在搜索路径($PATH)里寻找这些应用程序。搜索路径是一个能找到可执行程序的目录列表。如果你键入的命令不是一个内部命令并且在路径里没有找到这个可执行文件,将会显示一条错误信息。而如果命令被成功的找到的话,shell的内部命令或应用程序将被分解为系统调用并传给Linux内核。
shell的另一个重要特性是它自身就是一个解释型的程序设计语言,shell 程序设计语言支持在高级语言里所能见到的绝大多数程序控制结构,比如循环,函数,变量和数组。shell 编程语言很易学,并且一旦掌握后它将成为你的得力工具。任何在提示符下能键入的命令也能放到一个可执行的shell程序里,这意味着用shell语言能简单地重复执行某一任务。
shell 如何启动?
shell在你成功地登录进入系统后启动,并始终作为你与系统内核的交互手段直至你退出系统。你系统上的每位用户都有一个缺省的shell。每个用户的缺省shell在系统里的passwd文件里被指定,该文件的路径是/etc/passwd。passwd文件里还包含有其他东西:每个人的用户ID号,一个口令加密后的拷贝和用户登录后立即执行的程序,(注:为了加强安全性,现在的系统一般都把加密的口令放在另一个文件--shadow中,而passwd中存放口令的部分以一个x字符代替)虽然没有严格规定这个程序必须是某个Linux shell,但大多数情况下都如此。
5.实用小技巧
-
查看一个文件的inode
stat temp.txt
-
输出系统信息
uname --help
- 查看磁盘信息
df -h
- 查看目录下所有文件大小
du -h
- 以各种格式查看文件内容
od -t (o/d/h) temp.txt
第二天
一.压缩和解压缩
1.gzip 和 bzip2
gzip 1.txt 2.txt ---> 生成后缀为gz的压缩包 只能压缩一个文件
bzip2 1.txt 2.txt ----> 生成后缀为bz2的压缩包 只能压缩一个文件
gunzip 解压缩的名字
bunzip2 解压缩的名字 不能压缩目录
2.tar
tar 是归档的意思
压缩 : -c 创建一个文件
-x 解压一个文件
-v 显示一个文件压缩和解压的过程
-f 指定压缩的名称
-z 对gz文件进行压缩
-j 对bz2文件进行压缩
压缩:
tar -zcvf xxx.tar.gz xxx
tar -jcvf xxx.tar.bz2 xxx
解压:
tar -xvf xxx.tar.gz/xxx.tar.bz2 -C 目录名
3.rar
以后缀名rar结尾的压缩包,需要自己安装rar工具
压缩: rar a xxx
解压缩: rar x xxx.rar xxx
4.zip
需要安装zip工具
压缩 zip -r xxx
解压 unzip xxx.zip -d xxx
.bz2和.gz的区别在于,前者比后者压缩率更高,后者比前者花费更少的时间。也就是说同一个文件,压缩后,.bz2文件比.gz文件更小,但是.bz2文件的小是以花费更多的时间为代价的。
二.进程号
1.ps
ps
-a:是有tty的进程
-x:是带有?的进程
-u:显示一个进程的全部信息,且有tty
-f:显示必要的信息
-e:?+tty进程
2.kill
杀死一个进程
kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
kill -SIGKILL PID
kill -9 PID
3.top
相当于windows下面的任务管理器
4.环境变量和本地变量
set 显示当前shell的变量(本地变量),包括当前用户的变量( 环境变量)
env 显示当前用户的变量 (环境变量)
export 显示当前导出成用户变量的shell变量 (环境变量)
三.网络相关的命令
1.查看网卡和ip
ifconfig
2.查看是否能上网
ping www.baidu.com
3.查看某个网址的服务器
nslookup www.baidu.com
四.用户管理
1.添加用户
sudo adduser 用户名
2.修改密码
sudo passwd 用户名
3.删除用户
sudo userdel 用户名
五.文件服务器的搭建
1.ftp
vsftpd
vsftpd 是“very secure FTP daemon”的缩写,安全性是它的一个最大的特点。vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux、BSD、Solaris、 HP-UNIX等系统上面,是一个完全免费的、开放源代码的ftp服务器软件,支持很多其他的 FTP 服务器所不支持的特征。比如:非常高的安全性需求、带宽限制、良好的可伸缩性、可创建虚拟用户、支持IPv6、速率高等。
lftpd 是升级版的ftp的客户端
查看安装在何处
whereis vsftpd
网址:https://blog.csdn.net/hanyuyang19940104/article/details/80421632
扩展:lftp 可下载文件和目录
2.nfs
Ubuntu 下搭建 NFS 服务
1、NFS 介绍
NFS 即网络文件系统(Network File-System),可以通过网络让不同机器、不同系统之间可以实现文件共享。通过 NFS,可以访问远程共享目录,就像访问本地磁盘一样。NFS 只是一种文件系统,本身并没有传输功能,是基于 RPC(远程过程调用)协议实现的,采用 C/S 架构。
2、安装 NFS 软件包
sudo apt-get install nfs-kernel-server # 安装 NFS服务器端
sudo apt-get install nfs-common # 安装 NFS客户端
3、添加 NFS 共享目录
sudo vim /etc/exports
若需要把 “/nfsroot” 目录设置为 NFS 共享目录,请在该文件末尾添加下面的一行:
/nfsroot *(rw,sync,no_root_squash) # * 表示允许任何网段 IP 的系统访问该 NFS 目录
新建“/nfsroot”目录,并为该目录设置最宽松的权限:
sudo mkdir /nfsroot
sudo chmod -R 777 /nfsroot
sudo chown ipual:ipual /nfsroot/ -R # ipual 为当前用户,-R 表示递归更改该目录下所有文件
4、启动 NFS 服务
sudo /etc/init.d/nfs-kernel-server start 或者
sudo /etc/init.d/nfs-kernel-server restart
在 NFS 服务已经启动的情况下,如果修改了 “/etc/exports” 文件,需要重启 NFS 服务,以刷新 NFS 的共享目录。
5、测试 NFS 服务器
sudo mount -t nfs 192.168.12.123:/nfsroot /mnt -o nolock
192.168.12.123 为主机 ip,/nfsroot 为主机共享目录,/mnt 为设备挂载目录,如果指令运行没有出错,则 NFS 挂载成功,在主机的 /mnt 目录下应该可以看到 /nfsroot 目录下的内容(可先在 nfsroot 目录下新建测试目录),如需卸载使用umount /mnt
6、开发板挂载 Ubuntu
① 首先确定 Ubuntu 可以上网,然后将开发板联网,使其与 Ubuntu 在同一网段下(方式有很多种这里给出参考:1.将开发板接在电脑所连的路由器或交换机上 2.将开发板连接电脑的网口,使用虚拟网卡,桥接)
② 确定开发板与Ubuntu之间可以 ping 通:
ping 192.168.12.123
③ 开发板端输入
sudo mount -t nfs 192.168.12.123:/nfsroot /mnt -o nolock
如果没有报错即为挂载成功。
注意:在 mount 与 umount(解除挂载)操作时,用户的当前路径不能是操作的目标路径。
3.ssh
SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定;SSH 为建立在应用层基础上的安全协议。SSH 是较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。
#SSH分为客户端 openssh-client 和服务器 openssh-server
网址:https://blog.csdn.net/li528405176/article/details/82810342
安装
sudo apt-get install openssh-server
scp : super copy
前提是 openssh-server安装成功
scp -r 目标用户名@目标主机IP地址:/目标文件的绝对路径 /保存在本机的绝对(相对)路径
第三天
一.man命令和echo命令
1.查看man帮助文档的首页
man man
2.查看一个命令是否有别名
alias
二.vim的相关操作
1.命令模式
进入编辑模式
i I
o O
a A
s S
行首0 行尾$
删除行首d0 删除行尾d$
首gg 尾G 多少行11G
查看man文档 3K
查找: /查找的内容 ?/查找的内容 按”#“定位到所在的行 N n
dd p P yy x X r R
<< 左移
>> 右移
2.底行模式
按 ESC ESC ---> 命令模式
:12,14s/查找的字符/替换的字符
:%s/查找的字符/替换的字符
:/查找的字符/替换的字符g
:!命令
视图切换
:wqall ctrl+w+w :vsp :sp
3.视图模式
v进入
按上下左右键选中
y p P d
三.动态库和静态库
1.动态库运行时路径的修改
1.直接将库复制到lib目录中
2.修改LD_LIBRARY_PATH;用一次有效 export
3.永久有效~/.bashrc 需重启终端
4.修改etc/ld.so.conf文件 将自己动态库的路径加进去
sudo ldconfig -v
ldconfig是一个动态链接库管理命令,其目的为了让动态链接库为系统所共享。
默认搜寻/lib和/usr/lib,以及配置文件/etc/ld.so.conf内所列的目录下的库文件。
搜索出可共享的动态链接库,库文件的格式为:lib***.so.**,进而创建出动态装入程序(ld.so)所需的连接和缓存文件。
缓存文件默认为/etc/ld.so.cache,该文件保存已排好序的动态链接库名字列表。
四.gdb
gcc *.c -o app -g
gdb app
开始 start 执行一步 r 全速运行 c 继续运行
设置断点 b/break 行号/文件名:函数名/文件名:行号
查看断点信息: info b
删除断点 :d/del/delete 断点号
查看源代码:l l 文件名:函数名/文件名:行号
打印变量信息: p i/j
追踪变量信息: display i
删除变量信息: undisplay 变量号
查看变量的变量号:info display
下一步: n s
查看变量类型: ptype
退出: finish
五.makefile
wildcard
patsubstr
只能用于命令 $< $^ 依赖 $@目标文件
目标:依赖
命令
最终目标一定要写在第一条,如果满足了依赖,直接生成了目标文件,后面的代码便不会执行
是通过比较目标文件的时间和依赖文件的时间来决定是否需要更新
:PHONY clean ;伪定义
六.系统函数
1.前言
2.关于一些头文件
asm的路径是 arch/xxx/include/asm/
asm-generic 的路径是 include/asm-generic/
代码中包含asm/中的头文件,如果某一个架构没有自己特殊代码的话,其中会使用通用版本的头文件,即包含 asm-generic/里的对应.h文件。
代码中不会直接包含 asm-generic/ 里的.h文件
拿arm来举例
#include <asm/gpio.h> 引用的头文件是 arch/arm/include/asm/gpio.h
#include <asm-generic/gpio.h> 引用的是头文件是 include/asm-generic/gpio.h
#include <linux/xxx.h> 内核的基本文件
#include <asm/xxx.h> 与cpu相关的
#include <arch/xxx.h> 与外设相关的
3.错误宏定义
/usr/include/errno.h
/usr/include/asm-generic/errno.h
/usr/include/asm-generic/errno-base.h
4.系统IO
1.open
NAME open, openat, creat - open and possibly create a file
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);
O_APPEND O_CREAT O_TRUNC
O_RDONLY, O_WRONLY, or O_RDWR
S_IRWXU 00700 user (file owner) has read, write, and execute permission
S_IRUSR 00400 user has read permission
S_IWUSR 00200 user has write permission
S_IXUSR 00100 user has execute permission
S_IRWXG 00070 group has read, write, and execute permission
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 others have read, write, and execute permission
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission
2.read
NAME read - read from a file descriptor
SYNOPSIS
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
3.write
NAME write - write to a file descriptor
SYNOPSIS
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
4.lseek
NAME lseek - reposition read/write file offset
SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
SEEK_SET The file offset is set to offset bytes.
SEEK_CUR The file offset is set to its current location plus offset bytes.
SEEK_END The file offset is set to the size of the file plus offset bytes.
文件的扩展
int ret = lseek(fd, 2000, SEEK_END);
write(fd,'a',1);
用途:比如文件下载的时候,需要提前分配文件空间
5.文件操作函数
1.stat
STAT(2) Linux Programmer's Manual STAT(2)
NAME stat, fstat, lstat, fstatat - get file status
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);
struct stat {
dev_t st_dev; //设备号
ino_t st_ino; //结点ID
mode_t st_mode; //文件类型和权限
nlink_t st_nlink; //硬链接数
uid_t st_uid; //用户ID
gid_t st_gid; //组ID
dev_t st_rdev; //设备号
off_t st_size; //文件大小
blksize_t st_blksize; //块的大小
blkcnt_t st_blocks; //块的个数
struct timespec st_atim; //最后一次访问的时间
struct timespec st_mtim; //最后一次改变的时间
struct timespec st_ctim; //最后一次改变的时间(属性)
#define st_atime st_atim.tv_sec /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};
switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
2.access
NAME access, faccessat - check user's permissions for a file
SYNOPSIS #include <unistd.h>
int access(const char *pathname, int mode);
R_OK, W_OK, and X_OK. F_OK
3.chmod
NAME chmod, fchmod, fchmodat - change permissions of a file
SYNOPSIS #include <sys/stat.h>
int chmod(const char *pathname, mode_t mode);
int fchmod(int fd, mode_t mode);
@return: On success, zero is returned.
On error, -1 is returned, and errno is set appropriately.
S_IRUSR (00400) read by owner
S_IWUSR (00200) write by owner
S_IXUSR (00100) execute/search by owner ("search" applies for direc‐
tories, and means that entries within the directory
can be accessed)
S_IRGRP (00040) read by group
S_IWGRP (00020) write by group
S_IXGRP (00010) execute/search by group
S_IROTH (00004) read by others
S_IWOTH (00002) write by others
S_IXOTH (00001) execute search by others
4.chown
NAME chown, fchown, lchown, fchownat - change ownership of a file
SYNOPSIS #include <unistd.h>
int chown(const char *pathname, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *pathname, uid_t owner, gid_t group);
On success, zero is returned.
On error, -1 is returned, and errno is set appropriately.
man 5 passwd
5.truncate
NAME truncate, ftruncate - truncate a file to a specified length
SYNOPSIS #include <unistd.h>
#include <sys/types.h>
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
On success, zero is returned.
On error, -1 is returned, and errno is set appropriately.
6.link
NAME link, linkat - make a new name for a file
SYNOPSIS #include <unistd.h>
int link(const char *oldpath, const char *newpath);
创建一个硬链接
On success, zero is returned.
On error, -1 is returned, and errno is set appropriately.
7.symlink
NAME symlink, symlinkat - make a new name for a file
SYNOPSIS #include <unistd.h>
int symlink(const char *target, const char *linkpath);
创建一个软链接
On success, zero is returned.
On error, -1 is returned, and errno is set appropriately.
8.readlink
//读一个软链接
NAME readlink, readlinkat - read value of a symbolic link
SYNOPSIS #include <unistd.h>
ssize_t readlink(const char *pathname, char *buf, size_t bufsize);
RETURN VALUE
On success, these calls return the number of bytes placed in buf. (If
the returned value equals bufsiz, then truncation may have occurred.)
On error, -1 is returned and errno is set to indicate the error.
9.unlink
//断链接;软硬皆可
NAME unlink, unlinkat - delete a name and possibly the file it refers to
SYNOPSIS #include <unistd.h>
int unlink(const char *pathname);
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
10.rename
NAME rename, renameat, renameat2 - change the name or location of a file
SYNOPSIS #include <stdio.h>
int rename(const char *oldpath, const char *newpath);
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
6.目录操作函数
1.chdir
NAME chdir, fchdir - change working directory
SYNOPSIS #include <unistd.h>
int chdir(const char *path);
int fchdir(int fd);
//改变当前程序运行时进程的路径
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
2.getcwd
NAME getcwd, getwd, get_current_dir_name - get current working directory
SYNOPSIS #include <unistd.h>
char *getcwd(char *buf, size_t size);
//得到当前程序运行时所处的路径
RETURN VALUE
On success, these functions return a pointer to a string containing the
pathname of the current working directory. In the case getcwd() and
getwd() this is the same value as buf.
On failure, these functions return NULL, and errno is set to indicate
the error. The contents of the array pointed to by buf are undefined
on error.
3.mkdir
NAME mkdir, mkdirat - create a directory
SYNOPSIS
#include <sys/stat.h>
#include <sys/types.h>
int mkdir(const char *pathname, mode_t mode);
the mode of the created directory is (mode & ~umask & 0777).
RETURN VALUE
mkdir() and mkdirat() return zero on success, or -1 if an error
occurred (in which case, errno is set appropriately).
4.rmdir
NAME rmdir - delete a directory
SYNOPSIS #include <unistd.h>
int rmdir(const char *pathname);
//删除一个空目录
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
5.opendir
NAME opendir, fdopendir - open a directory
SYNOPSIS #include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
DIR *fdopendir(int fd);
RETURN VALUE
The opendir() and fdopendir() functions return a pointer to the direc‐
tory stream. On error, NULL is returned, and errno is set appropri‐
ately.
6.readdir
NAME readdir - read a directory
SYNOPSIS #include <dirent.h>
struct dirent *readdir(DIR *dirp);
struct dirent {
ino_t d_ino; /* Inode number */inode结点
off_t d_off; /* Not an offset; see below */偏移量
unsigned short d_reclen; /* Length of this record */子目录长度
unsigned char d_type; /* Type of file; not supported
by all filesystem types */类型
char d_name[256]; /* Null-terminated filename */名字
};
DT_BLK This is a block device.
DT_CHR This is a character device.
DT_DIR This is a directory.
DT_FIFO This is a named pipe (FIFO).
DT_LNK This is a symbolic link.
DT_REG This is a regular file.
DT_SOCK This is a UNIX domain socket.
DT_UNKNOWN The file type could not be determined.
RETURN VALUE
On success, readdir() returns a pointer to a dirent structure. (This
structure may be statically allocated; do not attempt to free(3) it.)
If the end of the directory stream is reached, NULL is returned and
errno is not changed. If an error occurs, NULL is returned and errno
is set appropriately. To distinguish end of stream and from an error,
set errno to zero before calling readdir() and then check the value of
errno if NULL is returned.
7.closedir
NAME closedir - close a directory
SYNOPSIS #include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp);
RETURN VALUE
The closedir() function returns 0 on success. On error, -1 is
returned, and errno is set appropriately.
7.其它
1.dup
NAME dup, dup2, dup3 - duplicate a file descriptor
SYNOPSIS
#include <unistd.h>
int dup(int oldfd);
int dup2(int oldfd, int newfd);
The dup() system call creates a copy of the file descriptor oldfd,using the lowest-numbered unused file descriptor for the new descriptor.
RETURN VALUE
On success, these system calls return the new file descriptor. On
error, -1 is returned, and errno is set appropriately.
//如果newfd的文件是打开的,则先关闭,再拷贝
2.fcntl
NAME fcntl - manipulate file descriptor
SYNOPSIS #include <unistd.h>
#include <fcntl.h>
int fcntl(int fd, int cmd, ... /* arg */ );
F_GETFL (void)
F_SETFL (int)
O_NONBLOCK
//修改文件的权限