Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Linux by (18.4k points)
edited by
How can I move all the .txt files from a folder and all included folders into my target directory?

I rename them to a folder they were included in, although that's are not that important. I'm not exactly familiar with bash.

1 Answer

0 votes
by (36.8k points)
edited by

To recursively move files, combine find with mv.

find src/dir/ -name '*.txt' -exec mv {} target/dir/ \;

To rename your files when you move them it's trickier. One way is to have the loop that passes the file name through tr / _, which converts slashes into your underscores.

find src/dir/ -name '*.txt' | while read file; do

    mv "$file" "target/dir/$(tr / _ <<< "$file")"

done

 To know about Linux join the Linux training

Related questions

0 votes
1 answer
0 votes
1 answer
asked Dec 3, 2020 in Linux by blackindya (18.4k points)

Browse Categories

...