在所有bin目录下找到2个字母命令 [英] Find 2 letter commands under all bin directory

查看:65
本文介绍了在所有bin目录下找到2个字母命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要该命令在所有以``bin''结尾的部分中找到所有2个字母的命令,例如 rm,ls,cp,mv .

我已经使用命令找到了这些目录
ls -alR/2>/dev/null | grep''^ d''| gerp''bin $''

请帮忙.

Hi,

I need the command to find all the 2 letter commands like rm, ls, cp, mv in all the diectories that ends with ''bin''.

I have found those directories using the command
ls -alR / 2>/dev/null | grep ''^d'' | gerp ''bin$''

Please help.

推荐答案

''

请帮助.
''

Please help.


我们毕竟是开发人员(错误处理留给读者):

We are developers, after all (error handling left to the reader):

/* finds recursively files having 2-letters name in directories whoose name ends with 'bin'
  usage:
    rd [<DIRNAME>]

  on <DIRNAME> missing, current directory is used
*/

#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>

#define BUFSIZE 0x400

int has_slash(const char * buf, size_t len);

int rd(const char * nam, int check)
{
  char buf[BUFSIZE];

  DIR * d = opendir(nam);

  if (d)
  {
    struct dirent *entry;
    while   ((entry = readdir(d)) != NULL)
    {
      if ( check && entry->d_type == DT_REG )
      {
        if (strlen(entry->d_name)==2)
        {
          printf("%s/%s\n", nam, entry->d_name);
        }
      }

      if (entry->d_type == DT_DIR)
      {
        int check;
        int len;
        strcpy( buf, nam );
        if ( ! has_slash(nam, strlen(nam)))
          strcat(buf, "/");
        strcat(buf, entry->d_name);
        len = strlen(entry->d_name);
        check =  ! strncmp( &entry->d_name[len-3], "bin", 3);
        if ( strcmp(".", entry->d_name) && strcmp("..", entry->d_name))
          rd(buf, check);
      }
    }
  }
  closedir(d);
  return 0;
}

int has_slash(const char * buf, size_t len)
{
  if ( len < 1) return 0;
  if ( buf[len-1] == '/' ) return 1;
  return 0;
}

int is_bin(const char * buf, size_t len)
{
  if ( has_slash( buf, len) )
  {
    len--;
  }
  if ( len < 3)
    return 0;
  else if ( len == 3 )
  {
    if ( ! strncmp(buf, "bin",3))
      return 1;
  }
  else
  {
    if ( !strncmp(&buf[len-4], "/bin", 4))
      return 1;
  }
  return 0;
}
int main (int argc, char * argv[])
{
  char buf[BUFSIZE];

  if (argc > 1)
  {
    strncpy(buf, argv[1], BUFSIZE-1);
    buf[BUFSIZE-1] = '\0';
  }
  else
  {
    getcwd(buf, sizeof(buf));
  }

  rd( buf, is_bin(buf, strlen(buf)));

  return 0;
}                                                                                                              



输出示例:



output example:


./rd/

/sbin/tc
/sbin/ss
/usr/lib/klibc/bin/ls
/usr/lib/klibc/bin/ln
/usr/lib/klibc/bin/dd
/usr/bin/pg
/usr/bin/pr
/usr/bin/m4
/usr/bin/at
/usr/bin/ar
/usr/bin/gs
/usr/bin/lp
/usr/bin/nm
/usr/bin/dc
/usr/bin/7z
/usr/bin/du
/usr/bin/wc
/usr/bin/bc
/usr/bin/tr
/usr/bin/ab
/usr/bin/uz
/usr/bin/od
/usr/bin/ul
/usr/bin/as
/usr/bin/nl
/usr/bin/xz
/usr/bin/id
/bin/ls
/bin/ip
/bin/cp
/bin/su
/bin/ln
/bin/mv
/bin/dd
/bin/ps
/bin/rm
/bin/df
./rd /

/sbin/tc
/sbin/ss
/usr/lib/klibc/bin/ls
/usr/lib/klibc/bin/ln
/usr/lib/klibc/bin/dd
/usr/bin/pg
/usr/bin/pr
/usr/bin/m4
/usr/bin/at
/usr/bin/ar
/usr/bin/gs
/usr/bin/lp
/usr/bin/nm
/usr/bin/dc
/usr/bin/7z
/usr/bin/du
/usr/bin/wc
/usr/bin/bc
/usr/bin/tr
/usr/bin/ab
/usr/bin/uz
/usr/bin/od
/usr/bin/ul
/usr/bin/as
/usr/bin/nl
/usr/bin/xz
/usr/bin/id
/bin/ls
/bin/ip
/bin/cp
/bin/su
/bin/ln
/bin/mv
/bin/dd
/bin/ps
/bin/rm
/bin/df


这篇关于在所有bin目录下找到2个字母命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆