Linux系统怎么c 复制文件夹夹下的全部文件到另外文件夹

温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
World without strangers!
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
#include&stdio.h&#include&fcntl.h&#include&unistd.h&#include&sys/stat.h&#include&sys/types.h&#include&dirent.h&#include&string.h&char paths[1000],patht[1000],temp_paths[1000],temp_patht[1000];void Copy(char *spathname,char *tpathname){&& int sfd,&& struct stat s,t;&&&& sfd=open(spathname,O_RDONLY);&& tfd=open(tpathname,O_RDWR|O_CREAT);&& while(read(sfd,&c,1)&0)&&&&&&& write(tfd,&c,1);&& fstat(sfd,&s);&& chown(tpathname,s.st_uid,s.st_gid);&& chmod(tpathname,s.st_mode);&&&&& close(sfd);&& close(tfd);}void d_copy(char *spathname,char *tpathname){&& struct stat s,t,temp_s,temp_t;&& struct dirent *s_p;&& DIR *dirs,*&&&&& stat(spathname,&s);&& mkdir(tpathname,s.st_mode);&& chown(tpathname,s.st_uid,s.st_gid);&& dirt=opendir(tpathname);&& dirs=opendir(spathname);&& strcpy(temp_paths,spathname);&& strcpy(temp_patht,tpathname);&& while((s_p=readdir(dirs))!=NULL)&& {&&&&& if(strcmp(s_p-&d_name,".")!=0&&strcmp(s_p-&d_name,"..")!=0)&&&&& {&&&&&&&&& strcat(temp_paths,"/");&&&&&&&&& strcat(temp_paths,s_p-&d_name);&&&&&&&&& strcat(temp_patht,"/");&&&&&&&&& strcat(temp_patht,s_p-&d_name);&&&&&&&&& lstat(temp_paths,&s);&&&&&&&&& temp_s.st_mode=s.st_&&&&&&&&& if(S_ISLNK(temp_s.st_mode))&&&&&&&&& {&&&&&&&&&&&&& printf("%s is a symbol link file/n",temp_paths);&&&&&&&&& }&&&&&&&&& else if(S_ISREG(temp_s.st_mode))&&&&&&&&& {&&&&&&&&&&&&& printf("Copy file %s ....../n",temp_paths);&&&&&&&&&&&&& Copy(temp_paths,temp_patht);&&&&&&&&&&&&& strcpy(temp_paths,spathname);&&&&&&&&&&&&& strcpy(temp_patht,tpathname);&&&&&&&&& }&&&&&&&&& else if(S_ISDIR(temp_s.st_mode))&&&&&&&&& {&&&&&&&&&&&&& printf("Copy directory %s ....../n",temp_paths);&&&&&&&&&&&&& d_copy(temp_paths,temp_patht);&&&&&&&&&&&&& strcpy(temp_paths,spathname);&&&&&&&&&&&&& strcpy(temp_patht,tpathname);&&&&&&&&& }&&&&& }&& }}int main(){&& struct dirent *sp,*&& char spath[1000],tpath[1000],temp_spath[1000],temp_tpath[1000];&& struct stat sbuf,tbuf,temp_sbuf,temp_&& char sdirect[1000],tdirect[1000],&& DIR *dir_s,*dir_t;&&&&& printf("Please input the sourse direct path and name :");&& scanf("%s",sdirect);&& dir_s=opendir(sdirect);&& if(dir_s==NULL)&& {&&&&& printf("This directory don't exist !/n");&&&&& return 0;&& }&& if(stat(sdirect,&sbuf)!=0)&& {&&&&& printf("Get status error !/n");&&&&& return 0;&& }&& printf("Please input the target direct path and name :");&& scanf("%s",tdirect);&& dir_t=opendir(tdirect);&& if(dir_t==NULL)&& {&&&&& mkdir(tdirect,sbuf.st_mode);&&&&& chown(tdirect,sbuf.st_uid,sbuf.st_gid);&&&&& dir_t=opendir(tdirect);&& }&& else&& {&&&&& chmod(tdirect,sbuf.st_mode);&&&&& chown(tdirect,sbuf.st_uid,sbuf.st_gid);&& }&&&&& strcpy(spath,sdirect);&& strcpy(tpath,tdirect);&& strcpy(temp_spath,sdirect);&& strcpy(temp_tpath,tdirect);/////////////////////////////////////////////////////////////////////////////////&& printf("Begin copy ......../n");&& while((sp=readdir(dir_s))!=NULL)&& {&&&&& if(strcmp(sp-&d_name,".")!=0&&strcmp(sp-&d_name,"..")!=0)&&&&& {&&&&&&&&& strcat(temp_spath,"/");&&&&&&&&& strcat(temp_spath,sp-&d_name);&&&&&&&&& strcat(temp_tpath,"/");&&&&&&&&& strcat(temp_tpath,sp-&d_name);&&&&&&&&& lstat(temp_spath,&sbuf);&&&&&&&&& temp_sbuf.st_mode=sbuf.st_&&&&&&&&& if(S_ISLNK(temp_sbuf.st_mode))&&&&&&&&& {&&&&&&&&&&&&& printf("%s is a symbolic link file/n",temp_spath);&&&&&&&&& }&&&&&&&&& else if((S_IFMT&temp_sbuf.st_mode)==S_IFREG)&&&&&&&&& {&&&&&&&&&&&&& printf("Copy file %s ....../n",temp_spath);&&&&&&&&&&&&& Copy(temp_spath,temp_tpath);&&&&&&&&&&&&& strcpy(temp_tpath,tpath);&&&&&&&&&&&&& strcpy(temp_spath,spath);&&&&&&&&& }&&&&&&&&& else if((S_IFMT&temp_sbuf.st_mode)==S_IFDIR)&&&&&&&&& {&&&&&&&&&&&&& printf("Copy directory %s ....../n",temp_spath);&&&&&&&&&&&&& d_copy(temp_spath,temp_tpath);&&&&&&&&&&&&& strcpy(temp_tpath,tpath);&&&&&&&&&&&&& strcpy(temp_spath,spath);&&&&&&&&& }&&&&& }&& }&& printf("Copy end !/n");&& closedir(dir_t);&& closedir(dir_s);&& return 1;}
阅读(104)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'Linux C将一目录下的所有文件复制到另一目录',
blogAbstract:'\t编程中经常遇到要将一个目录下所有的文件和目录复制到另一个目录下的情况,下面是具体的实现:#include&stdio.h&#include&fcntl.h&#include&unistd.h&#include&sys/stat.h&#include&sys/types.h&#include&dirent.h&#include&string.h&char paths[1000],patht[1000],temp_paths[1000],temp_patht[1000];',
blogTag:'linux,c/c++',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:7,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'World without strangers!',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}新手园地& & & 硬件问题Linux系统管理Linux网络问题Linux环境编程Linux桌面系统国产LinuxBSD& & & BSD文档中心AIX& & & 新手入门& & & AIX文档中心& & & 资源下载& & & Power高级应用& & & IBM存储AS400Solaris& & & Solaris文档中心HP-UX& & & HP文档中心SCO UNIX& & & SCO文档中心互操作专区IRIXTru64 UNIXMac OS X门户网站运维集群和高可用服务器应用监控和防护虚拟化技术架构设计行业应用和管理服务器及硬件技术& & & 服务器资源下载云计算& & & 云计算文档中心& & & 云计算业界& & & 云计算资源下载存储备份& & & 存储文档中心& & & 存储业界& & & 存储资源下载& & & Symantec技术交流区安全技术网络技术& & & 网络技术文档中心C/C++& & & GUI编程& & & Functional编程内核源码& & & 内核问题移动开发& & & 移动开发技术资料ShellPerlJava& & & Java文档中心PHP& & & php文档中心Python& & & Python文档中心RubyCPU与编译器嵌入式开发驱动开发Web开发VoIP开发技术MySQL& & & MySQL文档中心SybaseOraclePostgreSQLDB2Informix数据仓库与数据挖掘NoSQL技术IT业界新闻与评论IT职业生涯& & & 猎头招聘IT图书与评论& & & CU技术图书大系& & & Linux书友会二手交易下载共享Linux文档专区IT培训与认证& & & 培训交流& & & 认证培训清茶斋投资理财运动地带快乐数码摄影& & & 摄影器材& & & 摄影比赛专区IT爱车族旅游天下站务交流版主会议室博客SNS站务交流区CU活动专区& & & Power活动专区& & & 拍卖交流区频道交流区
白手起家, 积分 6, 距离下一级还需 194 积分
论坛徽章:0
我安照书上的命令做了几次都不行,求助中.
&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp&&nbsp|&&nbsp
论坛徽章:0
cp -R &要复制的文件/目录& &目标位置&
把你的情况详细说一下,最好把错误陈述陈述。
另外,你看的是哪一本书?
白手起家, 积分 6, 距离下一级还需 194 积分
论坛徽章:0
我看的是电子工业出版社&&从入门到精通&&,red hat linux 7.
它上面说的可以从一个文件夹到另一个文件夹的操作每次都只能在本目录下复制.
白手起家, 积分 1, 距离下一级还需 199 积分
论坛徽章:0
把你的命令写上来吧,大家能帮你找到问题的,呵呵
稍有积蓄, 积分 436, 距离下一级还需 64 积分
论坛徽章:0
应该不会这样子的。把你的语法写上来给大家看看吧。
家境小康, 积分 1114, 距离下一级还需 886 积分
论坛徽章:1
/home/lee#cp --help
用法:cp [选项]... 来源 目的地
&&或:cp [选项]... 来源... 目录
&&或:cp [选项]... --target-directory=目录 来源...
将&来源&文件复制至&目的地&,或将多个&文件&复制至&目录&。
长选项必须用的参数在使用短选项时也是必须的。
&&-a, --archive& && && && && & 等於 -dpR
& && &--backup[=CONTROL]& && & 为每个已存在的目的地文件创建备份文件
&&-b& && && && && && && && && &类似 --backup,但不接受任何参数
& && &--copy-contents& && && & 当使用递归模式时复制特殊文件的内容
&&-d& && && && && && && && && &等於 --no-dereference --preserve=link
& && &--no-dereference& && && &不会找出符号链接指示的真正目的地
&&-f, --force& && && && && && &如果无法打开已存在的文件,会删除该文件并再
& && && && && && && && && && && &尝试打开
&&-i, --interactive& && && && &覆盖文件前需要确认
&&-H& && && && && && && && && &使用命令列中的符号链接指示的真正目的地
&&-l, --link& && && && && && & 链接而非复制文件
&&-L, --dereference& && && && &一定先找出符号链接指示的真正目的地
&&-p& && && && && && && && && &等於 --preserve=mode,ownership,timestamps
& && &--preserve[=ATTR_LIST]& &若可能,保留指定的文件属性
& && && && && && && && && && && &(默认值为:mode,ownership,timestamps)
& && && && && && && && && && && &额外的属性有:links、all
& && &--sno-preserve=ATTR_LIST&&不保留指定的文件属性
& && &--parents& && && && && & 复制前先在&目录&创建来源文件路径中的所有目录
&&-P& && && && && && && && && &等於‘--no-dereference’
&&-R, -r, --recursive& && && & 复制目录及目录内的所有项目
& && &--remove-destination& &&&尝试打开目的地文件前先删除已存在的目的地
& && && && && && && && && && & 文件 (与 --force 选项作对比)
& && &--reply={yes,no,query}& &指定如何处理已存在的目的地文件
& && &--sparse=WHEN& && && && &控制创建 sparse 文件的方式
& && &--strip-trailing-slashes 删除参数中所有&来源&文件/目录末端的斜杠
&&-s, --symbolic-link& && && & 只创建符号链接而不是复制文件
&&-S, --suffix=后缀& && && && &自行指定备份文件的&后缀&
& && &--target-directory=目录&&将所有参数指定的&来源&文件/目录复制至&目录&
&&-u, --update& && && && && &&&只在&来源&文件比目的地文件新,或目的地文件
& && && && && && && && && && && &不存在时才进行复制
&&-v, --verbose& && && && && & 详细显示进行的步骤
&&-x, --one-file-system& && &&&不会跨越文件系统进行操作
& && &--help& &&&显示此帮助信息并离开
& && &--version&&显示版本信息并离开
默认使用模式中,&来源&文件是否 sparse 文件会由一种粗略的方式决定,而且相应
的&目的地&文件也会是 sparse 文件。此方式等於使用 --sparse=auto 选项。指定
--sparse=always 则只要&来源&文件含有足够长的 0 字节都会产生 sparse 的
&目的地&文件。
使用 --sparse=never 会禁止产生 sparse 文件。
备份文件的后缀为‘~’,除非以 --suffix 选项或是 SIMPLE_BACKUP_SUFFIX
环境变量指定。版本控制的方式可透过 --backup 选项或 VERSION_CONTROL 环境
变量来选择。以下是可用的变量值:
&&none, off& && & 不会进行备份 (即使使用了 --backup 选项)
&&numbered, t& &&&备份文件会加上数字
&&existing, nil& &若有数字的备份文件已经存在则使用数字,否则使用普通方式备份
&&simple, never& &永远使用普通方式备份
有一个特别情况:如果同时指定 --force 和 --backup 选项,而且&来源&和&目的地&
是同一个已存在的一般文件的话,cp 会将&来源&文件备份。
Report bugs to &bug-coreutils@gnu.org&.
复制代码
当你对某个command感到困惑的时候,首先应该看他的帮助页! 这是一个好习惯!
白手起家, 积分 6, 距离下一级还需 194 积分
论坛徽章:0
我已经解决了这个问题.谢谢大家的帮助!
稍有积蓄, 积分 482, 距离下一级还需 18 积分
论坛徽章:4
怎么解决的、期待、
论坛徽章:4
白手起家, 积分 150, 距离下一级还需 50 积分
论坛徽章:0
6楼的中文help 能赐教吗
北京皓辰网域网络信息技术有限公司. 版权所有 京ICP证:060528号 北京市公安局海淀分局网监中心备案编号:
广播电视节目制作经营许可证(京) 字第1234号
中国互联网协会会员&&联系我们:
感谢所有关心和支持过ChinaUnix的朋友们
转载本站内容请注明原作者名及出处Linux下如何使用cp命令复制文件及复制目录
作者:佚名
字体:[ ] 来源:互联网 时间:12-09 15:38:24
cp命令可以复制文件,加上参数还可以复制目录,想要更多的了解它,可以看看下面的cp命令的具体用法
  Linux下cp命令的用法主要用于复制文件,加上参数还可以复制目录,下面随小编一起来了解下Linux下cp命令的具体用法。
  一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数。但是如果是在shell脚本中执行cp时,没有-i参数时不会询问是否覆盖。这说明命令行和shell脚本的执行方式有些不同。
  1.命令格式:
  用法:
  cp [选项]。。。 [-T] 源 目的
  或:cp [选项]。。。 源。。。 目录
  或:cp [选项]。。。 -t 目录 源。。。
  2.命令功能:
  将源文件复制至目标文件,或将多个源文件复制至目标目录。
  3.命令参数:
  -a, --archive 等于-dR --preserve=all
  --backup[=CONTROL 为每个已存在的目标文件创建备份
  -b 类似--backup 但不接受参数
  --copy-contents 在递归处理是复制特殊文件内容
  -d 等于--no-dereference --preserve=links
  -f, --force 如果目标文件无法打开则将其移除并重试(当 -n 选项
  存在时则不需再选此项)
  -i, --interactive 覆盖前询问(使前面的 -n 选项失效)
  -H 跟随源文件中的命令行符号链接
  -l, --link 链接文件而不复制
  -L, --dereference 总是跟随符号链接
  -n, --no-clobber 不要覆盖已存在的文件(使前面的 -i 选项失效)
  -P, --no-dereference 不跟随源文件中的符号链接
  -p 等于--preserve=模式,所有权,时间戳
  --preserve[=属性列表 保持指定的属性(默认:模式,所有权,时间戳),如果
  可能保持附加属性:环境、链接、xattr 等
  -R, -r, --recursive 复制目录及目录内的所有项目&
  4.命令实例:
  实例一:复制单个文件到目标目录,文件在目标文件中不存在
  命令:
  cp log.log test5
  输出:
  [root@localhost test]# cp log.log test5
  [root@localhost test]# ll
  -rw-r--r-- 1 root root 0 10-28 14:48 log.log
  drwxr-xr-x 6 root root
  drwxrwxrwx 2 root root
14:47 test3
  drwxr-xr-x 2 root root
14:53 test5
  [root@localhost test]# cd test5
  [root@localhost test5]# ll
  -rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
  -rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
  -rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
  -rw-r--r-- 1 root root 0 10-28 14:53 log.log
  说明:
  在没有带-a参数时,两个文件的时间是不一样的。在带了-a参数时,两个文件的时间是一致的。
  实例二:目标文件存在时,会询问是否覆盖
  命令:
  cp log.log test5
  输出:
  [root@localhost test]# cp log.log test5
  cp:是否覆盖&test5/log.log&? n
  [root@localhost test]# cp -a log.log test5
  cp:是否覆盖&test5/log.log&? y
  [root@localhost test]# cd test5/
  [root@localhost test5]# ll
  -rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
  -rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
  -rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
  -rw-r--r-- 1 root root 0 10-28 14:48 log.log
  说明:
  目标文件存在时,会询问是否覆盖。这是因为cp是cp -i的别名。目标文件存在时,即使加了-f标志,也还会询问是否覆盖。
  实例三:复制整个目录
  命令:
  输出:
  目标目录存在时:
  [root@localhost test]# cp -a test3 test5
  [root@localhost test]# ll
  -rw-r--r-- 1 root root 0 10-28 14:48 log.log
  drwxr-xr-x 6 root root
  drwxrwxrwx 2 root root
14:47 test3
  drwxr-xr-x 3 root root
15:11 test5
  [root@localhost test]# cd test5/
  [root@localhost test5]# ll
  -rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log
  -rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log
  -rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log
  -rw-r--r-- 1 root root 0 10-28 14:48 log.log
  drwxrwxrwx 2 root root
14:47 test3
  目标目录不存在是:
  [root@localhost test]# cp -a test3 test4
  [root@localhost test]# ll
  -rw-r--r-- 1 root root 0 10-28 14:48 log.log
  drwxr-xr-x 6 root root
  drwxrwxrwx 2 root root
14:47 test3
  drwxrwxrwx 2 root root
14:47 test4
  drwxr-xr-x 3 root root
15:11 test5
  [root@localhost test]#
  说明:
  注意目标目录存在与否结果是不一样的。目标目录存在时,整个源目录被复制到目标目录里面。
  实例四:复制的 log.log 建立一个连结档 log_link.log
  命令:
  cp -s log.log log_link.log
  输出:
  [root@localhost test]# cp -s log.log log_link.log
  [root@localhost test]# ll
  lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -》 log.log
  -rw-r--r-- 1 root root 0 10-28 14:48 log.log
  drwxr-xr-x 6 root root
  drwxrwxrwx 2 root root
14:47 test3
  drwxrwxrwx 2 root root
14:47 test4
  drwxr-xr-x 3 root root
15:11 test5
  说明:
  那个 log_link.log 是由 -s 的参数造成的,建立的是一个『快捷方式』,所以您会看到在文件的最右边,会显示这个文件是『连结』到哪里去的!
  上面就是Linux中cp命令的用法介绍了,单独使用cp命令是无法复制文件夹的,需要加上-f参数,怎么样,你对mv命令有所了解了吗?
大家感兴趣的内容
12345678910
最近更新的内容}

我要回帖

更多关于 mac系统复制文件夹 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信