loop inside multiple-level paths and rename

Assuming I have a file named list.txt contains Baths of files inside a directory;

And the baths pattern like this:

./path/to/file.txt ./path/to/another/file.txt ... etc 

I want to loop inside list.txt file to get the baths of the files and change its encoding using iconv;

I was doing that if everything was in the same directory, using:

for i in `cat list.txt` ; do iconv -f windows-1256 -t utf8 $i > new_$i; done 

But the problem I'm facing now is, since it's muli-level baths, naming the output of iconv prints the new_$i in the start of the bath - I guess - and that gives me errors:

No such file or directory 

So, how can I get the outpot of iconv to be like:

./path/to/new_file.txt ./path/to/another/new_file.txt ... etc 

Thanks

Replay

You could use something like this, if you're using bash as your shell:

while read name
do dir="${name%/*}" file="${name##*/}"
iconv -f windows-1256 -t utf8 "$dir/$file" -o "$dir/new_$file"
done  < list.txt

The line which assigns dir and file uses constructs documented in the Parameter expansion part of the EXPANSION section of the bash man page. The quote marks around the constructs are intended to handle white space in file names.

If you want to test something like this before using it, you can prepend echo to the command of interest (iconv in this example) to make sure the command is what you intend it to be.

Category: linux Time: 2016-07-28 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 1.801 (s). 13 q(s)