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. JavaScript数据结构-14.集合

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. C#中复制数组

    string array = new string[]{"abc", "bcd", "efg"}; string bArray = new ...

  3. java外观模式(Facade)

    1.外观模式(Facade [fə'sɑd] n. 正面:表面:外观) 外观模式:可以理解为 中介模式(没错,就是在用户与系统之间,增加了一个类,而这个类就是外观类,所以这个模式就叫外观模式) 如下图 ...

  4. No result defined for action and result input

    今天在编程的时候,我遇到了No result defined for action and result input的错误,这个错误想必大家都有遇到过吧,我今天发了很长时间弄这个错误,我以为我的Act ...

  5. Weblogic Maven

    从weblogic 10.3.4开始支持maven deploy部署   步骤如下: 1.构建weblogic-maven-plugin jar 在D:\oracle\Middleware\wlser ...

  6. Field [_id] is a metadata field and cannot be added inside a document. Use the index API request parameters--转

    https://github.com/elasticquent/Elasticquent/issues/53 the data is coming from a Mongo DB instance. ...

  7. 深入redis内部--字典实现

    redis的字典定义和实现在dict.h和dict.c文件中. 1.字典结构 typedef struct dict { dictType *type; //定义了字典需要的函数 void *priv ...

  8. JavaScript的柯里化函数

    柯里化,或者说部分应用,是一种函数式编程的技术,对于熟悉以传统方式编写 JavaScript 代码的人来说可能会很费解.但如果使用得当,它可以使你的 JavaScript 函数更具可读性. 更具可读性 ...

  9. Modbus Com SerialPort

    项目中用到的工具,串口modbus协议读写数据. public class ModbusHelper { private readonly SerialPort _serialPort; privat ...

  10. 屏蔽F1~F12的快捷键的js函数

    function window.onhelp(){return false} //屏蔽F1帮助 /*keyCod从113-123分别禁用了F2-F12键,13禁用了回车键(Enter),8禁用了退格键 ...