First of all, this is the code;
#!/bin/bash
i=1;
while read line;
do
cp "$i.eps" "$line.eps"
while read line2;
do
mv "$line.eps" "$line2"
sed -i '1d' cate.txt
done < cate.txt
sed -i '1d' cate.txt
((i++))
done < Names.txt
cp "$i.eps" "$line.eps"
Let me explain; I have 2 files totally. One of them is named "Names.txt" which contains the name of files. And the other file is "cate.txt" which contains the name of directories. Also I have .eps files which has name like; 1.eps, 2.eps, etc...
So, what I would like to do is to read the first line in "Names.txt" and than change the first file's name with the first line, and than read first line in "cate.txt" and move the first file under the directory that I read in "cate.txt"
PS 1: I used sed command there because I was always reading the first line in "cate.txt". So, I thought after I read the first line, I can delete it and than read the first line again. But the code was not successful to do that.
PS 2: In this code I can read "Names.txt" and rename .eps files. But when I start reading the "cate.txt" the script doesn't work properly.
Thank you!
Assuming that names.txt
and cate.txt
have the same number of rows, you can join them together and use that output instead:
#!/bin/bash
i=1
while read filename dirname; do
mkdir -p $dirname
cp $i.file $dirname/$filename
((i++))
done < <(paste names.txt cate.txt)
Example before running:
$ tree
.
|-- 1.file
|-- 2.file
|-- 3.file
|-- cate.txt
`-- dirs.txt
$ cat names.txt
first_file
second_file
third_file
$ cat cate.txt
first_dir
second_dir
third_dir
And after:
$ tree
.
|-- 1.file
|-- 2.file
|-- 3.file
|-- cate.txt
|-- dirs.txt
|-- first_dir
| `-- first_file
|-- second_dir
| `-- second_file
`-- third_dir
`-- third_file
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加