引用文件名是否足以运行`xargs sudo rm -rf`?

时间:2022-02-09 09:23:47

I wrote a script that deletes all except the last two files in a folder:

我写了一个脚本,删除了除文件夹中最后两个文件以外的所有文件:

#!/bin/bash
ls -1 --quoting-style=shell-always /path/to/some/folder \
    | head -n -2 \
    | xargs printf -- "'/path/to/some/folder/%s'\n" \
    | xargs sudo rm -rf

This script will be executed as a cron job every day.

此脚本将每天作为cron作业执行。

The reasoning is as follows:

理由如下:

  1. Obtain a list of all files using ls -1 (so that I get one file per line);

    使用ls -1获取所有文件的列表(这样我每行得到一个文件);

  2. Remove the last two from the list using head -n -2;

    使用head -n -2从列表中删除最后两个;

  3. Since ls prints relative paths, use the xargs printf thing to prepend the folder path and make it an absolute path;

    因为ls打印相对路径,所以使用xargs printf来预先添加文件夹路径并使其成为绝对路径;

  4. Send them to sudo rm -rf using xargs.

    使用xargs将它们发送到sudo rm -rf。

Everyone has access to this folder, so anyone can create and delete any files in this folder.

每个人都可以访问此文件夹,因此任何人都可以创建和删除此文件夹中的任何文件。

The problem is: sudo rm -rf is scary. xargs sudo rm -rf is incredibly scary.

问题是:sudo rm -rf是可怕的。 xargs sudo rm -rf非常可怕。

I want to be sure that no one can damage other folders/systems by creating clever files to be deleted (either accidentally or on purpose). I don't know, something clever like:

我想确保没有人可以通过创建要删除的聪明文件(无论是偶然还是故意)来破坏其他文件夹/系统。我不知道,有点像:

file with / spaces.txt

带/ spaces.txt的文件

which could result in a super scary sudo rm -rf /.

这可能导致一个超级可怕的sudo rm -rf /。

This is why I am using --quoting-style=shell-always, this should prevent any tricks with files with spaces. But now I am wondering if someone could be extra clever with spaces and quotes in the filename, perhaps.

这就是为什么我使用--quoting-style = shell-always,这应该可以防止带有空格的文件的任何技巧。但是现在我想知道是否有人可能会更加聪明地使用文件名中的空格和引号。

Is my script safe?

我的脚本安全吗?


Note: I need sudo because I am acessing the folder remotely (from a mapped network drive using mount), and I couldn't get it to work without sudo.

注意:我需要sudo,因为我正在远程访问文件夹(从使用mount的映射网络驱动器),如果没有sudo,我无法使用它。

1 个解决方案

#1


7  

In Linux, any character is a valid filename constituting character except:

在Linux中,任何字符都是构成字符的有效文件名,除了:

  • \0 (ASCII NUL): as used for string termination in C
  • \ 0(ASCII NUL):用于C中的字符串终止
  • / (forward slash): as used for path separation
  • /(正斜杠):用于路径分离

So, your approach will not definitely work in many cases as you can imagine.

因此,在许多情况下,您的方法肯定不会像您想象的那样有效。

Few notes:

几点说明:

  • Don't parse ls; use dedicated tools (there is at least one for most use cases)
  • 不要解析ls;使用专用工具(大多数用例至少有一个)
  • When dealing with filenames, try to leverage the NUL separated output provided by almost all GNU tools that work with such data
  • 处理文件名时,请尝试利用几乎所有使用此类数据的GNU工具提供的NUL分离输出
  • Take care when piping, make sure both programs can understand NUL separations
  • 管道时要小心,确保两个程序都能理解NUL分离
  • Whenever you're invoking xargs, see if you can get away with find ... -exec; in most cases, you will be fine with just find alone
  • 每当你调用xargs时,看看你是否可以使用find ... -exec;在大多数情况下,单独找到你就可以了

I think these will get you going for now. steeldriver already provided the NUL separated idea in the comment, take a look at that as a starting point.

我想这些会让你现在开始。 steeldriver已经在评论中提供了NUL分离的想法,看看它作为一个起点。

#1


7  

In Linux, any character is a valid filename constituting character except:

在Linux中,任何字符都是构成字符的有效文件名,除了:

  • \0 (ASCII NUL): as used for string termination in C
  • \ 0(ASCII NUL):用于C中的字符串终止
  • / (forward slash): as used for path separation
  • /(正斜杠):用于路径分离

So, your approach will not definitely work in many cases as you can imagine.

因此,在许多情况下,您的方法肯定不会像您想象的那样有效。

Few notes:

几点说明:

  • Don't parse ls; use dedicated tools (there is at least one for most use cases)
  • 不要解析ls;使用专用工具(大多数用例至少有一个)
  • When dealing with filenames, try to leverage the NUL separated output provided by almost all GNU tools that work with such data
  • 处理文件名时,请尝试利用几乎所有使用此类数据的GNU工具提供的NUL分离输出
  • Take care when piping, make sure both programs can understand NUL separations
  • 管道时要小心,确保两个程序都能理解NUL分离
  • Whenever you're invoking xargs, see if you can get away with find ... -exec; in most cases, you will be fine with just find alone
  • 每当你调用xargs时,看看你是否可以使用find ... -exec;在大多数情况下,单独找到你就可以了

I think these will get you going for now. steeldriver already provided the NUL separated idea in the comment, take a look at that as a starting point.

我想这些会让你现在开始。 steeldriver已经在评论中提供了NUL分离的想法,看看它作为一个起点。