linux 文件夹遍历,linux查看已安装的软件包
很多朋友对于linux 文件夹遍历和linux查看已安装的软件包不太懂,今天就由小编来为大家分享,希望可以帮助到大家,下面一起来看看吧!
Linux内核遍历文件夹下的内容
本文旨在探讨如何在Linux内核中遍历文件夹获取所需内容。主要关注于一个具体需求:在内核模块中获取所有nvme字符设备的名称。
经过研究,存在两个主要方案。方案一:通过文件系统查找。在Linux中,/dev目录下包含所有设备节点,且/sys/class/nvme目录集中存放NVMe设备信息。方案二:遍历PCI设备。NVMe设备挂载在PCI总线上,故可通过此总线遍历所有设备类别。
最终选择方案一。方案二尝试失败,因为NVMe设备名称存储在struct nvme_dev中,但定义在pci.c文件中,无法直接获取。
转而采用方案一,借助内核提供的iterate_dir函数。通过定义struct dir_context并设置执行策略,可实现遍历并获取所需文件名,后续再统一处理。代码实现简洁高效。
需注意,遍历文件夹操作通常在用户空间进行,内核空间执行此类操作可能导致问题。但本例中仅需获取文件夹名称,不对文件内容进行修改,故影响不大。
C/C++编程遍历文件夹,统计当前文件个数,输出文件名
标准C是没有目录相关的函数的
CFree是16位的吧,那就更不用想了.貌似只能内嵌汇编使用dos中断来完成.
还是换编译器吧devcpp codeblock vc8之类的都很好
【cmail】:
这个要用到windows API
HANDLE FindFirstFile(
LPCTSTR lpFileName,
LPWIN32_FIND_DATA lpFindFileData
);
BOOL FindNextFile(
HANDLE hFindFile,
LPWIN32_FIND_DATA lpFindFileData
);
WIN32_FIND_DATA
【CHROX】:
在Win32平台下不用windows api,有好多功能实现起来都很费劲,特别是系统相关的
再说用api又不是什么丢人的事。开发可移植程序除外。
用FindFirstFile和FindNextFile两个api很容易实现
////////////////////////////////////////////////
void FindFile(LPCTSTR lpszPath){
TCHAR szFind[MAX_PATH];
lstrcpy(szFind,lpszPath);
if(!IsRoot(szFind)) lstrcat(szFind,"\\");
lstrcat(szFind,"*.*");
WIN32_FIND_DATA wfd;
HANDLE hFind=FindFirstFile(szFind,&wfd);
if(hFind==INVALID_HANDLE_VALUE) return;
do{
if(lstrcmp(wfd.cFileName,".")==0||lstrcmp(wfd.cFileName,"..")==0) continue;
char szFile[MAX_PATH];
lstrcpy(szFile,lpszPath);
if(!IsRoot(szFile)) lstrcat(szFile,"\\");
lstrcat(szFile,wfd.cFileName);
if((GetFileAttributes(szFile)&FILE_ATTRIBUTE_DIRECTORY)==FILE_ATTRIBUTE_DIRECTORY){
FindFile(szFile);//递归
}
else{
}//Do your things
}
} while(FindNextFile(hFind,&wfd));
CloseHandle(hFind);
}
【Geomatic】:
原来FindFirstFile和FindNextFile是WINDOWS的API我晕我以为是C++的函数库里的东西呢
我明白了我找到的代码和CHROX的差不多
#include<stdio.h>
#include<windows.h>
BOOL IsRoot(LPCTSTR lpszPath)
{
TCHAR szRoot[4];
wsprintf(szRoot,"%c:\\", lpszPath[0]);
return(lstrcmp(szRoot, lpszPath)== 0);
}
void FindInAll(::LPCTSTR lpszPath)
{TCHAR szFind[MAX_PATH];<br>lstrcpy(szFind, lpszPath);<br>if(!IsRoot(szFind))<br>lstrcat(szFind,"\\");<br>lstrcat(szFind,"*.*");//找所有文件<br>WIN32_FIND_DATA wfd;<br>HANDLE hFind= FindFirstFile(szFind,&wfd);<br>if(hFind== INVALID_HANDLE_VALUE)//如果没有找到或查找失败<br>return;<br><br>do<br>{<br>if(wfd.cFileName[0]=='.')<br>continue;//过滤这两个目录<br>if(wfd.dwFileAttributes& FILE_ATTRIBUTE_DIRECTORY)<br>{<br>TCHAR szFile[MAX_PATH];<br>if(IsRoot(lpszPath))<br>wsprintf(szFile,"%s%s", lpszPath, wfd.cFileName);<br>else<br>wsprintf(szFile,"%s\\%s", lpszPath, wfd.cFileName);<br>FindInAll(szFile);//如果找到的是目录,则进入此目录进行递归<br>}
else
{
TCHAR szFile[MAX_PATH];
if(IsRoot(lpszPath))
wsprintf(szFile,"%s%s", lpszPath, wfd.cFileName);
else
wsprintf(szFile,"%s\\%s", lpszPath, wfd.cFileName);
printf("%s\n",szFile);
//对文件进行操作
}
} while(FindNextFile(hFind,&wfd));
FindClose(hFind);//关闭查找句柄
}
int main(int argc, char* argv[])
{
FindInAll("g://music");
return 0;
}
谢谢大家的帮助
【Geomatic】:
这个怎么给你们给分啊
那个分有用吗
刚申请的好多功能不会用啊
【cmail】:
点上面的“管理”。
【CHROX】:
分可是个好东西。呵呵,你以后就明白了。
【jixingzhong】:
DEV C++,利用链表实现目录内所有文件列表显示
#include<stdio.h>
#include<dirent.h>
/*#include<alloc.h>*/
#include<string.h>
void main(int argc,char*argv[])
{
DIR*directory_pointer;
struct dirent*entry;
struct FileList
{
char filename[64];
struct FileList*next;
}start,*node;
if(argc!=2)
{
printf("Must specify a directory\n");
exit(1);
}
if((directory_pointer=opendir(argv[1]))==NULL)
printf("Error opening%s\n",argv[1]);
else
{
start.next=NULL;
node=&start;
while((entry=readdir(directory_pointer))!=NULL)
{
node->next=(struct FileList*)malloc(sizeof(struct FileList));
node=node->next;
strcpy(node->filename,entry->d_name);
node->next=NULL;
}
closedir(directory_pointer);
node=start.next;
while(node)
{
printf("%s\n",node->filename);
node=node->next;
}
}
}
【jixingzhong】:
linux下面的,作者不是我
A Demo written by camelrain
/*
the program find a file from current directory or your defined directory
commond optinon [path] filename
*/
#include<sys/types.h>
#include<sys/stat.h>
#include<dirent.h>
#include<pwd.h>
#include<unistd.h>
#include<stdio.h>
#include<string.h>
#define LENGTH 256
/* just if is a directory*/
static int IsDir(char* name);
/* search target file, arg1 is current path, arg2 is target file*/
static void search_file(char* path, char* name);
static int search_flag=0;
/*just if is a directory*/
static int IsDir(char* name){
struct stat buff;
if(lstat(name,&buff)<0)
return 0;//if not exist name,ignore
/*if is directory return 1,else return 0*/
return S_ISDIR(buff.st_mode);
}
/*search target file*/
static void search_file(char* path, char* name){
DIR*directory;
struct dirent* dir_entry;
char buffer[LENGTH];
if((directory=opendir(path))== NULL){
fprintf(stderr,"%s", path);
perror("");
return;
}
while(dir_entry=readdir(directory)){
if(!strcmp(dir_entry->d_name,".")||!strcmp(dir_entry->d_name,"..")){
/* do nothing*/
}
else{
/* if is boot directory add"/"*/
if((strcmp(path,"/"))==0)
sprintf(buffer,"%s%s",path,dir_entry->d_name);
/* if is not boot directory do not add"/"*/
else
sprintf(buffer,"%s/%s",path,dir_entry->d_name);
//printf("Now file:%s\n",buffer);
if(IsDir(buffer))
search_file(buffer, name);
else{
if(strcmp(dir_entry->d_name,name)==0)
{
printf("%s\n",buffer);
search_flag=1;
}
}
}
}
closedir(directory);
}
int main(int argc, char*argv[])
{
static char* current_dir;
static char* filename;
int length;
if(argc==1){
printf("A few parameters!!\n");
return 0;
}
if(argc==2){
current_dir=(char*)getcwd(current_dir,LENGTH);
filename=argv[1];
}
if(argc==3){
length=strlen(argv[1]);
if(length>1&&(argv[1][length-1]=='/')){
argv[1][length-1]='\0';
//printf("%d\n",strlen(argv[1]));
}
current_dir=argv[1];
filename=argv[2];
}
search_file(current_dir,filename);
if(!search_flag)
printf("Not found this(%s) file!\n",filename);
return 0;
}
【jixingzhong】:
VC下的:
long handle;
struct _finddata_t filestruct;
char path_search[_MAX_PATH];
handle= _findfirst("目录",&filestruct);
if((handle==-1)) return;
if(::GetFileAttributes(filestruct.name)& FILE_ATTRIBUTE_DIRECTORY)
{
if( filestruct.name[0]!='.')
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir("..");
}
}
else
{
if(!stricmp(filestruct.name, szFilename))
{
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
while(!(_findnext(handle,&filestruct)))
{
if(::GetFileAttributes(filestruct.name)&FILE_ATTRIBUTE_DIRECTORY)
{
if(*filestruct.name!='.')
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir("..");
}
else
{
if(!stricmp(filestruct.name,szFilename))
{
_getcwd(path_search,_MAX_PATH);
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
}
_findclose(handle);
}
linux遍历文件夹所有文件内容linux遍历文件
Linux获取文件名称和文件路径并写入txt?
>自己写的create_filelist.sh文件,如下
1、find命令:
find后跟一个存放想要查找的文件的地址,然后后面是-name参数,其后的参数代表文件名称*.jpg就是代表所有的jpg文件了。
‘>'符号代表输出到文件,此处输出到制定的txt文件中。
2、sed命令:
-i代表直接修改读取的文件内容,而非输出到终端
引号中内容代表替换,$代表在文件最后替换,后面跟了一个空格和数字代表每行字符后加空格和数字,代表文件对应的标签序号。
linux无法读取NTFS文件格式?
CentOS用户在进行ntfs磁盘设备读取的时候,发现这类格式的驱动文件无法读取,且挂载失败,出现这个问题的主要原因是CentOS不支持ntfs格式,下面以CentOS6.4为例,介绍下CentOS6.4读取不了ntfs磁盘设备的解决方法。问题:使用的是双系统Win7和CentOS6.4。在CentOS中无法识别win7中的硬盘。但是manmount的时候,在-t参数中有ntfs的选项。但是挂载的时候总是提示出错。mount-tntfs/dev/sda1/mediamount:unknownfilesystemtype‘ntfs’经过查找得知,CentOS默认是不安装ntfs格式的文件系统的驱动文件。解决方法:我们可以安装一个ntfs-3g来让CentOS支持ntfs格式的文件系统。去下载一个最新的稳定版本的ntfs-3g。下载ntfs-3g_ntfsprogs-2013.1.13.tgz转到下载的位置,#tar-xzfntfs-3g_ntfsprogs-2013.1.13.tgz#cdntfs-3g_ntfsprogs-2013.1.13#。/configure#make#makeinstallntfs-3g但是这个的挂载方法不是,mount-tntfs/dev/sda1/media而是mount-tntfs-3g/dev/sda1/media。感觉有点怪。如果想开机启动自动挂载,可以通过/etc/fstab文件实现。首先通过fdisk-l查看所有的分区信息。DeviceBootStartEndBlocksIdSystem/dev/sda1*1652852436128+7HPFS/NTFS/dev/sda2652916972838914307HPFS/NTFS/dev/sda31697327416838914307HPFS/NTFS/dev/sda4274173891492350464fW95Ext‘d(LBA)/dev/sda52741733791512000007HPFS/NTFS/dev/sda63379134966943718482Linuxswap/Solaris/dev/sda7349663500430720083Linux/dev/sda835004389143140198483Linux发现sda1、sda2、sda3、sda5都是ntfs,你可以根据自己的需要挂载。在/etc/fstal中挂载设备的书写格式如下。格式是设备挂载目录文件格式default00比如我现在要将/dev/sdb1挂载到/media/ntfs我们首先检查/media/ntfs,不存在新建目录,否则将会失败在/etc/fstab中添加下面内容(如果之前存在对应的挂载目录就先备份之前的文件,然后修改对应为。)/dev/sdb1/media/ntfsdefaults00保存文件,重启系统即可。
linux如何提取文件每一行生成新的文件?
通过循环读取文件每一行和重定向符>>来实现提取文件每一行生成新的文件。
unix/linux如何读取一个文件的存储权限?
unix/linux通过命令ls-a来读取一个文件的存储权限,通过命令chown来修改文件的存储权限。
linux文件执行语句内容如何获取?
定向输出到log文件中