Use find when you are searching

Where is… think ctrl (or cmd) F

Deena Blumenkrantz
Deena Does Data Science
3 min readJun 25, 2019

--

Use find to find the name of a file by searching for what you called it or what you wrote inside it.

This blog was stimulated by my need to generate a list of file names so that I could move them from one directory to another and my need to generate a list of file names so that I could iterate through them and perform a command on each.

## Make a list of file names`ls *R1*`- list all files that have the string `R1`- For `ls` , `*` means any number of any character- `ls` does not work when you put quotes around the string## When files are in subfolders, generate a list of file names and paths to those files`find . -name '*vcf'``find . -regex '.*vcf$'`- `find` needs a flag to search for a string- Enclosing the string in quotes works more frequently than not enclosing it in quotes- Generally, it is best to use the `-name` flag- With the `-name` flag- You can search for...- the presence of multiple strings with multiple `-name 'string'` flags (like using and)- file names that don't include specific strins with `! -name 'string'`- file names that have one **or** another string with `-o -name 'string'`- file names that `^` don't have a character (or string) adjacent to a character or string `find -name '*gz.[^bs]*'` or `find -name '*[^z].bai'`- read `[^bs]` as "not b or s"- `*` means any number of any character- With the `-regex` flag- `*` means that the character preceeding it can occur any number of times- `.` represents any character- To illustrate, `[abc]*.vcf` means that the letters a, b, or c can repeat an infinite number of times before `.vcf`- e.g. `abcabcabc`, `aaaaaa`, `bbbb`, or `ccccc`- `$` is not needed, but if present `find` still works## Move a set of files from one directory to another`find -name *.vcf | xargs -I {} mv {} ../relative/path/to/destination/{}`**xargs**- `xargs [flags] [command [arguments]]`- `xargs -I {} mv {} ../path/{}`- xargs reads items from the standard input, delimited by blanks or newlines, and executes the given command- In the case above,- `mv` is the command- `{} ../path/{}` are the arguments- Flag `-I string`- The `-I` flag is given a `string`, which is used as a substitution character- The `string`, in this case, is `{}`- The `-I` flag allows `xargs` to replace occurances of `{}` in the arguments of the command (`mv`) with filenames read from standard input- In summary: read `xargs -I {} mv {} destination/{}` as "take each input `{}` (which was an output from grep) and move it to the destination directory while keeping its original filename"## Redirect your list into a .txt file### The 3 most common redirection operators`>` output redirection operator- redirects output to a file (or device) and overwrites anything that already exists there`>>` output append operator- redirects output to a file (or device) and appends the output to anything that already exists there`<` input redirection operator- directs data from a file (or device) to a program or device### Examples of redirecting lists to files`ls *R1* > r1_list.txt``find . -name '*vcf' > list_vcf.txt`

--

--

Deena Blumenkrantz
Deena Does Data Science

I’m a molecular virologist training to become a computational biologist / bioinformatician