I just start to use bash script and i need to use find command with more than one file type.
list=$(find /home/user/Desktop -name '*.pdf')
this code work for pdf type but i want to search more than one file type like .txt or .bmp together.Have you any idea ?
The Bash find Command 101 The find command allows you to define those criteria to narrow down the exact file(s) you'd like to find. The find command finds or searches also for symbolic links (symlink). A symbolic link is a Linux shortcut file that points to another file or a folder on your computer.
Here comes another option, “-type,” to use in the “find” instruction to specify a file type, i.e. file or directory. We have used this option to search for type “file” for bash file and got a single result, i.e. new.sh in Desktop folder. If you don't add the path, it will search the directories as below.
The find command is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments. find command can be used in a variety of conditions like you can find files by permissions, users, groups, file types, date, size, and other possible criteria.
The find command takes a number of paths, and searches for files and directories in each path “recursively”. Thus, when the find command encounters a directory inside the given path, it looks for other files and directories inside it.
Welcome to bash. It's an old, dark and mysterious thing, capable of great magic. :-)
The option you're asking about is for the find
command though, not for bash. From your command line, you can man find
to see the options.
The one you're looking for is -o
for "or":
list="$(find /home/user/Desktop -name '*.bmp' -o -name '*.txt')"
That said ... Don't do this. Storage like this may work for simple filenames, but as soon as you have to deal with special characters, like spaces and newlines, all bets are off. See ParsingLs for details.
$ touch 'one.txt' 'two three.txt' 'foo.bmp' $ list="$(find . -name \*.txt -o -name \*.bmp -type f)" $ for file in $list; do if [ ! -f "$file" ]; then echo "MISSING: $file"; fi; done MISSING: ./two MISSING: three.txt
Pathname expansion (globbing) provides a much better/safer way to keep track of files. Then you can also use bash arrays:
$ a=( *.txt *.bmp ) $ declare -p a declare -a a=([0]="one.txt" [1]="two three.txt" [2]="foo.bmp") $ for file in "${a[@]}"; do ls -l "$file"; done -rw-r--r-- 1 ghoti staff 0 24 May 16:27 one.txt -rw-r--r-- 1 ghoti staff 0 24 May 16:27 two three.txt -rw-r--r-- 1 ghoti staff 0 24 May 16:27 foo.bmp
The Bash FAQ has lots of other excellent tips about programming in bash.
If you want to loop over what you "find", you should use this:
find . -type f -name '*.*' -print0 | while IFS= read -r -d '' file; do printf '%s\n' "$file" done
Source: https://askubuntu.com/questions/343727/filenames-with-spaces-breaking-for-loop-find-command
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With