简体   繁体   中英

Copy files and one directory up

I have a txt file with something like a couple of thousand files with following patch scheme:

/fixed_dir1/fixed_dir2/fixed_dir3/variable_dir1/filename1
/fixed_dir1/fixed_dir2/fixed_dir3/variable_dir1/filename2
/fixed_dir1/fixed_dir2/fixed_dir3/variable_dir2/filename3

I would like loop over all the files and just copy variable_dir/filename into a newdir, eg end up with something like:

/new/path/variable_dir1/filename1
/new/path/variable_dir1/filename2
/new/path/variable_dir2/filename3

Could you provide me with a small shell script for bash for that?

you then might use a while loop

while read ;
do 
rep=s( echo $REPLY | sed 's/.\/dir3//' ) #bad method but more readable
mkdir -p new/$rep && cp $REPLY new/$rep/
done < filesource

example

👨francois@💻zaphod🐙:~/tmp/test$ touch filename{1,2,3,4}
👨francois@💻zaphod🐙:~/tmp/test$ ls filename* > toto
👨francois@💻zaphod🐙:~/tmp/test$ while read ; do mkdir filename$REPLY && cp $REPLY filename$REPLY ; done  < toto
👨francois@💻zaphod🐙:~/tmp/test$ tree
.
├── filename1
├── filename2
├── filename3
├── filename4
├── filenamefilename1
│   └── filename1
├── filenamefilename2
│   └── filename2
├── filenamefilename3
│   └── filename3
├── filenamefilename4
│   └── filename4

Welcome to the wonderful world of Notepad++, where I have done this:

First, I've copied the list of your files, twice:

/fixed_dir1/fixed_dir2/fixed_dir3/variable_dir1/filename1
/fixed_dir1/fixed_dir2/fixed_dir3/variable_dir1/filename2
/fixed_dir1/fixed_dir2/fixed_dir3/variable_dir2/filename3

In the second copy, I've selected /fixed_dir1/fixed_dir2/fixed_dir3 and done a find/replace by /new/path . After that, I've used column mode to copy that second list behind the first one, and again using column mode, I've added cp at the beginning of the list, so I get something like:

cp /fixed_dir1/fixed_dir2/fixed_dir3/variable_dir1/filename1 /new/path/variable_dir1/filename1
cp /fixed_dir1/fixed_dir2/fixed_dir3/variable_dir1/filename2 /new/path/variable_dir1/filename2
cp /fixed_dir1/fixed_dir2/fixed_dir3/variable_dir2/filename3 /new/path/variable_dir2/filename3

You can try the first line, see if it works. In case not you correct it and use column mode again to get it working for all lines.

You might think " Are you serious? I was looking for a script with some programming skills and you come up with some basic text formatting! ". I can tell you: I've done numerous actions like this and if anything is wrong with your script, you might mess up your whole files/directories, while this approach might only be wrong for one, you correct it, and for the rest you have full control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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