導航:首頁 > 操作系統 > 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當前目錄相關的資料

熱點內容
web前端後端程序員 瀏覽:24
萬能zip的壓縮包怎麼解壓 瀏覽:40
國內動漫用什麼app看 瀏覽:353
樹莓派高級編程 瀏覽:928
30歲學編程晚嗎 瀏覽:68
解壓專家怎麼打開 瀏覽:86
php開源留言板 瀏覽:49
新鄉市區疫情怎麼查詢app 瀏覽:158
我的世界伺服器怎麼弄圖 瀏覽:999
vc6的編譯框 瀏覽:198
程序員寫照 瀏覽:539
怎麼退出github伺服器版本 瀏覽:797
雲伺服器sip 瀏覽:910
對稱平衡型壓縮機 瀏覽:953
rust連接什麼伺服器 瀏覽:382
php刪除數組的空元素 瀏覽:74
有什麼古今翻譯的app 瀏覽:54
華為平板里的app熱門推薦怎麼關閉 瀏覽:731
kindle可以看pdf嗎 瀏覽:620
小米文件夾變小 瀏覽:324