Exclude a file when deleting files in Linux programming

Guided reading

Sometimes we need to exclude some files when deleting all files in a certain folder under Linux.

For example, the following is a real user case:

  • When the user restores the default settings in the embedded device, some complicated configurations are not allowed to be restored (that is, some files are preserved);

experiment

For example we have test/a.txt b.txt c.txt d.txt e.txtand test/config/setting.inithese configuration files, all have different roles.

Suppose we need to preserve test/c.txtand test/config/setting.iniconfigure the configuration information in these two configuration files.

1. Create a file for the experiment

[root@TrueDei test]# touch a.txt b.txt c.txt d.txt e.txt
[root@TrueDei test]# 
[root@TrueDei test]# ls
a.txt  b.txt  c.txt  d.txt  e.txt
[root@TrueDei test]# 

2. Execute the test command
We can do it in steps

  • 1. First query all the files after discharge
  • 2. Delete according to the query results

You can view all files in a directory directly through lsor command, we found thatfind

[root@TrueDei test]# ls ./*
./a.txt  ./b.txt  ./c.txt  ./d.txt  ./e.txt
[root@TrueDei test]# 
[root@TrueDei test]# find ./*
./a.txt
./b.txt
./c.txt
./d.txt
./e.txt
[root@TrueDei test]# 

Create a secondary directory and try again.
It is also possible to query all files

[root@TrueDei test]# mkdir config
[root@TrueDei test]# touch config/setting.ini
[root@TrueDei test]# 
[root@TrueDei test]# find ./*                
./a.txt
./b.txt
./config
./config/setting.ini
./c.txt
./d.txt
./e.txt
[root@TrueDei test]# 
[root@TrueDei test]# ls ./*                  
./a.txt  ./b.txt  ./c.txt  ./d.txt  ./e.txt

./config:
setting.ini
[root@TrueDei test]# 

查询并排除我们想排除的文件
grepCommands, which should be very common, can filter out the data we want.
If we need to exclude data we don't want, Linux also provides a egrepcommand;

# 查看所有的文件
[root@TrueDei test]# ls ./*
./a.txt  ./b.txt  ./c.txt  ./d.txt  ./e.txt

./config:
setting.ini
[root@TrueDei test]# 

# 排除setting.ini文件
[root@TrueDei test]# ls ./* |egrep -v  '(setting.ini)'
./a.txt
./b.txt
./c.txt
./d.txt
./e.txt

./config:
[root@TrueDei test]# 
[root@TrueDei test]# 

# 排除c.txt和setting.ini文件
[root@TrueDei test]# ls ./* |egrep -v  '(c.txt|setting.ini)'
./a.txt
./b.txt
./d.txt
./e.txt

./config:
[root@TrueDei test]# 
[root@TrueDei test]# 

rmdelete with command

# 我们直接使用rm命令是有时候还询问是否删除等信息。在代码中就不好操作了。很烦人的
[root@TrueDei test]# rm `ls ./* |egrep -v  '(c.txt|setting.ini)'`
rm: remove regular empty file './a.txt'? y
rm: remove regular empty file './b.txt'? ^C

# 我们加上-rf即可
[root@TrueDei test]# rm `ls ./* |egrep -v  '(c.txt|setting.ini)'` -rf
[root@TrueDei test]# 

You can see success.只保留了c.txt和setting.ini

[root@TrueDei test]# ls ./*
./c.txt

./config:
setting.ini
[root@TrueDei test]# 

C language Linux programming implementation

The functions provided by the C language systemcan be operated using shell commands.

system("rm `ls /root/test/* |egrep -v  '(c.txt|setting.ini)'` -rf; /sbin/reboot -f");

Guess you like

Origin blog.csdn.net/qq_17623363/article/details/120543687