操作系统-VM-linux命令解释器

简介: 操作系统-VM-linux命令解释器

一. 程序概述


  • 完成的任务


1.mypwd列出当前目录的名称。


2.mychdir改变当前目录。


3.mymkdir新建一个目录


4.myrmdir删除文件夹


5.exit退出命令解释器。


6.myrename将文件或文件夹重命名。


7.mycp复制当前已存在的文件。


8.myfind在指定的目录查找指定的文件或文件夹,并输出绝对路径。


9.mylist列出目录名中全部的目录和文件。


10.mydate显示与设置系统的日期和时间。


11.mycd切换目录


  • 遇到的困难及解决的问题


1.安装的虚拟机不是中文的,我先是在终端输入“sudo apt -y install fcitx fcitx-bin fcitx-table fcitx-table-all”,再输入“sudo apt -y install fcitx-config-gtk”安装可视化配置界面;然后用火狐下载安装搜狗输入法(linux版64位),并在ubuntu软件中心安装,再将输入法框架改为fcitx,并下载“language support”中的chinese包,再切换输入法,设置为搜狗


2.无法实现像学校一样可以从桌面拖入拖进文件的功能,我先是下了VMware TOOL,然后将压缩包放桌面,再进入终端,“sudo su”进入超级用户模式,再一步一步cd访问安装,最后重启实现


3.date功能在网上看了很多,但不知道为什么结构图添加就报错


二. 概念原理


本次操作系统课程设计使用的语言为C++,包含的头文件大多是Linux下c的函数库。通过调用相关的函数库来实现模拟shell命令操作。下面是对一些概念原理的说明:


  1. 源程序包含的部分头文件


#include<sys/time.h>//gettimeoftime/settimeofday的函数
#include<time.h>//包含获取/转换时间的函数
#include<iostream>//主要用于一些标准输入输出:cin,cout操作
#include<cstring>//标准C++函数库,主要用于字符串处理
#include<sys/types.h>//基本系统数据类型
#include<sys/stat.h>//文件状态
#include<dirent.h>//文件操作函数
#include<fcntl.h>//文件控制
#include<ftw.h>//文件树漫游
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#include<string.h>


以上是整个源程序涉及到的一些函数头文件


  1. 函数概念说明


以下是对程序调用Linux c函数库的方法说明:


(1).调用getcwd()函数


函数原型:char * getcwd(char * buf,size_t size);


函数说明:getcwd()会将当前的工作目录绝对路径复制到参数buf所指的内存空间,参数size为buf的空间大小。在调用此函数时,buf所指的内存空间要足够大,若工作目录绝对路径的字符串长度超过参数size大小,则回值NULL,errno的值则为ERANGE。倘若参数buf为NULL,getcwd()会依参数size的大小自动配置内存(使用malloc()),如果参数size也为0,则getcwd()会依工作目录绝对路径的字符串程度来决定所配置的内存大小,进程可以在使用完此字符串后利用free()来释放此空间。


返回值:执行成功则将结果复制到参数buf所指的内存空间,或是返回自动配置的字符串指针。失败返回NULL,错误代码存于errno。


(2).调用opendir()函数


函数原型:DIR * opendir(const char * name);


函数说明:opendir()用来打开参数name指定的目录,并返回DIR形态的目录流,和open()类似,接下来对目录的读取和搜索都要使用此返回值。


返回值:成功则返回DIR 型态的目录流,打开失败则返回NULL。


(3).调用readdir()函数


函数原型:struct dirent * readdir(DIR * dir);


函数说明:readdir()返回参数dir目录流的下个目录进入点。


结构dirent定义如下


struct dirent
{
ino_t d_ino;
ff_t d_off;
signed short int d_reclen;
unsigned char d_type;
har d_name[256;
};


  • d_ino 此目录进入点的inode


  • d_off 目录文件开头至此目录进入点的位移


  • d_reclen _name的长度,不包含NULL字符


  • d_type d_name 所指的文件类型


  • d_name 文件名


  • 返回值:成功则返回下个目录进入点。有错误发生或读取到目录文件尾则返回NULL。


  • 附加说明:EBADF参数dir为无效的目录流。


(4).调用closedir()函数


函数原型:*int closedir(DIR dir);


函数说明:closedir()关闭参数dir所指的目录流。


返回值:关闭成功则返回0,失败返回-1,错误原因存于errno 中。


(5).调用chdir()函数


函数原型:int chdir(const char * path);


函数说明:chdir()用来将当前的工作目录改变成 以参数path所指的目录。


返回值:执行成功返回0,失败返回-1;


(6).调用mkdir()函数


函数原型:*int mkdir(const char pathname, mode_t mode);


函数说明:mkdir()函数以mode方式创建一个以参数pathname命名的目录,mode定义新创建目录的权限。


返回值:若目录创建成功,则返回0,否则返回-1;


(7).调用rmdir()函数


函数原型:*int _rmdir(const char dirname);


函数说明:rmdir()函数删除以参数dirname为命名的目录。


返回值:若目录删除成功,则返回0,否则返回-1;


(8).调用rename()函数


函数原型:int rename(const char * oldpath,const char * newpath);


函数说明:rename()会将参数oldpath 所指定的文件名称改为参数newpath所指的文件名称。若newpath所指定的文件已存在,则会被删除。


返回值:执行成功则返回0,失败返回-1。


(9).调用ftw()函数


表头文件:#include <ftw.h>


函数原型:int ftw(const char *dir, int (*fn) (const file, const struct stat sb, int flag), int depth)


函数说明:ftw() 会从参数dir指定的 目录 开始,往下一层层地递归式遍历子 目录 。ftw()会传三个参数给fn(), 第一个参数file指向当时所在的 目录 路径,第二个参数是sb, 为stat结构指针,第三个参数为旗标,有下面几种可能值:


  • FTW_F 一般文件 FTW_D 目录


  • FTW_DNR 不可读取的 目录 ,此 目录 以下将不被遍历


  • FTW_SL 符号连接


  • FTW_NS 无法取得stat结构数据,有可能是权限问题


最后一个参数depth代表ftw()在进行遍历 目录 时同时打开的文件数。ftw()在遍历时每一层 目录 至少需要一个文件描述词,如果遍历时用完了depth所给予的限制数目,整个遍历将因不断地关文件和开文件操作而显得缓慢.


如果要结束ftw()的遍历,fn()只需返回一非零值即可,此值同时也会是ftw()的返回值。否则ftw()会试着走完所有的 目录 ,然后返回0.


返回值:遍历中断则返回fn()函数的返回值,全部遍历则返回0,若有错误发生则返回-1


三. 详细设计


  1. 菜单:在列举菜单之前先把控制台用clear命令清除内容,再进行显示,以保证界面美观,在菜单之后直接调用pwd()函数显示当前工作目录,方便用户查看当前工作目录。


system("clear");
   cout<<"~~~~~~~~~欢迎来到lotay的命令解释器~~~~~~~~~~"<<endl;
(省略。。。。)
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
pwd();
cout<<endl;


  1. 显示当前所在目录的路径名:这个功能写在pwd()函数中,使用getcwd()函数获取当前目录存进path数组中并且打印在控制台。


char path[100];
    getcwd(path,100);//Get path
cout<<"current directory: "<<path<<endl;


  1. 列举指定目录中的所有目录及文件:此功能写在list()函数中,通过传入的目录,用opendir()函数打开目录,获取目录流,并且用readdir()函数读取每一个目录节点,打印出信息,最后closedir()关闭该目录。


DIR* d = opendir(dir.c_str());
    if(d==NULL) {
        return false;
    } else {
  struct dirent *dirent;
        while(dirent=readdir(d)) {
      cout<<" "<<dirent->d_name;
  }
  closedir(d);
cout<<endl;
        return true;
}


  1. 改变当前目录:使用chdir()函数改变当前打开的工作目录,返回0时,改变成功,chdir()函数返回true,否则返回false。


if(chdir(path.c_str())==0) {
        return true;
    } else {
        return false;
}


  1. 新建目录:在函数makedir()中调用系统的mkdir函数创建指定目录名的目录,当返回0时创建成功,makedir ()函数返回true,否则返回false,创建的目录一般具有所有权限。


if(mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)==0) {
        return true;
    } else {
        return false;
}


  1. 删除目录:在deldir()中调用rmdir函数删除指定目录,返回0时删除成功,deldir ()函数返回true,否则返回false。


if(rmdir(dir.c_str())==0) {
        return true;
    } else {
        return false;
}


  1. 重命名:在rename()函数中调用C中的标准函数rename,将旧的目录或文件名改为新的目录或文件名,当返回0时,重命名成功,rename()函数返回true,否则返回false。


if(rename(lastname.c_str(),newname.c_str())==0) {
        return true;
    } else {
        return false;
}


  1. 复制文件:先判断文件是否存在,若存在则判断目标文件是否存在,假如已经存在那么便在原来的文件名字后缀加上(1)再复制,加入不存在则直接复制,复制的方式是先调用read()函数从源文件读出内容存进buf数组,接着调用write()函数将内容写进目标文件。


int fo1,fo2;
    char buf[1024];
    fo1=open(existname.c_str(),O_RDONLY);
    if(fo1==-1) {
        return false;
    } else {
  fo2=open(newname.c_str(),O_RDONLY);
  if(fo2!=-1) {
          int i;
      cout<<"Overwrite original file??"<<endl;
      cout<<"----1 is yes,not 1 is no.";
      cin>>i;
      if(i!=1) {
    newname+="(1)";
       }
       close(fo2);
  }
        fo2=open(newname.c_str(),O_WRONLY|O_CREAT,S_IRWXU);
  int size = read(fo1,buf,sizeof(buf));
  write(fo2,buf,size);
  close(fo1);
  close(fo2);
  return true;
}


  1. 查找指定文件和目录:通过调用ftw函数回调遍历指定目录中的文件,fn是回调函数,每一次遍历到一个节点,ftw函数都会把节点路径和节点信息以及类型传入fn函数中,同时,把需要查找的文件名字作为全局变量,在fn中判断是否存在并且输出。


ftw(dir.c_str(),fn,500);
int fn(const char *fpath, const struct stat *st, int typeflag) {
    for(int i=strlen(fpath)-1,j=file.length()-1;;i--,j--) {
  if(j==-1&&fpath[i]=='/') {
      cout<<"  "<<fpath;
      if(typeflag==FTW_F) cout<<"   FILE"<<endl;
      else if(typeflag==FTW_D) cout<<"  DIRECTORY"<<endl;
      num++;
      break;
  }
  if(fpath[i]=='/') break;
  if(j==-1) break;
  if(fpath[i]!=file[j]) {
      break;
  } 
    }
    return 0;
}


  1. 退出:直接在main函数中返回0。


四. 完成情况


完成了整个操作系统课程设计的命令行功能基本要求:


1.mypwd列出当前目录的名称。


2.mychdir改变当前目录。


3.mymkdir新建一个目录


4.myrmdir删除文件夹


5.exit退出命令解释器。


6.mycp复制当前已存在的文件。


7.mylist列出目录名中全部的目录和文件。


8.mydate显示与设置系统的日期和时间。


9.mycd切换目录


基本完成了以下几个扩展命令的要求:


1.myrename将文件或文件夹重命名。


2.myfind在指定的目录查找指定的文件或文件夹,并输出绝对路径。


通过测试,以上所有命令都运行正常。整个程序,代码实现简单清晰,没有太复杂的算法。基本上就是对函数的调用实现。运行结果又较好的提示信息,无论是成功错误都提示相应的信息。源代码的注释内容也非常清晰,方便查看理解函数调用的功能。


五. 使用情况


  1. 首先进入命令解释权界面


在进入程序会有菜单可供选择,一共有11个功能,并且在菜单下面会有当前工作目录显示在界面上,如下图:


  1. 更改当前目录的名字(输入命令2)


  1. 新建一个目录(命令3)


  1. 删除一个目录(命令4)


上面是存在该目录的情况,下面是不存在该目录的情况


  1. 退出命令解释器(命令5)


  1. 将文件重命名(命令6)


  1. 复制已经存在的文件(命令7)


  1. 查找文件


六. 设计总结


  1. 经验教训


一开始还是应该好好把vmtools装好的,这样可以节约很多试错代码的时间,其实我对c语言和c++有点混淆,不过好在平时上机也经常有用c++做,只要有思路大概就可以做出来,csdn真是个很好的学习网站,对Linux我还挺感兴趣,他的界面UI简单美观,操作方便,还可以学习一些命令代码,感觉敲代码的过程很享受,不过经常一遇见Bug就无从下手了,并且网上的资料也比较零碎,我应该去图书馆系统的看看馆藏讲解linux的书籍的。回顾整个课设,主要是date那里花了很多时间,不知道为什么结构体加进去报错,我下去再看看,希望进一步提升自己的能力。难的地方是如果去理解Linux c函数库的调用。需要花时间去理解每一个函数的作用和相关参数的作用。刚开始做的时候,也是不知道怎么开始,之前把问题想得复杂化了,后来通过一步一步实现每个指令,才慢慢找到解决的办法。


  1. 实践感受


总的来说,整个课程设计还算比较顺利,因为对Linux操作系统接触得并不多,对一些命令行的实现还不怎么清楚,后来通过查阅资料,发现Linux确实非常简洁好用,我非常喜欢这样的系统。历时两天的时间,把程序设计出来了,也认真写了下这次的文档。感觉通过一段时间的学习,自己的编程能力确实变强了,但还是有许多不足。在程序设计过程中,要学会查看文档,因为很多文档都是英文,这就需要英文好一点。我也在克服查看英文文档的障碍,努力提升自己的英文阅读能力。通过这次课程设计,也让自己捡起了C++这门语言,熟悉了Linux环境下的一些命令操作,并且让我有了极大兴趣去研究Linux,我在接下来的时间里也会使用和学习Linux程序设计。总的来说,感觉不错。


七. 参考文献


[1]《教材名称》,主编,出版社,出版时间 (教材)


[2] 作者.文献题名[J].刊名,年,卷(期):起止页码. (期刊论文)


[3] 作者.文献题名[D].出版地:出版单位,出版年:起止页码 (学位论文)


[4] [文献类型/载体类型标识]:[J/OL]网上期刊、[EB/OL]网上电子公告 (电子文献)


[5] 《Linux 程序设计第4版》


[6] 《The GUN C Library Manual》


代码详情:


#include<iostream>
#include<stdio.h>
#include<cstring>
#include<dirent.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<dirent.h>
#include<fcntl.h>
#include<ftw.h>
#include<errno.h>
#include<string.h>
#include<sys/time.h>//gettimeoftime/settimeofday的函数
#include<time.h>//包含获取/转换时间的函数
using namespace std;
//struct tm
//{
//  int tm_sec;//秒
//  int tm_year;//年
//  int tm_mon;//月
//  int tm_mday;//日
//  int tm_hour;//时
//  int tm_min;//分
//  int tm_sec;//秒
//}
//struct timeval
//{
//  _time_t tv_sec;
//  _suseconds_t tv_usec;
//}
//struct timezone
//{
//  int tz_minuteswest;
//  int tz_dsttime;
//}
void pwd()
{
  char path[100];
  getcwd(path,100);
  cout<<"当前目录"<<path<<endl;
}
bool list(string dir)
{
  DIR* d=opendir(dir.c_str());
  if(d==NULL)
  {
    return false;
  }
  else
  {
    struct dirent *dirent;
    while(dirent=readdir(d))
    {
      cout<<" "<<dirent->d_name;
//文件名,文件类型,文件名长
    }
    closedir(d);
    cout<<endl;
    return true;
  }
}
bool changedir(string path)
{
  if(chdir(path.c_str())==0)
  {
    return true;
  }
  else
  {
    return false;
  }
}
bool makedir(string dir)
{
  if(mkdir(dir.c_str(),S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH)==0)
  {
    return true;
  }
  else
  {
    return false;
  }
}
bool deldir(string dir)
{
  if(rmdir(dir.c_str())==0)
  {
    return true;
  }
  else
  {
    return false;
  }
}
bool rename(string lastname,string newname){
  if(rename(lastname.c_str(),newname.c_str())==0)
  {
    return true;
  }
  else
  {
    return false;
  }
}
bool copy(string existname,string newname){
  int fo1,fo2;
  char buf[1024];
  fo1=open(existname.c_str(),O_RDONLY);
  if(fo1==-1)
  {
    return false;
  }
  else
  {
    fo2=open(newname.c_str(),O_RDONLY);
    if(fo2!=-1)
    {
      int i;
      cout<<"是否重新书写原目录 ?"<<endl;
      cout<<"-----1 是,not 1 不是."<<endl;
      cin>>i;
      if(i!=1)
      {
        newname+="(1)";
      }
      close(fo2);
    }
    fo2=open(newname.c_str(),O_WRONLY|O_CREAT,S_IRWXU);
    int size=read(fo1,buf,sizeof(buf));
    write(fo2,buf,size);
    close(fo1);
    close(fo2);
    return true;
  }
}
int num=0;
string file;
int fn(const char *fpath,const struct stat *st,int typeflag)
{
  for(int i=strlen(fpath)-1,j=file.length()-1;;i--,j--)
  {
    if(j==-1&&fpath[i]=='/')
    {
      cout<<" "<<fpath;
      if(typeflag==FTW_F)cout<<" FILE"<<endl;
      else if(typeflag==FTW_D)cout<<" DIRECTORY"<<endl;
      num++;
      break;
    }
    if(fpath[i]=='/')break;
    if(j==-1)break;
    if(fpath[i]!=file[i])
    {
      break;
    }
  }
  return 0;
}
bool find(string dir,string filename)
{
  file=filename;
  ftw(dir.c_str(),fn,500);
  if(num==0)
  {
    return false;
  }
  else
  {
    return true;
  }
}
void cd()
{
  //char buffer[1024];
  //FILE *in,*out;
  //int len;
  //if((in=fopen(dirname1,"r"))==NULL)
  //{
  //  cout<<"这个文件无法打开"<<endl;
  //  exit(1);
  //}
  //if((out=fopen(dirname2,"w"))==NULL)
  //{
  //  cout<<"这个新文件无法打开"<<endl;
  //  exit(1);
  //}
  //while((len=fread(buffer,1,1024,in))>0)
  //{
  //  fwrite(buffer,1,len,out);
  //  memset(buffer,0,1024);
  //}
  //fclose(out);
  //fclose(in);
   char dirname[20];  
   cin>>dirname;
   if(chdir(dirname) == -1)  
   {  
      cout<<"这个文件不存在!"<<endl;  
    }  
    else  
    {  
      cout<<"切换文件成功!!!"<<endl;  
     }  
}
//int alllist(char *dirPath)
//{
  //DIR *dir=opendir(dirPath);
  //if(dir==NULL)
  //{
  //  cout<<strerror(errno)<<endl;
  //  return;
  //}
  //chdir(dirPath);
  //struct dirent *ent;
  //while((ent=readdir(dir))!=NULL)
  //{
  //  if(strcmp(ent->d_name,".")==0||strcmp(ent->d_name,"..")==0)
  //  {
  //    continue;
  //  }
  //  struct stat st;
  //  stat(ent->d_name,&st);
  //  if(S_ISDIR(st.st_mode))
  //  {
  //    getFileName(ent->d_name);
  //  }
  //  else
  //  {
  //    cout<<ent->d_name<<endl;
  //  }
  //  closedir(dir);
  //  chdir("..");
  //}
//}
long gettimesec(void)
{
  long cursec=0;
  struct timeval curtimesec;
  gettimeofday(&curtimesec,NULL);
  cursec=curtimesec.tv_sec;
  return cursec;
}
void TimeSet(int year,int month,int day,int hour,int min,int sec)
{
  struct tm tptr;
  struct timeval tv;
  tptr.tm_year=year+1900;
  tptr.tm_mon=month+1;
  tptr.tm_mday=day;
  tptr.tm_hour=hour;
  tptr.tm_min=min;
  tptr.tm_sec=sec;
  tv.tv_sec=mktime(&tptr);
  tv.tv_usec=0;
  settimeofday(&tv,NULL);
}
void time()
{
  time_t now;
  //struct tm *timenow;
  time(&now);
  //timenow=localtime(&now);
  //cout<<"本地时间是:"<<asctime(timenow)<<endl;
}
string datetime(){
//    time_t now = time(0);// 基于当前系统的当前日期/时间
//    tm *ltm = localtime(&now);
//    char iyear[50],imonth[50],iday[50],ihour[50],imin[50],isec[50];
//    sprintf(iyear, "%d",1900 + ltm->tm_year );
//    sprintf(imonth, "%02d", 1 + ltm->tm_mon );
//    sprintf(iday, "%02d", ltm->tm_mday );
//    sprintf(ihour, "%02d", ltm->tm_hour );
//    sprintf(imin, "%02d",  ltm->tm_min);
//    sprintf(isec, "%02d",  ltm->tm_sec);
//    vector<string> sDate{iyear, imonth, iday};
//    vector<string> sTime{ihour, imin, isec};
//    string myDate = boost::algorithm::join(sDate, "-") ;
//    string myTime = boost::algorithm::join(sTime, ":") ;
//    vector<string> sDateTime{myDate, myTime};
//    string myDateTime = boost::algorithm::join(sDateTime, " ") ;
//    return myDateTime;
    time_t timep;//time_t表示的时间转换为没有经过时区转换的UTC时间,是一个struct tm结构指针
    time (&timep);//调用time()方法获取time_t类型的当前时间
    char tmp[64];
    strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep) );
    //调用strftime()函数格式化localtime(&timep)并把它存在tmp中
    //localtime()函数将timep的指分解成tm结构,并用本地时区表示
    puts(tmp);
}
void menu(){
  system("clear");
  cout<<"~~~~~~~~~~~~~~~~~~~~~~~~欢迎来到lotay的命令解释器~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
  cout<<"1.{mypwd}列出当前目录的名称。"<<endl;
  cout<<"2.{mychdir}改变当前目录。"<<endl;
  cout<<"3.{mymkdir}新建一个目录。"<<endl;
  cout<<"4.{myrmdir}删除文件夹。"<<endl;
  cout<<"5.{exit}退出命令解释器。"<<endl;
  cout<<"6.{myrename}将文件或文件夹重命名。"<<endl;
  cout<<"7.{mycp}复制当前已存在的文件。"<<endl;
  cout<<"8.{myfind}在指定的目录查找指定的文件或文件夹,并输出绝对路径。"<<endl;
  cout<<"9.{mylist}列出目录名中全部的目录和文件。"<<endl;
  cout<<"10.{mydate}显示与设置系统的日期和时间。"<<endl;
  cout<<"11.{mycd}切换目录。"<<endl;
  cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
  pwd();
  cout<<endl;
}
int main()
{
//struct tm;
system("data");//未修改前时间
TimeSet(2020,12,11,1,30,8);
system("data");//修改后时间
menu();
string s;
while(1){
cout<<"请输入菜单数字:"<<endl;
cin>>s;
if(s=="1")
{
//列出当前目录名
  menu();
  cout<<"请输入当前目录:"<<endl;
  string dir;
  cin>>dir;
    if(!list(dir))
    {
      cout<<"打开失败/没有该目录!"<<endl;
    }
}
else if(s=="2")
{
//改变当前目录
  menu();
  cout<<"请输入指定的文件名或目录名:"<<endl;
  string path;
  cin>>path;
  if(!changedir(path))
  {
    cout<<"打开失败/该目录中不存在!"<<endl;
  }
  else
  {
    cout<<"当前文件已更改"<<path<<endl;
  }
}
else if(s=="3")
{
//新建
  cout<<"请输入一个新的文件名:"<<endl;
  string dir;
  cin>>dir;
  if(!makedir(dir))
  {
    cout<<"创建文件失败!"<<endl;
  }
  else
  {
    cout<<"文件创建成功啦~"<<endl;
  }
}
else if(s=="4")
{
//删除
  menu();
  cout<<"请输入一个已存在的文件名:"<<endl;
  string dir;
  cin>>dir;
  if(!deldir(dir))
  {
    cout<<"文件不存在/删除失败"<<endl;
  }
  else
  {
    cout<<"文件删除成功"<<endl;
  }
}
else if(s=="5")
{
//退出
  menu();
  cout<<"那拜拜啦呀,欢迎下次再来嘻嘻!"<<endl;
  return 0;
}
else if(s=="6")
{
//重命名
  menu();
  string lastname,newname;
  cout<<"请输入旧的文件名/目录名:"<<endl;
  cin>>lastname;
  cout<<"请输入新的文件名/目录名:"<<endl;
  cin>>newname;
  if(!rename(lastname,newname))
  {
    cout<<"重命名失败啦哦!"<<endl;
  }
  else{
    cout<<"耶!重命名成功啦呀~"<<endl;
  }
}
else if(s=="7")
{//复制
  menu();
  string existname,newname;
  cout<<"请输入一个已存在的文件名或目录名:"<<endl;
  cin>>existname;
  cout<<"请输入一个新的文件名或目录名(用来存放复制的数据):"<<endl;
  cin>>newname;
  if(!copy(existname,newname))
  {
    cout<<"啊哦,复制失败啦耶!"<<endl;
  }
  else
  {
    cout<<"复制成功啦呀!"<<endl;
  }
}
else if(s=="8")
{//查找
  menu();
  string dir,filename;
  cout<<"请输入目录名:"<<endl;
  cin>>dir;
  cout<<"请输入目录名或文件名:"<<endl;
  cin>>filename;
  if(!find(dir,filename))
  {
    cout<<"查找失败!"<<endl;
  }
}
else if(s=="9")
{
//列出目录中全部文件和目录
  menu();
  string dirpath;
  cout<<"请输入需要遍列的目录名:"<<endl;
  cin>>dirpath;
  list(dirpath);
  //getFileName(*dirpath);
  //myls();
}
else if(s=="10")
{
//显示与设置时间与日期
  menu();
  int choose=0;
  cout<<"选择显示时间请输入1,选择设置时间请输入2"<<endl;
  cout<<"-----1 显示,2 设置."<<endl;
  cin>>choose;
  if(choose=1)
  {
     datetime();
  }
  if(choose=2)
  {
    int y,mon,d,h,m,s;
    cout<<"请依次输入'年 月 日 时 分 秒'"<<endl;
    cin>>y>>mon>>d>>h>>m>>s;
    TimeSet(y,mon,d,h,m,s);
    cout<<"设置成功"<<endl;
  }
  else
  {
    cout<<"只能输入‘1’或‘2’啦!不要输入其他的哦"<<endl;
  }
}
else if(s=="11")
{
//切换目录
  menu();
  cd();
//cout<<"请输入要切换的目录名"<<endl;
//cin>>dir;
}
  else
  {
    menu();
    cout<<"输入出错啦呀,请再输入一次哦!"<<endl;
  }
}
  return 0;
}
相关实践学习
CentOS 7迁移Anolis OS 7
龙蜥操作系统Anolis OS的体验。Anolis OS 7生态上和依赖管理上保持跟CentOS 7.x兼容,一键式迁移脚本centos2anolis.py。本文为您介绍如何通过AOMS迁移工具实现CentOS 7.x到Anolis OS 7的迁移。
相关文章
|
4天前
|
Linux 数据安全/隐私保护 Windows
命令方式:window向linux传文件
【10月更文挑战第6天】本文介绍了如何在Linux系统中通过命令`ip a`获取IP地址,并在Windows系统下使用CMD命令行工具和SCP命令实现文件传输。示例展示了如何将D盘中的`mm.jar`文件上传至IP地址为192.168.163.122的Linux系统的/up/目录下,最后在Linux系统中确认文件传输结果。
118 65
|
4天前
|
Web App开发 网络协议 Linux
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
这篇文章是关于Linux命令的总结,涵盖了从基础操作到网络配置等多个方面的命令及其使用方法。
24 1
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
|
6天前
|
监控 安全 网络协议
|
2天前
|
存储 安全 Linux
Linux文件管理命令md5sum awk
通过结合 `md5sum`和 `awk`,不仅可以高效地进行文件完整性校验,还能灵活地处理和分析校验结果,为系统管理、数据审计等工作提供强大的支持。
10 2
|
3天前
|
Unix 物联网 大数据
操作系统的演化与比较:从Unix到Linux
本文将探讨操作系统的历史发展,重点关注Unix和Linux两个主要的操作系统分支。通过分析它们的起源、设计哲学、技术特点以及在现代计算中的影响,我们可以更好地理解操作系统在计算机科学中的核心地位及其未来发展趋势。
|
3天前
|
运维 Java Linux
Linux 下命令后台运行秘籍:无惧终端断开的魔法
本文详细介绍了在 Linux 系统下使命令不受终端断开影响、持续在后台运行的多种方法及其原理。包括使用 `nohup`、`setsid`、括号括起来、作业调度和 `screen` 等技巧,帮助读者提高工作效率,确保任务不被意外中断。
12 0
Linux 下命令后台运行秘籍:无惧终端断开的魔法
|
9天前
|
应用服务中间件 Linux nginx
Linux下操作Nginx相关命令
Linux下操作Nginx相关命令
|
9天前
|
关系型数据库 MySQL Unix
【Linux】常用的命令
【Linux】常用的命令
13 1
|
2天前
|
Linux
linux/mac 下查看、修改文件权限的命令
这篇文章介绍了在Linux和Mac操作系统下如何查看和修改文件及文件夹的权限。
|
5天前
|
Linux 网络安全 vr&ar
Linux 命令大全
10月更文挑战第5天
12 0