开发者

How can I add text to the same line?

I used this command to find mp3 files and write their name on 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 开发者_如何学Gohave been moved.

For example if the mp3 files are 1.mp3 and 2.mp3 then the log.txt should look like

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

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

How can I do that using unix commands? 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; text processing is rather awkward in the shell.

E.g. in Perl you can just postprocess the output of find, and print out what you did.

#!/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; in particular handling of funny filenames (embedded spaces, special chars) is easier. Be careful when invoking syste() commands though.

For docs on the File::Find module see http://perldoc.perl.org/File/Find.html .


GNU find

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜