Navigate directory and move files one level up, recursively [duplicate]
I have many directory with different name, each of them contain a subdirectory named httpd-ack (same name for all of them) in which are stored files.
I need a command to recursively enter each directory, enter subdir httpd-ack, move all files one level up and then delete the httpd-ack folder (which is empty now).
start is
name1/httpd-ack/(files)
name2/httpd-ack/(files)
name3/...[...]
ending should be
name1/(files)
name2/(files)
name3....[...]
Any help will be much appreciated....
42 Answers
Thanks to @Jos, this worked for me:
Create a file named
movefiles.sh(and set it executable) with the following code:#!/bin/bash path=$1 echo "Now processing $path" cd "$path" if [ -d "./httpd-ack" ]; then cd httpd-ack mv * ../ cd .. rmdir httpd-ack fiInvoke find as follows:
find . -maxdepth 1 -type d -exec ./movefiles.sh {} \;
It worked like a charm... (I didn't try @bac0n solution.)
1This script should be a good start:
#!/bin/bash
moveFiles () { path=$1 echo "Now processing $path" cd "$path" if [ -d "./httpd-ack" ]; then cd httpd-ack mv * ../ cd .. rmdir httpd-ack fi
}
find top/level/dir -type d -exec moveFiles {} \;First, it defines a function moveFiles that moves into the httpd-ack directory (if one is present) and moves all files upwards, then cds up and deletes the now empty httpd-ack.
Then it finds all directories, starting from /top/level/dir, and calls the newly defined function on each of them.
I'm sure this could be improved. Editors, feel free.
5