我正在查找mp3和mp3.md5文件,并将它们向上移动一个目录级别。如何指示 mv 目标路径?
找到:http://www.cyberciti.biz/tips/howto-linux-unix-find-move-all-mp3-file.html 哪种帮助 - 文件结构如下。从$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
感谢
尝试将find
命令更改为
find . -name '*.mp3*' -type f -print0 | xargs -0 -I list mv list ${LOCATION}${DIR}
这不行吗?
find . -name '*.mp3*' -type f -execdir mv -nv -- {} .. ;
这将找到所有名称中带有.mp3
的文件(-type f
)。对于每个这样的文件,它将从它们在命令mv {} ..
中的目录运行(其中{}
被替换为文件名)。那是使用-execdir
而不是-exec
.
看:
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$
它可以是这样的(未经测试)
for i in $( find $LOCATION -type d -name 'Web_Copy' ); do
mv $i/* $i/.. && rmdir $i
done