Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete a file in a directory with c language in Linux

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Linux how to use the c language to delete files in a directory of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone in this Linux how to use the c language to delete files in a directory of the article will have a harvest, let's take a look.

Delete the files in the directory by using c language

Recently, the work is about the simple file operation under linux. I am not very familiar with the file operation functions under linux system before. After this practice, I have a certain understanding of the use of these functions.

I think you should be familiar with how to create files, read and write files. What I am introducing is how to traverse a directory and delete files in that directory (you can specify a suffix name), and you can also specify

The modification time range of the file (how many hours ago the old file can be deleted). Here is a simple function implementation for beginners' reference only (after all, I am also a beginner\ (^ o^) / ~)

# include # define file_max_len 256 void rmv_old_files (const char * path, const char * suf, int hours) {char filename [file _ max_len] = {0}; struct tm * tm; struct dirent * dirp; struct stat statbuf; dir * dp = null; time_t curr_time; int namelen, offset Char * chtemp = null; curr_time = time ((time_t*) null); dp = opendir (path); if (null = = dp) {return } while ((dirp=readdir (dp))! = null) {if (strcmp (dirp- > d_name, ".") = 0 | strcmp (dirp- > d_name, "..") = = 0) {continue;} namelen = strlen (dirp- > d_name) Chtemp = dirp- > if name; if (* suf! ='\ 0') {offset = namelen-strlen (suf); if (offsetd_name) If (! stat (filename, & statbuf)) {/ * check the st_mtime of the file If more than retention_hours ago then delete it*/ if (curr_time-statbuf.st_mtime > = hours*3600 & & s_isreg (statbuf.st_mode)) {unlink (filename) } closedir (dp);} attachment: linux deletes files under the specified directory command rm-f to specify the directory *

# the most classic way to delete all types of files in the specified directory

2.find specified directory-type f-delete or find specified directory-type f-exec rm-f {}\

# use the find command to find all ordinary files in the specified directory and delete the or. Delete them with the processing action of the find command

3.find specifies the directory-type f | xargs rm-f

# too long parameter list; too many files to delete

4.rm-f `find specify directory-type f`

# Delete all ordinary files in the specified directory

5.for delete in `ls-l specifies the directory path`; do rm-f *; done

# use for loop statement to delete all types of files in the specified directory

This is the end of the article on "how to delete files in a directory with C language in Linux". Thank you for reading! I believe that everyone has a certain understanding of "how to delete files under a directory in Linux with c language". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report