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 use custom command del instead of rm command in shell programming practice

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article is about how to use the custom command del instead of the rm command in shell programming. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

I don't know how you feel about using the rm command on the server. Anyway, every time I use it, I will confirm it. Even so, I still feel a little panicked when I press enter. Because it is different from the windows system, using the win system to delete the file by default, the file will be placed in the Recycle Bin. If you want to recover, you can find the file directly from the Recycle Bin and restore it. But in linux, delete files with rm, that is really deleted, there is no concept of the Recycle Bin.

Because of the danger of the rm command, I don't want to write a shell script myself to delete files. When the self-defined command deletes a file, it moves the location of the original file to the Recycle Bin directory, and then deletes the original file. If you delete it by mistake, you can find the file in the Recycle Bin and restore it.

First, first, we need to create a Recycle Bin directory. If you already have this directory, you never have to recreate it.

# Recycle Bin directory DEL_BAK_DIR= "/ root/.delbak" if [!-d "$DEL_BAK_DIR"]; then mkdir "$DEL_BAK_DIR" | exit 1fi

Then we get the parameters entered by the user, loop through the parameters, and call the delete function to delete them. Of course, we first need to check whether the user has entered parameters, if the user does not enter parameters, then directly exit the program, do not go down.

# get parameter if (($#)

< 1 ));then echo "Usage ${0} file1 file2 ..." exit 2fi# 删除文件for file in $*do delete $filedone 最后,我们需要编写delete这个函数了。这个函数应该有哪些功能呢?首先,它肯定能对待删除的文件名做检查,如果不是文件或目录,它就不继续往下走了。另外,每次将删除文件放到回收站目录前,应该对回收站目录所在分区剩余空间做检查,如果空间不足也不往下走。 下面是文件是否存在的检查 if [ ! -e $1 ];then echo "file $1 is not exists" exit 3fi 而关于分区空间的检查则稍微复杂些。首先,我们要获取到回收站目录所在分区剩余空间,然后获取到待删除文件的大小,拿这两个数据做对比。 # 分区剩余空间是否充足校验part_free=$(df -k $DEL_BAK_DIR | grep -iv filesystem | awk '{print $4}')file_size=$(du -ks $1 | awk '{print $1}') if (( part_free >

File_size) then # back up and delete the source file. Else echo "The disk size is not enough" exit 4fi

At this point, all the preparations are done, except for the last backup of the source file, and then delete the source file. The complete code is given below:

#! / bin/bash# uses the custom command del instead of the rm command export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:~/binexport LANG=en# Recycle Bin directory DEL_BAK_DIR= "/ root/.delbak" function delete () {# file name whether there is a check if [!-e $1] Then echo "file $1 is not exists" exit 3 fi # Partition part_free=$ (df-k $DEL_BAK_DIR | grep-iv filesystem | awk'{print $4}') file_size=$ (du-ks $1 | awk'{print $1}') if ((part_free > file_size) then # backup and delete the source file Now=$ (date +% Y%m%d%H%M) if [!-d "$DEL_BAK_DIR/.$now"] Then mkdir "$DEL_BAK_DIR/.$now" | | exit 5 fi mv-f $1 $DEL_BAK_DIR/.$now/$ (basename $1) else echo "The disk size is not enough" exit 4 fi} if [!-d "$DEL_BAK_DIR"]; then mkdir "$DEL_BAK_DIR" | exit 1fi# acquisition parameter if (($# < 1)) Then echo "Usage ${0} file1 file2..." Exit 2fi# delete the file for file in $* do delete $filedone Thank you for reading! About shell programming practice how to use custom commands del instead of rm commands to share here, I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

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

Servers

Wechat

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

12
Report