ls -R
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h> int do_ls(const char *dir)
{
char dir_name[];
DIR *dirp;
struct dirent *dp;
struct stat dir_stat; if ( != access(dir, F_OK) )
{
return ;
} if ( > stat(dir, &dir_stat) )
{
perror("get directory stat error");
return -;
} if ( S_ISDIR(dir_stat.st_mode) )
{
dirp = opendir(dir);
printf("%s:\n",dir);
int count = ;
while ( (dp=readdir(dirp)) != NULL )
{ ++count;
if ( ( == strcmp(".", dp->d_name)) || ( == strcmp("..", dp->d_name)) ) {
continue;
}
printf("%s\t",dp->d_name);
if( == count)
{
printf("\n");
count = ;
} }
printf("\n---\n");
rewinddir(dirp); while ( (dp=readdir(dirp)) != NULL )
{
if ( ( == strcmp(".", dp->d_name)) || ( == strcmp("..", dp->d_name)) ) {
continue;
} char buf[] = {};
sprintf(buf,"%s/%s",dir,dp->d_name);
do_ls(buf); }
} }
int main()
{
do_ls(".");
}

rm -r
1 #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <libgen.h>
#include <string.h>
void rmr(char* path)
{
DIR* dir = opendir(path);
if(dir == NULL)
perror("opendir"),exit(-);
struct dirent* ent;
char buf[];
while((ent=readdir(dir)))
{
if(ent->d_type == )
{
if(strcmp(ent->d_name,".")==||strcmp(ent->d_name,"..")==)
continue;
sprintf(buf,"%s/%s",path,ent->d_name); rmr(buf); }
if(ent->d_type == )
{
sprintf(buf,"%s/%s",path,ent->d_name);
if(remove(buf)!=) perror("remove"),exit(-);
}
}
if(rmdir(path)!=) perror("rmdir"),exit(-);
}
int main(int argc,char* argv[])
{
if(argc != )
{
printf("Usage:%s directory name",basename(argv[]));
exit(-);
} rmr(argv[]);
printf("rm -r %s success.\n",argv[]);
return ;
}
 ls -l
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h> void show_file_info(char* filename, struct stat* info_p)
{
char* uid_to_name(), *ctime(), *gid_to_name(), *filemode();
void mode_to_letters();
char modestr[]; mode_to_letters(info_p->st_mode, modestr); printf("%s", modestr);
printf(" %4d", (int) info_p->st_nlink);
printf(" %-8s", uid_to_name(info_p->st_uid));
printf(" %-8s", gid_to_name(info_p->st_gid));
printf(" %8ld", (long) info_p->st_size);
printf(" %.12s", + ctime(&info_p->st_mtime));
struct passwd *curr;
curr = getpwuid(getuid()); if(curr->pw_gid == info_p->st_gid && info_p->st_mode & S_IXGRP )
{
printf("\033[0;32m");
} if(curr->pw_gid == info_p->st_gid && curr->pw_uid == info_p->st_uid && info_p->st_mode & S_IXUSR )
{
printf("\033[0;32m");
} if(info_p->st_mode & S_IXOTH)
{
printf("\033[0;32m");
} printf(" %s\n",filename);
printf("\033[0m");
} void mode_to_letters(int mode, char str[])
{
strcpy(str,"----------"); if (S_ISDIR(mode))
{
str[] = 'd';
} if (S_ISCHR(mode))
{
str[] = 'c';
} if (S_ISBLK(mode))
{
str[] = 'b';
} if ((mode & S_IRUSR))
{
str[] = 'r';
} if ((mode & S_IWUSR))
{
str[] = 'w';
} if ((mode & S_IXUSR))
{
str[] = 'x';
} if ((mode & S_IRGRP))
{
str[] = 'r';
} if ((mode & S_IWGRP))
{
str[] = 'w';
} if ((mode & S_IXGRP))
{
str[] = 'x';
} if ((mode & S_IROTH))
{
str[] = 'r';
} if ((mode & S_IWOTH))
{
str[] = 'w';
} if ((mode & S_IXOTH))
{
str[] = 'x';
}
} char* uid_to_name(uid_t uid)
{
struct passwd* getpwuid(),* pw_ptr;
static char numstr[]; if((pw_ptr = getpwuid(uid)) == NULL)
{
sprintf(numstr,"%d",uid); return numstr;
}
else
{
return pw_ptr->pw_name;
}
} char* gid_to_name(gid_t gid)
{
struct group* getgrgid(),* grp_ptr;
static char numstr[]; if(( grp_ptr = getgrgid(gid)) == NULL)
{
sprintf(numstr,"%d",gid);
return numstr;
}
else
{
return grp_ptr->gr_name;
}
}
void do_ls(char dirname[])
{
DIR* dir_ptr;
struct dirent* direntp; if ((dir_ptr = opendir(dirname)) == NULL)
{
fprintf(stderr, "ls2: cannot open %s \n", dirname);
}
else
{
while ((direntp = readdir(dir_ptr)) != NULL)
{
dostat(direntp->d_name);
} close(dir_ptr);
}
} void dostat(char* filename)
{
struct stat info; if (stat(filename, &info) == -)
{
perror(filename);
}
else
{
show_file_info(filename, &info);
}
} int main(int ac,char* av[])
{
if(ac == )
{
do_ls(".");
}
else
{
while(--ac)
{
printf("%s: \n",++*av);
do_ls(*av);
}
}
}

ls 操作命令 -l/-R和rm -r dir 功能实现的更多相关文章

  1. linux下rm -r误删NTFS文件恢复方法

    一时疏忽,手一抖,把整个挂载的F盘删了一半!顿时傻眼!! 被删的F盘是Windows下NTFS分区,在Ubuntu12.04中挂载了F盘,使用rm命令时粗心大意,误删了一半的数据. 血的教训告诉我们, ...

  2. 删除GitHub或者GitLab 上的文件夹,git rm -r --ceched 文件夹名 ,提交commit,git push

    方法一 这里以删除 .setting 文件夹为案例 git rm -r --cached .setting #--cached不会把本地的.setting删除 git commit -m 'delet ...

  3. T100——程序从标准签出客制后注意r.c和r.l

    标准签出客制后,建议到对应4gl目录,客制目录 r.c afap280_01 r.l afap280_01 ALL 常用Shell操作命令: r.c:编译程序,需在4gl路径之下执行,产生的42m会自 ...

  4. git rm–r folder fatal:pathspec "" did not match any files

    问题描述: 某年某月某日,在查看git库的时候,发现文件的分布和文件夹的名字是极其不合理的,所以移动和重命名了某些文件. 在删除(git rm –r folder)一个空文件夹的时候,出现错误:fat ...

  5. ViewGroup.layout(int l, int t, int r, int b)四个输入参数的含义

    ViewGroup.layout(int l, int t, int r, int b)这个方法是确定View的大小和位置的,然后将其绘制出来,里面的四个参数分别是View的四个点的坐标,他的坐标不是 ...

  6. git rm -r --cache命令 及 git .gitignore 文件

    git 的  .gitignore 文件的作用是在代码提交时自动忽略一个文件.不将其纳入版本控制系统. 比如.一般我们会忽略IDE自动生成的配置文件等. 如果一个你要忽略的文件已经纳入到了git ,也 ...

  7. git rm -r --cached 去掉已经托管在git上的文件

    1.gitignore文件 在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改 .gitignore 文件的方法.这个文件每一行保存了一个匹配的规则例如: # 此为注释 – 将被 ...

  8. S​Q​L​_​S​e​r​v​e​r​_​2​0​0​8​定​期​自​动​备​份​详​细​图​解

    S​Q​L​_​S​e​r​v​e​r​_​2​0​0​8​定​期​自​动​备​份​详​细​图​解 设置自动数据库的定期备份计划. http://wenku.baidu.com/link?url=Tu ...

  9. git rm -r --cached解决已提交的文件在.gitignore中加入忽略后无效的问题。

    有时候,发现有不该提交的文件已经提交后,仅仅在.gitignore中加入忽略是不行的.这个时候需要执行: git rm -r --cached 文件/文件夹名字 去掉已经托管的文件,然后重新提交: g ...

随机推荐

  1. 安装php readline扩展报错 Please reinstall libedit

    现象:configure: error: Please reinstall libedit – I cannot find readline.h解决办法:安装 Editline Library (li ...

  2. Tomcat服务器安装配置

    1.到http://tomcat.apache.org/官网下载Tomcat的zip版本,这样直接解压就行了,不用安装.我下载是Tomcat6.0版本的zip文件,解压在D:\Java\apache- ...

  3. orcale 之游标的属性

    无论是显式游标还是隐式游标都会有四个属性分别是:%ISOPEN.%FOUND.%NOTFOUND.%ROWCOUNT 下面我们分别对着几种属性进行分析. %FOUND 该属性表示当前游标是否指向有效的 ...

  4. python2和python3中列表推导式的变量泄露问题

    Python 2.x 中,在列表推导中 for 关键词之后的赋值操作可能会影响列表推导上下文中的同名变量.像下面这个 Python 2.7 控制台对话: Python 2.7.15 (default, ...

  5. C#(winform)设置窗口置顶

    只要设置窗体的TopMost属性即可: registerForm.TopMost = true;

  6. CyclicBarrier正确的使用方法和错误的使用方法

    CyclicBarrier是java推出的一个并发编程工具,它用在多个线程之间协同工作.线程约定到达某个点,到达这个点之后的线程都停下来,直到最后一个线程也到达了这个点之后,所有的线程才会得到释放.常 ...

  7. spring 线程异步执行

    多线程并发处理起来通常比较麻烦,如果你使用spring容器来管理业务bean,事情就好办了多了.spring封装了Java的多线程的实现,你只需要关注于并发事物的流程以及一些并发负载量等特性,具体来说 ...

  8. JS Date函数在safari中的问题

    问题描述:在做Web的时候,在PC上用Chrome调试成功,但是在safari一测就出现了问题.经过debug发现是日期相关出现问题.查阅一些资料后发现,safari中对于JavaScript的Dat ...

  9. 最近在研究asp.net mvc

    看了很多大牛写的博客,依然对mvc云里雾里. 有一点是毋庸置疑的C应该是一座桥梁建立在model和view之间. 在ASP.NET MVC中,控制器通常是继承System.Web.Mvc.Contro ...

  10. MySQL---1、介绍

    一.MySQL简介 1.MySQL简介 MySQL是一个轻量级关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司.目前MySQL被广泛地应用在Internet上的中小型网站 ...