0

I am trying to get the largest 20 files in a directory that are older than 60 days by passing the unix 'find' command to Python's subprocess.Popen. This is what I have tried:

# Get largest files older than 60 days
cmd_string = 'find {0} -type f -mtime +60 -size +300M -exec du -sh {{}} \; | sort -rh | head -n20'.format(rmnt)
print(cmd_string)
cmd_args = shlex.split(cmd_string, posix=False)
print(cmd_args[12])
find_out = subprocess.Popen(cmd_args, stdout=subprocess.PIPE).stdout

where rmnt is a directory name (eg. '/mnt/active'). The print statements return the command correctly:

find /mnt/active -type f -mtime +60 -size +300M -exec du -sh {} \; | sort -rh | head -n20
\;

But I am getting this error:

find: missing argument to `-exec'

I thought that the problem was due to the special character "\" but it is being printed as expected.

Update 1.

I found this similar question: find: missing argument to `-exec' when using subprocess

It deals with subprocess.call rather than subprocess.Popen but the answer that "there is no shell to interpret and remove that backslash" seems to apply here also. However, with the backslash removed I still get an error:

find: paths must precede expression: |

This error suggests that the backslash is not being passed to the find command

4

1 回答 1

0

对于任何有类似问题的人,我的代码中有两个问题。

1.

默认情况下,subprocess.Popen 使用shell=False,因此没有 shell 来解释和删除反斜杠,如下所述:find: missing argument to `-exec' when using subprocess

2.

要将管道与子流程模块一起使用,您必须通过shell=True这是一个安全隐患。因此,应将命令分成 2 个或更多命令,如下所述:如何使用带有管道的 `subprocess` 命令

对我有用的代码是:

# Get largest files older than 60 days
find_cmd = 'find {0} -type f -mtime +60 -size +300M -exec du -sh {{}} ;'.format(rmnt)
find_args = shlex.split(find_cmd)
sort_cmd = 'sort -rh'
sort_args = shlex.split(sort_cmd)

find_process = subprocess.Popen(find_args, stdout=subprocess.PIPE)
sort_process = subprocess.Popen(sort_args, stdin=find_process.stdout,stdout=subprocess.PIPE)
find_process.stdout.close()
sort_out = sort_process.stdout
于 2021-03-31T09:12:17.467 回答