Looping two files (Rename and Move)

cyonder

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!

Josh Jolly

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]

編集
0

コメントを追加

0

関連記事

分類Dev

Rename a file with FileSystemObject while looping through files

分類Dev

Batch File to rename multiple files and move to folder

分類Dev

how to move files one by one and rename it from a directory to another in vb.net

分類Dev

Using rename to rename files and directories

分類Dev

os.rename to move a file

分類Dev

How to rename files with count?

分類Dev

Rename multiple files randomly

分類Dev

How to rename files with count?

分類Dev

Efficient way to rename files

分類Dev

Help with oneliner - create random files, rename to two-char filename, fill with random strings

分類Dev

Delete and rename files using unlink() and rename()

分類Dev

Python | Looping over pairs of files

分類Dev

Move and rename picture with Intervention/image module

分類Dev

Rename Files using wildcard paths

分類Dev

AWK, SED, REGEX to rename files

分類Dev

How to rename the set of files with pattern

分類Dev

Batch rename files to a sequential numbering

分類Dev

How to rename files with different extensions

分類Dev

Shellscript Looping Through All Files in a Folder

分類Dev

How to rename files to exclude the datetime stamp?

分類Dev

Rename files to be capitalised but not impact on file extensions

分類Dev

How do I rename the extension for a bunch of files?

分類Dev

How do I rename the extension for a bunch of files?

分類Dev

How do I rename all files to lowercase?

分類Dev

Rename files in install part of homebrew formula

分類Dev

Python - rename files incrementally based on julian day

分類Dev

Rename files, using text-file as source

分類Dev

Bash rename files with duplicated file names

分類Dev

Rename sequential files to a row/column format

Related 関連記事

ホットタグ

アーカイブ