3

I want to use the find command to execute a special command on all of the files inside the directory that are contained inside multiple different directories with a specific keyword in it (the keyword is "Alpha") and keep the output inside the working directory of the initial file I ran the command on.

The command works such that it requires to you to provide the initial file to perform the command on and then the name of the newly converted file. So like this

command file_to_run_command_on.txt new_file.txt

This is my code

find $PWD -name *.txt* -exec command {} new_file. \;

Right now, it finds all the text files in this directory even in the sub directories and outputs just one file in the directory I run the initial find command from. I'm also unsure how to add the additional search for the keyword in the directory. All advice appreciated!

2
  • Are you wondering why command file1.txt newfile. followed by command file2.txt newfile. only results in a single output file? This is because you have specified the same output name for both, so they keep overwriting that name Commented Oct 26, 2018 at 22:19
  • @thatotherguy Right, but if it stayed inside the local directory where that initial file is contained the files would not be overwritten because all of these files are in their own unique directory. I see why the initial post was not clear on this though so I have changed the wording. Does this make more sense? Commented Oct 26, 2018 at 22:42

1 Answer 1

7

-exec runs the command in the directory from which you start find.

-execdir runs the command in the matching file's directory.

To only find *.txt* files whose parents contain a specific file, you could use:

find "$PWD" -path "*keyword*/*.txt*" -execdir command {} new_file \;

This will run the command for foo/bar/some-keyword-dir/baz/etc/file.txts but not for foo/bar/baz/file.txts (no keyword in parent directory names) or foo/bar/some-keyword-dir/baz/file.tar (not *.txt*)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.