1 默认情况下  实际用户和有效用户是一样的

实际用户:执行用户
  有效用户:权限用户


getuid()  实际用户


geteuid() 有效用户

chmod u+s 之后 ,其他人执行文件时,实际用户和有效用户会不一样

2 目录相关函数

int chdir(const char *path);改变当前目录


int mkdir(const char *pathname, mode_t mode); 创建目录


int rmdir(const char *pathname); 删除目录


 int unlink(const char *pathname); 删除文件


mode_t umask(mode_t mask); 设置文件权限屏蔽位


stat  fstat lstat文件目录状态

3 目录的遍历

3.1 方法一 opendir + readdir


DIR *opendir(const char *name);


struct dirent *readdir(DIR *dirp);


int closedir(DIR *dirp);

struct dirent {

               ino_t          d_ino;       /* inode number */

               off_t          d_off;       /* offset to the next dirent */

               unsigned short d_reclen;    /* length of this record */

               unsigned char  d_type;      /* type of file; not supported


by all file system types */

               char      
 d_name[256]; /* filename */

           };

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>//exit() int main()
{
DIR *d = opendir("/home/zhao");
if(d == 0)
{
perror("opendir:%m\n");
exit(1);
} struct dirent * de;
while(de=readdir(d))
{
printf("%s \t%d\n",de->d_name,de->d_type);
}
//d_type 4 表示目录 8表示文件 closedir(d); }

3.2   方法2 scandir


   int scandir(const char *dirp, //目录名 


struct dirent ***namelist, //返回目录列表


int (*filter)(const struct dirent *), //回调函数 过滤目录 NULL表不过滤


int (*compar)(const struct dirent **, const struct dirent **)); //对查询结果排序 NULL表不排序

过滤规则 filter返回0 则不过滤掉 非0则显示


排序规则 compar  >0 排在前面 <0排在后面


已有的排序


int alphasort(const void *a, const void *b);


int versionsort(const void *a, const void *b);

返回值: >=0 目录个数


-1 目录查找失败

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>//exit() int filter(const struct dirent *);
int sort(const struct dirent**,const struct dirent **); int main()
{
struct dirent **d;
//int r = scandir("/home/zhao",&d,filter,alphasort);
int r = scandir("/home/zhao",&d,filter,sort); //与alphasort你序
printf("子目录个数为%d\n",r);
while(*d != 0)
{
printf("%s\n",(*d)->d_name);
d++;
} return 0;
} //过滤掉名字以.开头的文件夹
int filter(const struct dirent* d)
{
if(strncmp(d->d_name,".",1) == 0)
{
return 0;
} return 1;
} int sort(const struct dirent**a,const struct dirent **b)
{
return -alphasort(a,b);
}

LINUX编程学习笔记(十三) 遍历目录的两种方法的更多相关文章

  1. linux系列之: 你知道查看文件空间的两种方法吗?

    目录 简介 du命令 df命令 总结 简介 linux系统中查看文件空间大小应该是一个非常常见的命令了,今天给大家介绍linux系统中查看文件空间的两种方法和在使用中可能会遇到的奇怪问题. 为什么会有 ...

  2. python学习--python 连接SQLServer数据库(两种方法)

    1. python 学习.安装教程参照: http://www.runoob.com/python/python-tutorial.html 2. 集成开发环境 JetBrains PyCharm C ...

  3. Linux 编程学习笔记----动笔makefile档

    Befroe Beginning. 在设置暑假的plan ,关于Linux的书籍如今在看的是ALP和Linux高级程序设计(杨宗德)第三版.在计划中的是Linux高级环境编程. 如今開始关于Linux ...

  4. Linux编程学习笔记(二)

    续上个章节,这个章节主要是Linux的远程登录系统操作笔记 一. Linux一般作为服务器使用,但是服务器都是在机房的,所以不可能经常跑到机房去操作系统,所以使用远程登录系统,在Linux的系统一般使 ...

  5. Linux 编程学习笔记----ANSI C 文件I/O管理

    转载请注明出处:http://blog.csdn.net/suool/article/details/38129201 问题引入 文件的种类 依据数据存储的方式不同,能够将文件分为文本文件和二进制文件 ...

  6. LINUX编程学习笔记(十四) 创建进程与 父子进程内存空间

    1什么是进程:进程是一个执行中的程序 执行的程序: 代码->资源->CPU 进程有很多数据维护:进程状态/进程属性 所有进程属性采用的一个树形结构体维护 ps  -a//所有进程 ps - ...

  7. Linux 编程学习笔记----命令行参数处理

    转载请注明出处.http://blog.csdn.net/suool/article/details/38089001 问题引入----命令行參数及解析 在使用linux时,与windows最大的不同 ...

  8. 链式前向星存树图和遍历它的两种方法【dfs、bfs】

    目录 一.链式前向星存图 二.两种遍历方法 一.链式前向星存图:(n个点,n-1条边) 链式前向星把上面的树图存下来,输入: 9 ///代表要存进去n个点 1 2 ///下面是n-1条边,每条边连接两 ...

  9. C51编程中对单片机绝对地址访问的两种方法

    在进行8051单片机应用系统程序设计时,编程都往往少不了要直接操作系统的各个存储器地址空间.C51程序经过编译之后产生的目标代码具有浮动地址,其绝对地址必须经过BL51连接定位后才能确定.为了能够在C ...

随机推荐

  1. java 随机数的生成

    生成10个不小于100000的6位数 public static void main(String[] args) { Random random = new Random(); for (int i ...

  2. Android核心基础(十一)

    1.Android的状态栏通知(Notification) 通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: //获取通知管理器 NotificationManager mNotificat ...

  3. State Design Pattern 状态设计模式

    设置好内部状态,然后依据不同的函数作为行为模式,进行状态转换. 有点像Finite Automata算法,两者的思想是一样的. 会Finite Automata,那么这个设计模式就非常easy了. # ...

  4. Hadoop MapReduce编程的一些个人理解

    首先要实现mapreduce就要重写两个函数,一个是map 还有一个是reduce map(key ,value) map函数有两个參数,一个是key,一个是value 假设你的输入类型是TextIn ...

  5. Streaming编程实例(c,c++,python等)

    1.概述 Hadoop Streaming是Hadoop提供的一个编程工具,它允许用户使用任何可执行文件或者脚本文件作为Mapper和Reducer,例如: 采用shell脚本语言中的一些命令作为ma ...

  6. VPS服务器下的centos网卡配置详解……

    自动激活网卡 安装了CENTOS 6.X后,每次启动了系统都需要手动激话网卡,以下方法可以在系统启动后自动激活网卡. cat /etc/sysconfig/network-scripts/ifcfg- ...

  7. 译文:前端性能的重要性 The Importance of Frontend Performance

    欢迎訪问我的主页.最新的文章我会首先公布在个人主页上: http://blog.guaidm.com/shocky/ 原书下载地址:http://pan.baidu.com/s/1pJocRwB 在我 ...

  8. eval 捕获dbi错误

    [root@dr-mysql01 ~]# cat t2.pl use DBI; my $dbUser='zabbix'; my $user="root"; my $passwd=& ...

  9. 使用perf生成Flame Graph(火焰图)

      具体的步骤参见这里: <flame graph:图形化perf call stack数据的小工具>   使用SystemTap脚本制作火焰图,内存较少时,分配存储采样的数组可能失败,需 ...

  10. Twenty Newsgroups Classification任务之二seq2sparse(2)

    接上篇,SequenceFileTokenizerMapper的输出文件在/home/mahout/mahout-work-mahout0/20news-vectors/tokenized-docum ...