3

OS: Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-62-generic x86_64)

I have a directory like the following:

~/total/
    test1/
        test1.txt
        some_other_file_i_dont_care.py
    test2/
        test2.tex
        some_folder_i_dont_care/
    test3/
        test3.csv

I want to move only the text files out of there, and erase their parent folders (which have the same names as the files I'm interested in).

So the result should be this:

~/total/
    test1.txt
    test2.tex
    test3.csv

I think I'm not far from the solution with this function:

find ~/total/ -type f  \( -iname '*.txt' -o -iname '*.tex' -o -iname '*.csv' \)          
| xargs mv -ifile file ~/total/

But then, how can I remove the remaining folders?

tomasyany
  • 133

1 Answers1

3

You can do the two operations in one go of find :

find . -depth \( -regex '.*\.\(txt\|tex\|csv\)' -exec mv -- {} . \; \
                 -o -not -name . -delete \)
  • -regex '.*\.\(txt\|tex\|csv\)' -exec mv -- {} . \; will find the files with .txt or .tex or csv extensions and will move those to the current directory

  • -not -name . -delete will then remove everything else

Example :

total$ tree
.
├── test1
│   ├── some.py
│   └── test1.txt
├── test2
│   ├── somedir
│   └── test2.tex
└── test3
    └── test3.csv


total$ find . -depth \( -regex '.*\.\(txt\|tex\|csv\)' -exec mv -- {} . \; -o -not -name . -delete \)


total$ tree
.
├── test1.txt
├── test2.tex
└── test3.csv
heemayl
  • 93,925