
#
造
5
个
目录,每个目录下,造
3
个
文件和两个子目录如下:
cd
$home/tmp
for
i
in
d1
d2
d3
d4
d5
do
mkdir
-p
$i
touch
$i/1.txt
$i/2.txt
$i/3.txt
mkdir
-p
$i/tmp1
$i/tmp2
done
#
检验测试环境:
$
ls
-lr
d1
total
0
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
2.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
3.txt
drwxr-sr-x
2
wenlee
comm
256
dec
22
10:35
tmp1/
drwxr-sr-x
2
wenlee
comm
256
dec
22
10:35
tmp2/
#
利用下列脚本来实现你要做的:
cd
$home/tmp
for
i
in
*/1.txt
do
echo
"found
$i,
save
$i
and
remove
everything
else
under
$(dirname
$i)/"
save_this_file=$(basename
$i)
curr_dir=$(dirname
$i)
#
把这个1.txt暂时存到/tmp里面去,为了避免已经有同样的档案名称在/tmp,加上$$
(i.e.
pid)
mv
$i
/tmp/${save_this_file}.$$
rm
-rf
$curr_dir
mkdir
-p
$curr_dir
mv
/tmp/${save_this_file}.$$
$curr_dir
done
#
屏幕执行输出如下:
found
d1/1.txt,
save
d1/1.txt
and
remove
everything
else
under
d1/
found
d2/1.txt,
save
d2/1.txt
and
remove
everything
else
under
d2/
found
d3/1.txt,
save
d3/1.txt
and
remove
everything
else
under
d3/
found
d4/1.txt,
save
d4/1.txt
and
remove
everything
else
under
d4/
found
d5/1.txt,
save
d5/1.txt
and
remove
everything
else
under
d5/
#
复验实验环境:
$
ls
-l
d?/*
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d1/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d2/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d3/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d4/1.txt
-rw-r--r--
1
wenlee
comm
0
dec
22
10:35
d5/1.txt
ok?
thanks!
UNIX环境高级编程,或者LINUX程序设计里头都有这个例子。从《LINUX程序设计第二版》当中找了个print2.c的代码给你
/* We start with the appropriate headers and then a function, printdir,
which prints out the current directory.
It will recurse for subdirectories, using the depth parameter is used for indentation. */
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
void printdir(char *dir, int depth)
{
DIR *dp
struct dirent *entry
struct stat statbuf
if((dp = opendir(dir)) == NULL) {
fprintf(stderr,"cannot open directory: %s\n", dir)
return
}
chdir(dir)
while((entry = readdir(dp)) != NULL) {
lstat(entry->d_name,&statbuf)
if(S_ISDIR(statbuf.st_mode)) {
/* Found a directory, but ignore . and .. */
if(strcmp(".",entry->d_name) == 0 ||
strcmp("..",entry->d_name) == 0)
continue
printf("%*s%s/\n",depth,"",entry->d_name)
/* Recurse at a new indent level */
printdir(entry->d_name,depth+4)
}
else printf("%*s%s\n",depth,"",entry->d_name)
}
chdir("..")
closedir(dp)
}
/* Now we move onto the main function. */
int main(int argc, char* argv[])
{
char *topdir, pwd[2]="."
if (argc != 2)
topdir=pwd
else
topdir=argv[1]
printf("Directory scan of %s\n",topdir)
printdir(topdir,0)
printf("done.\n")
exit(0)
}
#include <Windows.h>#include <stdio.h>
#include <stdlib.h>
typedef void (*Callback)(const char *path, const WIN32_FIND_DATA *info, void *arg)
void PrintPath(const char *path, const WIN32_FIND_DATA *info, void *arg)
{
printf("%s\n", info->cFileName)
}
void RecursiveDirectory(const char *path, Callback callback, void *arg)
{
WIN32_FIND_DATA fd
HANDLE hd = INVALID_HANDLE_VALUE
hd = FindFirstFile(path, &fd)
if(hd == INVALID_HANDLE_VALUE)
{
return
}
do
{
callback(path, &fd, arg)
if((fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, ".."))
{
char subpath[MAX_PATH] = {'\0'}
sprintf(subpath, "%s\\%s\\*", path, fd.cFileName)
RecursiveDirectory(subpath, callback, arg)
}
} while(FindNextFile(hd, &fd))
FindClose(hd)
}
int main(int argc, char **argv)
{
if(argc > 1)
{
RecursiveDirectory(argv[1], PrintPath, NULL)
}
return 0
}
很久之前的一个框,有用就看吧。
*nix的*dir系列函数比win的Find*系列函数好用多了。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)