Linux命令rm:删除文件/文件夹

一 命令作用

命令rm(remove)用于删除文件/文件夹。

二 命令格式

rm [选项] 参数1……参数n

选项可由可无,参数至少为1个。

2.1 选项

r:用于删除文件夹

f:force的缩写,表示强制删除。普通用户删除内容不会弹出提示,只有管理员用户才会有提示。

2.2 参数

表示要删除的文件或文件夹路径,可以是绝对路径或相对路径。多个参数时,每个参数用空格隔开。会一次性删除所有参数路径的文件或文件夹。

rm命令的参数支持通配符 *,用于模糊匹配。

a*:表示所有以a开头的文件或文件夹;

*a:表示所有以a结尾的文件或文件夹;

*a*:表示所有包含a的文件或文件夹。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
很抱歉,作为一个语言模型,我无法提供完整的代码实现。但我可以给你一些代码框架和伪代码来帮助你开始实现一个基本的内存文件系统。 首先,你需要定义一个内存结构体来存储文件系统的超级块、inode 表和数据块。以下是一个可能的结构体定义: ```c struct mrfs_sb_info { unsigned long s_blocksize; /* 块大小 */ unsigned long s_blocksize_bits; /* 块大小的位数 */ unsigned long s_inodes_count; /* inode 数量 */ unsigned long s_blocks_count; /* 块数量 */ unsigned long s_free_blocks_count; /* 空闲块数量 */ unsigned long s_free_inodes_count; /* 空闲 inode 数量 */ }; struct mrfs_inode { unsigned short i_mode; /* 文件类型和权限 */ unsigned short i_uid; /* 用户 ID */ unsigned short i_gid; /* 组 ID */ unsigned short i_links_count; /* 文件链接数量 */ unsigned long i_size; /* 文件大小 */ unsigned long i_blocks; /* 文件占用的块数量 */ unsigned long i_data[15]; /* 文件数据块地址 */ }; struct mrfs_dir_entry { unsigned long inode; /* inode 号 */ char name[NAME_MAX]; /* 文件名 */ }; ``` 接下来,你需要实现文件系统注册/注销函数,用于在系统中注册或注销文件系统类型。以下是一个可能的实现: ```c static struct file_system_type mrfs_fs_type = { .name = "mrfs", .fs_flags = 0, .mount = mrfs_mount, .kill_sb = mrfs_kill_sb, }; int mrfs_init(void) { int ret; ret = register_filesystem(&mrfs_fs_type); if (ret != 0) { printk(KERN_ERR "failed to register mrfs: %d\n", ret); return ret; } return 0; } void mrfs_exit(void) { unregister_filesystem(&mrfs_fs_type); } ``` 然后,你需要实现 ls、cd、mv、touch、mkdir、rmrmdir、read、write 和 exit 等命令的函数。以下是一些可能的伪代码: ```c int mrfs_ls(struct file *file, void *dirent, filldir_t filldir) { struct mrfs_inode *inode = file->f_path.dentry->d_inode; struct mrfs_dir_entry *de; int i; if (!S_ISDIR(inode->i_mode)) { return -ENOTDIR; } for (i = 0; i < inode->i_blocks; i++) { de = (struct mrfs_dir_entry *) mrfs_get_block(inode->i_data[i]); while (de->inode != 0) { filldir(dirent, de->name, strlen(de->name), 0, de->inode, DT_UNKNOWN); de++; } } return 0; } int mrfs_cd(struct file *file, const char __user *buf, size_t len) { struct inode *old_inode = file->f_path.dentry->d_inode; struct inode *new_inode; char *path; int err; path = memdup_user(buf, len); if (IS_ERR(path)) { return PTR_ERR(path); } new_inode = mrfs_find_inode(old_inode, path); if (new_inode == NULL) { err = -ENOENT; goto out; } if (!S_ISDIR(new_inode->i_mode)) { err = -ENOTDIR; goto out_iput; } file->f_path.dentry = d_obtain_alias(d_find_any_alias(new_inode)); err = 0; out_iput: iput(new_inode); out: kfree(path); return err; } int mrfs_mv(struct file *file, const char __user *old, size_t old_len, const char __user *new, size_t new_len) { struct inode *old_inode = file->f_path.dentry->d_inode; struct inode *new_inode; char *old_path, *new_path; int err; old_path = memdup_user(old, old_len); if (IS_ERR(old_path)) { return PTR_ERR(old_path); } new_path = memdup_user(new, new_len); if (IS_ERR(new_path)) { err = PTR_ERR(new_path); goto out_old_path; } new_inode = mrfs_find_inode(old_inode, new_path); if (new_inode != NULL) { err = -EEXIST; goto out_new_path; } new_inode = mrfs_find_inode(old_inode, old_path); if (new_inode == NULL) { err = -ENOENT; goto out_new_path; } if (S_ISDIR(new_inode->i_mode)) { err = -EISDIR; goto out_iput; } err = mrfs_rename(old_inode, old_path, new_inode, new_path); out_iput: iput(new_inode); out_new_path: kfree(new_path); out_old_path: kfree(old_path); return err; } int mrfs_touch(struct file *file, const char __user *name, size_t len) { struct inode *inode = file->f_path.dentry->d_inode; char *path; int err; path = memdup_user(name, len); if (IS_ERR(path)) { return PTR_ERR(path); } err = mrfs_create(inode, path, S_IFREG | S_IRUSR | S_IWUSR); if (err < 0) { goto out; } err = mrfs_open(file, path, len, O_WRONLY | O_TRUNC); if (err < 0) { goto out_unlink; } return 0; out_unlink: mrfs_unlink(inode, path); out: kfree(path); return err; } int mrfs_mkdir(struct file *file, const char __user *name, size_t len) { struct inode *inode = file->f_path.dentry->d_inode; char *path; int err; path = memdup_user(name, len); if (IS_ERR(path)) { return PTR_ERR(path); } err = mrfs_create(inode, path, S_IFDIR | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); if (err < 0) { goto out; } return 0; out: kfree(path); return err; } int mrfs_rm(struct file *file, const char __user *name, size_t len) { struct inode *inode = file->f_path.dentry->d_inode; char *path; int err; path = memdup_user(name, len); if (IS_ERR(path)) { return PTR_ERR(path); } err = mrfs_unlink(inode, path); if (err < 0) { goto out; } return 0; out: kfree(path); return err; } int mrfs_rmdir(struct file *file, const char __user *name, size_t len) { struct inode *inode = file->f_path.dentry->d_inode; char *path; int err; path = memdup_user(name, len); if (IS_ERR(path)) { return PTR_ERR(path); } err = mrfs_rmdir(inode, path); if (err < 0) { goto out; } return 0; out: kfree(path); return err; } ssize_t mrfs_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { struct inode *inode = file->f_path.dentry->d_inode; char *data; ssize_t err; if (!S_ISREG(inode->i_mode)) { return -EINVAL; } data = mrfs_get_data(inode, *ppos, count); if (IS_ERR(data)) { return PTR_ERR(data); } err = simple_read_from_buffer(buf, count, ppos, data, inode->i_size); kfree(data); return err; } ssize_t mrfs_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct inode *inode = file->f_path.dentry->d_inode; char *data; ssize_t err; if (!S_ISREG(inode->i_mode)) { return -EINVAL; } data = mrfs_get_data(inode, *ppos, count); if (IS_ERR(data)) { return PTR_ERR(data); } err = simple_write_to_buffer(data, count, ppos, buf, count); mrfs_update_inode(inode); kfree(data); return err; } int mrfs_exit(struct super_block *sb) { struct mrfs_sb_info *sbi = MRFS_SB(sb); struct mrfs_inode *inode; int i; /* 释放 inode 表和数据块 */ for (i = 0; i < sbi->s_inodes_count; i++) { inode = mrfs_get_inode(sb, i); if (inode != NULL) { mrfs_free_inode(inode); } } for (i = 0; i < sbi->s_blocks_count; i++) { mrfs_free_block(sbi->s_blocks[i]); } kfree(sbi); return 0; } ``` 最后,你需要实现一个文件系统的驱动程序,用于将文件系统挂载到系统中。以下是一个可能的实现: ```c struct dentry *mrfs_mount(struct file_system_type *fs_type, int flags, const char *dev_name, void *data) { struct dentry *root; struct super_block *sb; struct mrfs_sb_info *sbi; struct mrfs_inode *root_inode; int err; /* 创建超级块 */ sb = sget(fs_type, NULL, mrfs_fill_super, NULL); if (IS_ERR(sb)) { return ERR_CAST(sb); } /* 创建根目录 */ root_inode = mrfs_alloc_inode(sb); if (root_inode == NULL) { err = -ENOMEM; goto out; } root_inode->i_mode = S_IFDIR | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; root_inode->i_uid = current_fsuid(); root_inode->i_gid = current_fsgid(); err = mrfs_add_entry(root_inode, ".", root_inode); if (err < 0) { goto out_free_inode; } err = mrfs_add_entry(root_inode, "..", root_inode); if (err < 0) { goto out_free_inode; } root = d_make_root(d_alloc_root(root_inode)); if (root == NULL) { err = -ENOMEM; goto out_free_inode; } return root; out_free_inode: mrfs_free_inode(root_inode); out: deactivate_locked_super(sb); return ERR_PTR(err); } void mrfs_kill_sb(struct super_block *sb) { kill_litter_super(sb); } ``` 以上是一个基本的实现框架,具体的实现方法需要根据你的具体情况进行调整和优化。希望这些信息可以对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洪恒远

感君意气无所惜,一为歌行歌主客

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值