Sebastian Zeki
Sebastian Zeki

Reputation: 6874

bash script to move files from deep directory structure

I would like to be able to move files to a directory (eg Desktop/tbb) from a directory structure where each file is kept deep in the structure in different folders.

The directory structure with the files I want is like this:

/Users/me/Desktop/SeqS/Plots/Results/FTF/freak/5M/5M_T1/SNAPSHOT/CN/Jimbob/LP7005321bam_ratio.txt

/Users/me/Desktop/SeqS/Plots/Results/FTF/freak/5M/5M_T2/SNAPSHOT/CN/Salad/LP9904920bam_ratio.txt

/Users/me/Desktop/SeqS/Plots/Results/FTF/freak/5M/5M_T3/SNAPSHOT/CN/Spags/LP6005334bam_ratio.txt

To move all of them to /Desktop/tbb

Is there a way of asking in bash to search for example for all files that end in ...bam_ratio.txt within a folder and it to search folders within that to retrieve the correct files?

Upvotes: 1

Views: 110

Answers (2)

ice13berg
ice13berg

Reputation: 713

find command. It's different in different versions of bash, but it looks something like this.

find /Users/me/Desktop/SeqS/Plots/Results/FTF/freak/ -type f -name "*bam_ratio.txt" -exec mv {} /Desktop/tbb/ \;

Upvotes: 1

ebo
ebo

Reputation: 2747

You can use the find command to find the files you want, e.g.:

find . -type f -iname "*bam_ratio.txt"

Will find all files ending in 'bam_ratio.txt' in the current directory.

To move them to your desired directory you can use the -exec flag like:

find . -type f -iname "*bam_ratio.txt" -exec mv {} ~/Desktop/tbb/ \;

Upvotes: 1

Related Questions