简体   繁体   English

如何将文本添加到同一行?

[英]How can I add text to the same line?

I used this command to find mp3 files and write their name on log.txt: 我使用以下命令查找mp3文件,并将其名称写在log.txt上:

find -name *.mp3 >> log.txt

I want to move the files using the mv command and I would like to append that to the log file so it could show the path where the files have been moved. 我想使用mv命令移动文件,我想将其附加到日志文件,以便它可以显示文件已移动的路径。

For example if the mp3 files are 1.mp3 and 2.mp3 then the log.txt should look like 例如,如果mp3文件是1.mp3和2.mp3,则log.txt应该看起来像

1.mp3 >>>> /newfolder/1.mp3 

2.mp3 >>>> /newfolder/2.mp3

How can I do that using unix commands? 如何使用Unix命令来做到这一点? Thank you! 谢谢!

Using only move: 仅使用移动:

mv -v *.mp3 tmp/ > log.txt

or using find: 或使用查找:

find -name '*.mp3' -exec mv -v {} test/ >> log.txt \;

You should probably use some scripting language like Perl or Python; 您可能应该使用Perl或Python之类的脚本语言。 text processing is rather awkward in the shell. 外壳中的文本处理相当笨拙。

Eg in Perl you can just postprocess the output of find, and print out what you did. 例如,在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);

Doing it in Perl or similar makes some things easier than in the shell; 用Perl或类似语言进行操作比使用Shell更容易。 in particular handling of funny filenames (embedded spaces, special chars) is easier. 特别是有趣的文件名(嵌入式空格,特殊字符)的处理更加容易。 Be careful when invoking syste() commands though. 但是,在调用syste()命令时要小心。

For docs on the File::Find module see http://perldoc.perl.org/File/Find.html . 有关File :: Find模块的文档,请参见http://perldoc.perl.org/File/Find.html

GNU find 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

output 产量

$ 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM