find -exec with multiple commands

xargs multiple commands
xargs multiple arguments
find xargs
xargs
xargs grep
xargs reuse argument
echo one two three mkdir
ubuntu xargs

I am trying to use find -exec with multiple commands without any success. Does anybody know if commands such as the following are possible?

find *.txt -exec echo "$(tail -1 '{}'),$(ls '{}')" \;

Basically, I am trying to print the last line of each txt file in the current directory and print at the end of the line, a comma followed by the filename.

find accepts multiple -exec portions to the command. For example:

find . -name "*.txt" -exec echo {} \; -exec grep banana {} \;

Note that in this case the second command will only run if the first one returns successfully, as mentioned by @Caleb. If you want both commands to run regardless of their success or failure, you could use this construct:

find . -name "*.txt" \( -exec echo {} \; -o -exec true \; \) -exec grep banana {} \;

How to perform multiple commands on a list of files in bash (xargs , csv files to find. Of course, since this uses append to file for the redirect, you'll need to delete the polls file after each use, or it will  Execute multiple commands in a find I am checking that a file is older than a reference file that I build with a touch command before processing it. If it is not old enough, I want to sleep for an hour and check again.

find . -type d -exec sh -c "echo -n {}; echo -n ' x '; echo {}" \;

Running multiple commands for multiple files on one line, Xargs is an extremely powerful complement to the awesome find command. One downside is that you usually need to have a single pipeline. Is it possible to execute multiple commands using exec on the Linux/UNIX find command output ? The answer is "yes". Each -exec action is to be associated with a escaped semi-colon (\;) e.g. I had to find files named "1251936000.log" and then need to perform two actions on it: - Count number of lines in the file. - Do a "ls -l" listing of the file.

One of the following:

find *.txt -exec awk 'END {print $0 "," FILENAME}' {} \;

find *.txt -exec sh -c 'echo "$(tail -n 1 "$1"),$1"' _ {} \;

find *.txt -exec sh -c 'echo "$(sed -n "\$p" "$1"),$1"' _ {} \;

UNIX tip – xargs with multiple commands, for dir in $(find salford_* -type d);do sed -e "1r ${dir}/MenuSettings.bak" -e '1,2d' $​{dir}/MenuSettings.txt >> ${dir}/MenuSettings2.txt sed '/^AppName: */{h;s///;x;};  find . -type f \( -exec command1 \; -false -o -exec command2 \; \) In the second set, the escaped parentheses group the two -exec clauses. The -false between them forces the test state to "false" and the -o causes the next expression (the second -exec ) to be evaluated because of the -false .

Looping through wildcard folders and executing multiple commands , 10 More Discussions You Might Find Interesting Pass Multiple Commands and Open Multiple Xterms via PSS. Hello, I'm attempting to open multiple xterms  I see several references to the ability to execute multiple commands on the results of a single find command. What I am trying to do is use a flag to track which files have been processed by a recurring script. I find the files that do NOT have the flag set, then I process them. Next I want to set the flag.

There's an easier way:

find ... | while read -r file; do
    echo "look at my $file, my $file is amazing";
done

Alternatively:

while read -r file; do
    echo "look at my $file, my $file is amazing";
done <<< "$(find ...)"

Xargs: multiple commands to each argument, I can't get my script to execute multiple commands. Here's the code. It's a menu script. #!/bin/ksh clear print 10 More Discussions You Might Find Interesting  I have 407 shell scripts in several directories. 17 seconds for : find '.sh' -exec grep '^#' '{}' ';' ### but 3 seconds for find '.sh' -print0 | xargs -0 grep '#^' – Vouze Sep 13 '16 at 9:22 2 Exec with + instead of \; to handle multiple parameters performs similarly to xargs; there's no need to conflate the use of exec in the general case

how do i get my script to execute multiple commands?, Using parallel (in the moreutils package): parallel -j 2 -- 'lsyncd lsyncd.lua' '​webpack --progress --color -w'. Since the parallel process runs in  I am trying to use find -exec with multiple commands without any success. Does anybody know if commands such as the following are possible? find *.txt -exec echo "$(tail -1 '{}'),$(ls '{}')" \; Basically, I am trying to print the last line of each txt file in the current directory and print at the end of the line, a comma followed by the filename.

How to run multiple commands in parallel and see output from both , find and xargs command; 6 Search for pattern using xargs; 7 Run multiple commands with xargs; 8 Executing commands in parallel; 9 Accept input from a file  I am using SUSE Linux Enterprise Server 10 SP2 (i586) and I had earlier ammended my sudoers file to allow users to become root user with "sudo su - " command Now I am trying to add multiple users to the sudoers file to run several commands such as restarting the server, restarting the nagios

Linux xargs command examples, To run multiple commands at once, enter them into the command line separated with semicolon, && or ||. Using semicolon. When using semicolon, commands are​  In this guide, we covered a simple yet helpful find utility trick to enable us find multiple filenames by issuing a single command. To understand and use find for many other vital command line operations, you can read our article below. Don’t Miss: Master Linux ‘find’ Command with This 35-Examples

Comments
  • superuser.com/questions/236601/…
  • As far as checking for the possibility of the command, did you not try it out on your system?
  • From the find manual page: There are unavoidable security problems surrounding use of the -exec option; you should use the -execdir option instead.unixhelp.ed.ac.uk/CGI/man-cgi?find
  • Related: unix.stackexchange.com/questions/156008/…
  • @JVE999 link is broken, alternative at ss64.com/bash/find.html
  • how to grep twice? this is failing: find ./* -exec grep -v 'COLD,' {} \; -exec egrep -i "my_string" {} \;
  • @rajeev The second exec will only run if the return code for the first returns success, otherwise it will be skipped. This should probably be noted in this answer.
  • This would be the answer
  • Note the use of -n in some of the other answers to suppress the newline generated by echo, which is handy if your second command produces only one line of output and you want them to be easier to read.
  • Great solution. Works like a charm.
  • If you want to run Bash instead of Bourne you can also use ... -exec bash -c ... instead of ... -exec sh -c ....