bash
function fl () {
if [[ -z "$1" ]]; then
fl_read_dir $PWD
elif [[ "${1: -1}" == '/' ]]; then
fl ${1%/}
else
fl_read_dir $1
fi
}
function fl_read_dir () {
for file in `ls -a $1`; do
if [ -d $1"/"$file ]; then
if [[ $file != '.' && $file != '..' ]]; then
fl_read_dir $1"/"$file
fi
else
echo $1"/"$file
fi
done
}
C
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
void do_ls (char *);
int main (int argc, char *argv[]) {
if (argc == 1)
do_ls ("."); // 打开当前目录
else
for (int i = 1; i < argc; i++)
do_ls (argv[i]);
}
void do_ls (char *dirname) {
if (dirname[strlen (dirname) - 1] == '/')
dirname[strlen (dirname) - 1] = '\0';
struct dirent *direntp;
DIR *dir_ptr = opendir (dirname);
if (dir_ptr == NULL) {
fprintf (stderr, "can not open %s", dirname);
} else {
while (direntp = readdir (dir_ptr), direntp != NULL)
if (direntp->d_type == DT_DIR) {
if (strcmp (direntp->d_name, ".") &&
strcmp (direntp->d_name, "..")) {
char *tmp_dir = (char *) malloc (
strlen (dirname) + strlen (direntp->d_name) + 1 + 1);
strcpy (tmp_dir, dirname);
strcat (tmp_dir, "/");
strcat (tmp_dir, direntp->d_name);
do_ls (tmp_dir);
free (tmp_dir);
}
} else
printf ("%s/%s\n", dirname, direntp->d_name);
closedir (dir_ptr);
}
}
标签:tmp,direntp,文件名,打印,include,Bash,file,dirname,dir
From: https://www.cnblogs.com/YHFBlogs/p/17311532.html