in my working directory I have several folders which have hundreds of files of different types (.txt, .csv, .png...). These folders are structured as this example:
myDir/
myDir/Folder_1/ ... file_1.txt, file_2.csv, file_3.txt
myDir/Folder_2/ ...
myDir/Folder_3/ ...
...
I need to move all the .csv files to a new directory but keeping the same directory tree like this:
myDir/
myDir/Folder_1/ ...
myDir/Folder_2/ ...
...
myDir/New/
myDir/New/Folder_1/ ... file_2.csv
myDir/New/Folder_2/ ...
...
I found that cp command has --parents
flag that does what I want to do, but I don't want to copy these files, I only want to move them.
I searched for this question before and found this one: Bash script for moving files and their parent directory
It is quite similar to what I need to do, but I'm not able to understand it.
Check the commands the following snippet prints out. If they do what you want, then just remove the echo
(before mkdir
and mv
).
for path in myDir/*; do
[[ -d "$path" ]] && echo mkdir -p "myDir/New/${path#myDir/}"
done
for csv in myDir/*/*.csv; do
echo mv "$csv" "myDir/New/${csv#myDir/}"
done