The exact same command should work fine in a script:
#!/usr/bin/env bash
find te*/my\ files/ -print
If you need to have it as a variable, it gets a bit more complex:
#!/usr/bin/env bash
search='te*/my\ files/'
eval find "$search" -print
WARNING:
Using eval
like that is not safe and can result in executing arbitrary and possibly harmful code if your file names can contain certain characters. See bash FAQ 48 for details.
It's better to pass the path as an argument:
#!/usr/bin/env bash
find "$@" -name "file*"
Another approach is to avoid find
altogether and use bash's extended globbing features and globs:
#!/usr/bin/env bash
shopt -s globstar
for file in te*/my\ files/**; do echo "$file"; done
The globstar
bash option lets you use **
to match recursively:
globstar
If set, the pattern ** used in a pathname expansion con‐
text will match all files and zero or more directories
and subdirectories. If the pattern is followed by a /,
only directories and subdirectories match.
To make it act 100% like find and include dotfiles (hidden files), use
#!/usr/bin/env bash
shopt -s globstar
shopt -s dotglob
for file in te*/my\ files/**; do echo "$file"; done
You can even echo
them directly without the loop:
echo te*/my\ files/**
Combining:
find -type f \( -name "*.avi" -or -name '*.mp4' \) -exec md5sum {} + > checklist.chk
Adding output to one file:
find -type f -name "*.avi" -exec md5sum {} + > checklist.chk
find -type f -name "*.mp4" -exec md5sum {} + >> checklist.chk
Best Answer
The command you are looking for is:
Adding
type -f
will make thefind
command look only for files.In your second command, you need to add a space after
\(
and before\)
(you also forgot\
before(
).Also, you don't need a
-
between-not
and-name
. Your first command works fine if you remove it, although not producing the output that you want (see JoL's comment).You can read more about
find
's syntax and options at the command's online Ubuntu manpage, or run the commandman find
to read the manual in the terminal. Specifically, read theOPERATORS
section of the manual, since that's what appears to be confusing you the most.Note: Most terminal commands provide a manual with the proper command syntax and available options which you can read if you run
man <command>
in your terminal.