[英]How can I add text to the same line?
我使用以下命令查找mp3文件,并将其名称写在log.txt上:
find -name *.mp3 >> log.txt
我想使用mv命令移动文件,我想将其附加到日志文件,以便它可以显示文件已移动的路径。
例如,如果mp3文件是1.mp3和2.mp3,则log.txt应该看起来像
1.mp3 >>>> /newfolder/1.mp3
2.mp3 >>>> /newfolder/2.mp3
如何使用Unix命令来做到这一点? 谢谢!
仅使用移动:
mv -v *.mp3 tmp/ > log.txt
或使用查找:
find -name '*.mp3' -exec mv -v {} test/ >> log.txt \;
您可能应该使用Perl或Python之类的脚本语言。 外壳中的文本处理相当笨拙。
例如,在Perl中,您可以仅对find的输出进行后处理,并打印出您所做的工作。
#!/usr/bin/perl -w
use strict;
use File::Find;
my @directories_to_search=("/tmp/");
sub wanted {
print "$File::Find::name >>> newdir/$_\n";
# do what you want with the file, e.g. invoke commands on it using system()
}
find(\&wanted, @directories_to_search);
用Perl或类似语言进行操作比使用Shell更容易。 特别是有趣的文件名(嵌入式空格,特殊字符)的处理更加容易。 但是,在调用syste()命令时要小心。
有关File :: Find模块的文档,请参见http://perldoc.perl.org/File/Find.html 。
GNU查找
find /path -type f -iname "*.mp3" -printf "%f/%p\n" | while IFS="/" -r read filename path
do
mv "$path" "$destination"
echo "$filename >>> $destination/$filename " > newfile.txt
done
产量
$ touch 'test"quotes.txt'
$ ls -ltr
total 0
-rw-r--r-- 1 root root 0 2009-11-20 10:30 test"quotes.txt
$ mkdir temp
$ ls -l temp
total 0
$ find . -type f -iname "*\"*" -printf "%f:%p\n" | while IFS=":" read filename path; do mv "$filename" temp ; done
$ ls -l temp
total 0
-rw-r--r-- 1 root root 0 2009-11-20 11:53 test"quotes.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.