How can I execute multiple find commands in a single line?

scientific_explorer

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)
steeldriver

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:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Execute combine multiple Linux commands in one line

From Dev

How can I run multiple commands at once from windows command line using c++ / c?

From Dev

How can I get Ansible to execute multiple commands using with_items?

From Dev

Execute three commands in single command line in Linux

From Dev

Multiple commands on a single line in Linux

From Dev

How can I run multiple commands which have & in one command line?

From Dev

How can i execute multiple commands with JAVA code?

From Dev

How can I do the `history` command and not have line numbers so I can copy multiple line commands?

From Dev

How can I run multiple commands which have & in one command line?

From Dev

How can I run multiple commands at once from windows command line using c++ / c?

From Dev

How can I send stdout to multiple commands?

From Dev

How can I get Ansible to execute multiple commands using with_items?

From Dev

How can I implement pipe for multiple commands?

From Dev

How do I execute multiple commands when using find?

From Dev

How can I execute commands on startup in awesome?

From Dev

Execute multiple commands with 1 line in Windows commandline?

From Dev

How do I execute multiple commands using the same argument?

From Dev

Can I execute multiple lines sudo commands?

From Dev

How can I ask icinga2 from the command line to execute a single-time check of everything?

From Dev

How do i execute multiple commands sequentially on the command line?

From Dev

How can I find & replace a single word to multiple different words?

From Dev

How can i execute awk script with find command on multiple files

From Dev

How can I write a single command line that launches a new docker container with interactive bash and executes a few commands in it?

From Dev

How do I execute FTP commands on one line?

From Dev

How can I send multiple commands' output to a single shell pipeline?

From Dev

How to execute the multiple commands with && operator in new lines(not all in single line) :shell script

From Dev

How can I execute commands in a text file?

From Dev

How can I simplify multiple update SQL commands as a single SQL command?

From Dev

Can I execute two commands in one line in batch?

Related Related

  1. 1

    Execute combine multiple Linux commands in one line

  2. 2

    How can I run multiple commands at once from windows command line using c++ / c?

  3. 3

    How can I get Ansible to execute multiple commands using with_items?

  4. 4

    Execute three commands in single command line in Linux

  5. 5

    Multiple commands on a single line in Linux

  6. 6

    How can I run multiple commands which have & in one command line?

  7. 7

    How can i execute multiple commands with JAVA code?

  8. 8

    How can I do the `history` command and not have line numbers so I can copy multiple line commands?

  9. 9

    How can I run multiple commands which have & in one command line?

  10. 10

    How can I run multiple commands at once from windows command line using c++ / c?

  11. 11

    How can I send stdout to multiple commands?

  12. 12

    How can I get Ansible to execute multiple commands using with_items?

  13. 13

    How can I implement pipe for multiple commands?

  14. 14

    How do I execute multiple commands when using find?

  15. 15

    How can I execute commands on startup in awesome?

  16. 16

    Execute multiple commands with 1 line in Windows commandline?

  17. 17

    How do I execute multiple commands using the same argument?

  18. 18

    Can I execute multiple lines sudo commands?

  19. 19

    How can I ask icinga2 from the command line to execute a single-time check of everything?

  20. 20

    How do i execute multiple commands sequentially on the command line?

  21. 21

    How can I find & replace a single word to multiple different words?

  22. 22

    How can i execute awk script with find command on multiple files

  23. 23

    How can I write a single command line that launches a new docker container with interactive bash and executes a few commands in it?

  24. 24

    How do I execute FTP commands on one line?

  25. 25

    How can I send multiple commands' output to a single shell pipeline?

  26. 26

    How to execute the multiple commands with && operator in new lines(not all in single line) :shell script

  27. 27

    How can I execute commands in a text file?

  28. 28

    How can I simplify multiple update SQL commands as a single SQL command?

  29. 29

    Can I execute two commands in one line in batch?

HotTag

Archive