Question

I'm finding mp3 and mp3.md5 files and moving them one directory level higher. How would one indicate the mv target path?

Found: http://www.cyberciti.biz/tips/howto-linux-unix-find-move-all-mp3-file.html which sort of helps -- file structure is below. Running script from $LOCATION.

|-- 681506b
|   |-- 681506b.xml
|   `-- Web_Copy
|       |-- 681506b_01.mp3
|       `-- 681506b_01.mp3.md5
DESIRED STRUCTURE AFTER DELETING 'Web_Copy' dir:
|-- 681506b
|   |--681506b.xml
|   |--681506b_01.mp3
|   |--681506b_01.mp3.md5

LOCATION="/var/www/web/html/testdata/"
DIRLIST=`ls -x`
for DIR in $DIRLIST
do
  if [ -d "$DIR" ]
   then
   find . -name "*.mp3*" -type f -print0|xargs -0L1 mv {} $LOCATION$DIR
  fi
done

ERROR: mv: target ./681506b/Web_Copy/681506b_01.mp3 is not a directory
S/B:  mv /var/www/web/html/testdata/681506b/
REPLACED mv with echo: 
{} /var/www/web/html/testdata/680593a./681506b/Web_Copy/681506b_01.mp3

Thx

Was it helpful?

Solution 3

It can be something like this (not tested)

for i in $( find $LOCATION -type d -name 'Web_Copy' ); do 
  mv $i/* $i/.. && rmdir $i
done 

OTHER TIPS

Try changing your find command to

find . -name '*.mp3*' -type f -print0 | xargs -0 -I list mv list ${LOCATION}${DIR}

Wouldn't just this work?

find . -name '*.mp3*' -type f -execdir mv -nv -- {} .. \;

This will find all the files (-type f) with .mp3 in their name. For each such file, it will run from the directory they are in the command mv {} .. (where {} is replaced by the file's name). That's the use of -execdir instead of -exec.

Look:

gniourf@somewhere$ mkdir Test && cd Test
gniourf@somewhere$ mkdir -p 681506b{,/Web_Copy}; touch 681506b/{681506b.xml,Web_Copy/681506b.mp3{,.md5}}
gniourf@somewhere$ tree
.
`-- 681506b
    |-- 681506b.xml
    `-- Web_Copy
        |-- 681506b.mp3
        `-- 681506b.mp3.md5

2 directories, 3 files
gniourf@somewhere$ find . -name '*.mp3*' -type f -execdir mv -nv -- {} .. \;
`./681506b.mp3' -> `../681506b.mp3'
`./681506b.mp3.md5' -> `../681506b.mp3.md5'
gniourf@somewhere$ tree
.
`-- 681506b
    |-- 681506b.mp3
    |-- 681506b.mp3.md5
    |-- 681506b.xml
    `-- Web_Copy

2 directories, 3 files
gniourf@somewhere$ 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top