1

I have the following find commands. I would be using python to call these commands and run them. It is a requirement that I run these commands in one go.

find commands

find /path/to/files/ -type f -mtime +3 -exec rm -f {} \;
find /path/to/files2 -type f -mtime +6 -exec rm -f {} \;

When I run them in bash like so, I get an error find: paths must precede expression: `find'

find /path/to/files/ -type f -mtime +3 -exec rm -f {} \; find /path/to/files2 -type f -mtime +6 -exec rm -f {} \;

But when I run it like this I don't get an error.

find /path/to/files/ -type f -mtime +3 -exec rm -f {} \; && find /path/to/files2 -type f -mtime +6 -exec rm -f {} \;

I would like to understand why I don't get an error with &&. And what is the best way to run consecutive find commands.

It is a requirement for me that I should run them like below. I would prefer to know a solution which can accomplish the below, but I'm also open to suggestions.

cmd1 = 'find ... \; '
cmd2 = 'find ... \; '
cmd3 = 'find ... \; '

# This utilises string concatenation
full_cmd = cmd1 + cmd2 + cmd3

# I need to run them in one call.
# It's hard (not impossible) to change this since it is a part of a bigger code base
python_func_which_runs_bash(full_cmd)
Stephen Kitt
  • 434,908

1 Answers1

3

Your command with the && does run the commands consecutively, however the second command only runs if the first exits successfully. You could use ; in place of && if you want the second command to run unconditionally.

Note that the escaped \; doesn't count - it's escaped precisely so that the shell doesn't see it as a conjunction, and instead passes it as an ordinary argument to find.

See also:

steeldriver
  • 81,074