I've got a file, that contains about 20k strings. Each string in that file should be an argument for a command within bash script.

At first my script generates a file with this strings.

find /home/alotoffolders -type f -name  "*.mp4" | grep some_greps > ~/tests/find_smth

After that trying to set a variable, that will be a part of argument

filename=$(cut -d/ -f11 ~/tests/find_smth)

Then I'm trying to read all file line by line and use every string as an input argument (seems like it works), but for output - there is some issue.

  for i in `cat ~/tests/find_smth`; do ./other_script -input $i -output /home/folder1/folder2/$filename; done

Script can't see the static path before $filename.

Where is the problem?

1 Answer 1

  • NEVER use for i in $(cat ...) ... or similar. It loops over words, not lines. And even if it would, filenames are allowed to contain newlines.
  • Use shell parameter expansion or basename command instead of cut to get the file name.
  • Instead of using grep on the results of find, you might use e.g. find's -regex or -iregex option.

Use find -exec with sh -c:

find /home/alotoffolders -type f -iregex 'some_pattern.mp4' \
  -exec sh -c '
    /path/to/other_script -input "$1" -output "/home/folder1/folder2/${1##*/}"
  ' find-sh {} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.