导航:首页 > 操作系统 > linuxc当前目录

linuxc当前目录

发布时间:2022-05-09 02:47:29

❶ 谁能用C给写个在linux下,遍历当前目录下的

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);
}

❷ linux c怎么获取进程信息 当前目录

//获取当前进程名(进程目录在函数内已获取到)
boolGetLocalProgramName(char*processname)
{
charprocessdir[1024]={0};
char*path_end;
size_tlen=1024;

boolret=false;

do
{
if(readlink("/proc/self/exe",processdir,len)<=0)
{
fprintf(stderr,"[ERROR]cannotgetprocessname ");
break;
}

path_end=strrchr(processdir,'/');//进程目录
if(path_end==NULL)
{
fprintf(stderr,"[ERROR]cannotparseprocessname ");
break;
}

++path_end;
*path_end='';
strcpy(processname,path_end);

ret=true;
}while(0);

returnret;
}

这是我以前的代码,稍微改造一下就行。

❸ linux命令:浏览当前目录下的所有以“.c”为后缀名的文件是什么

Linux中浏览当前目录下所有以".c"为后缀名的文件,可以使用ls命令,直接执行如下命令:

ls*.c

❹ linux c 删除目录

命令行 rm -rf 文件夹名称

(4)linuxc当前目录扩展阅读:
1、Linux rm命令用于删除一个文件或者目录。
2、语法:rm [options] name...

3、参数:-i 删除前逐一询问确认;-f 即使原档案属性设为唯读,亦直接删除,无需逐一确认;-r 将目录及以下之档案亦逐一删除。
4、实例:删除文件可以直接使用rm命令,若删除目录则必须配合选项"-r",例如:# rm -r homework rm:是否删除 目录 "homework"? y
5、删除当前目录下的所有文件及目录,命令行为:rm -r *
6、文件一旦通过rm命令删除,则无法恢复,所以必须格外小心地使用该命令。

❺ linux 中 -C是什么意思

-c :建立一个压缩文件的参数指令(create 的意思)

首先介绍一个名词“控制台(console)”,它就是我们通常见到的使用字符操作界面的人机接口,例如dos。我们说控制台命令,就是指通过字符界面输入的可以操作系统的命令,例如dos命令就是控制台命令。

我们现在要了解的是基于Linux操作系统的基本控制台命令。有一点一定要注意,和dos命令不同的是,Linux的命令(也包括文件名等等)对大小写是敏感的,也就是说,如果你输入的命令大小写不对的话,系统是不会做出你期望的响应的。

(5)linuxc当前目录扩展阅读

-x :解开一个压缩文件的参数指令!

-t :查看 tarfile 里面的文件!

特别注意,在参数的下达中, c/x/t 仅能存在一个!不可同时存在!

因为不可能同时压缩与解压缩。

-z :是否同时具有 gzip 的属性?亦即是否需要用 gzip 压缩?

-j :是否同时具有 bzip2 的属性?亦即是否需要用 bzip2 压缩?

-v :压缩的过程中显示文件!这个常用,但不建议用在背景执行过程!

❻ linux下怎么用C语言切换用户的当前目录

切换用户当前目录可以有两种理解:

一、切换目录,供程序其它部分使用。
比如有文件存储在A目录中,打开时希望可以直接用fopen文件名即可,而不加上文件所在目录名,那么可以调用chdir实现。
1、函数声明:

int chdir(const char *path);
2、头文件:
stdlib.h
3、使用方法:
chdir(path_name);
传入字符串即要切换的目录名, 如chdir("my_folder");
4、返回值:
成功返回0 ,失败返回-1。
二、要切换调用C可执行程序后的目录。即原本在A目录,调用程序退出后切换到B目录。
这一点无法做到,程序中做的所有更改目录操作,均不会影响到程序退出后所在目录。 这是由系统本身决定的,超出C程序所能实现的范围。

❼ linux c 查看当前目录下是否有指定文件

1. Shell 版本
#获取当前脚本所在绝对路径
cur_dir=$(cd "$(dirname "$0")"; pwd)

2. C语言版本
方法一、用realpath函数。这种方法用于开机启动程序获取自身目录会出错
char current_absolute_path[MAX_SIZE];
//获取当前目录绝对路径
if (NULL == realpath("./", current_absolute_path))
{
printf("***Error***\n");
exit(-1);
}
strcat(current_absolute_path, "/");
printf("current absolute path:%s\n", current_absolute_path);
方法二、用getcwd函数。这种方法用于开机启动程序获取自身目录会出错
char current_absolute_path[MAX_SIZE];
//获取当前目录绝对路径
if (NULL == getcwd(current_absolute_path, MAX_SIZE))
{
printf("***Error***\n");
exit(-1);
}
printf("current absolute path:%s\n", current_absolute_path);

方法三、用readlink函数。这种方法最可靠,可用于开机启动程序获取自身目录
char current_absolute_path[MAX_SIZE];
//获取当前程序绝对路径
int cnt = readlink("/proc/self/exe", current_absolute_path, MAX_SIZE);
if (cnt < 0 || cnt >= MAX_SIZE)
{
printf("***Error***\n");
exit(-1);
}
//获取当前目录绝对路径,即去掉程序名
int i;
for (i = cnt; i >=0; --i)
{
if (current_absolute_path[i] == '/')
{
current_absolute_path[i+1] = '\0';
break;
}
}
printf("current absolute path:%s\n", current_absolute_path);

❽ LINUX C语言编程 修改当前目录下的所有子目录所有者为当前用户

chown -Rh `who` ./
ls -l | tr -s ' ' | cut -d' ' -f 5,9

阅读全文

与linuxc当前目录相关的资料

热点内容
卡通手风琴文件夹 浏览:104
java代码网 浏览:354
推荐解压小游戏app 浏览:80
饥荒联机如何加入服务器 浏览:107
cssjs压缩 浏览:765
程序员剩两个按键的视频 浏览:402
如何解决服务器升级的问题 浏览:618
华为云盘显示服务器异常 浏览:182
java高级编程思想 浏览:389
phpftpmkdir 浏览:674
pdf如何涂改 浏览:21
算法优化实例 浏览:749
新华字典11pdf 浏览:995
苹果怎么登安卓奶块 浏览:808
安卓怎么更新微信809 浏览:336
诛仙游戏服务器名称根据什么来的 浏览:84
hp电脑怎么解压文件 浏览:666
农业银行app扫一扫怎么用 浏览:763
石器时代源码怎么看 浏览:570
python源码剖析kindle 浏览:974