Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using find command in bash script

Tags:

find

bash

command

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 ?

like image 722
justuser Avatar asked Dec 14 '11 17:12

justuser


People also ask

What is the find command in bash?

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.

How do I search a bash script?

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.

What is find in shell script?

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.

How does find command work in Linux?

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.


2 Answers

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.

like image 137
ghoti Avatar answered Oct 09 '22 04:10

ghoti


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

like image 37
Vivek Sethi Avatar answered Oct 09 '22 02:10

Vivek Sethi