Hi ,

Actually I have just to copy some specific files from a directory. As there are 5000 files and I have to copy only 300. so I just want to make program which read the name of the file given in text file and then copies those file from the directory to some other directory . for ex

Directory parijat : a1.mol2 a2.mol2 ................................a5000.mol2

I want to copy a3.mol2, a890.mol2,a675.mol2,a4500.mol2 etc

so I have given alist containing file name

a3.mol2
a890.mol2
...
.
.
-
a4500.mol2

I am using bash but ...but it wont work out

cat file | while read line ; grep $line directory

Recommended Answers

All 5 Replies

This is Python forum and it is easy to do in Python, but in Linux or other unix style system you can use backquotes.

So to copy listed files from file 'file' to directory 'directory' you could do:

cp `cat file` directory

Thank but I am very new to programming in python so i am learning to python and bash scripting

This is Python forum and it is easy to do in Python, but in Linux or other unix style system you can use backquotes.

So to copy listed files from file 'file' to directory 'directory' you could do:

cp `cat file` directory

hey but when i am running this it does not work

import shutil, os 
srcfolder = "/source/folder/here"
destinationfolder = "/destination/folder/here"
f = open("listed.txt")
for filename in f:
    shutil.copy(os.path.join(cwd, filename), destinationfolder)

This should do it?

hey but when i am running this it does not work

Seems to me working in Portable Ubuntu under WindowsXP:

pubuntu@pubuntu:/usr/bin$ ls | tail -n 20 >~/file
pubuntu@pubuntu:/usr/bin$ cd
pubuntu@pubuntu:~$ cat file
xvidtune
xvinfo
xwd
x-window-manager
xwininfo
xwud
x-www-browser
xxd
yelp
yes
zdump
zenity
zip
zipcloak
zipgrep
zipinfo
zipnote
zipsplit
zjsdecode
zsoelim
pubuntu@pubuntu:~$ cd -
/usr/bin
pubuntu@pubuntu:/usr/bin$ cp `cat ~/file` ~
pubuntu@pubuntu:/usr/bin$ cd
pubuntu@pubuntu:~$ ls -l
total 1808
drwxr-xr-x 2 pubuntu pubuntu   4096 2009-05-05 16:32 Desktop
-rw-r--r-- 1 pubuntu pubuntu    357 2009-05-03 20:00 examples.desktop
-rw-r--r-- 1 pubuntu pubuntu    155 2010-07-02 18:17 file
-rwxr-xr-x 1 pubuntu pubuntu  31348 2010-07-02 18:18 xvidtune
-rwxr-xr-x 1 pubuntu pubuntu   9584 2010-07-02 18:18 xvinfo
-rwxr-xr-x 1 pubuntu pubuntu  26200 2010-07-02 18:18 xwd
-rwxr-xr-x 1 pubuntu pubuntu 489740 2010-07-02 18:18 x-window-manager
-rwxr-xr-x 1 pubuntu pubuntu  26104 2010-07-02 18:18 xwininfo
-rwxr-xr-x 1 pubuntu pubuntu  26172 2010-07-02 18:18 xwud
-rwxr-xr-x 1 pubuntu pubuntu   4277 2010-07-02 18:18 x-www-browser
-rwxr-xr-x 1 pubuntu pubuntu  13932 2010-07-02 18:18 xxd
-rwxr-xr-x 1 pubuntu pubuntu 270720 2010-07-02 18:18 yelp
-rwxr-xr-x 1 pubuntu pubuntu  30264 2010-07-02 18:18 yes
-rwxr-xr-x 1 pubuntu pubuntu  13764 2010-07-02 18:18 zdump
-rwxr-xr-x 1 pubuntu pubuntu  66468 2010-07-02 18:18 zenity
-rwxr-xr-x 1 pubuntu pubuntu 185952 2010-07-02 18:18 zip
-rwxr-xr-x 1 pubuntu pubuntu  85052 2010-07-02 18:18 zipcloak
-rwxr-xr-x 1 pubuntu pubuntu   2953 2010-07-02 18:18 zipgrep
-rwxr-xr-x 1 pubuntu pubuntu 158240 2010-07-02 18:18 zipinfo
-rwxr-xr-x 1 pubuntu pubuntu  80688 2010-07-02 18:18 zipnote
-rwxr-xr-x 1 pubuntu pubuntu  80688 2010-07-02 18:18 zipsplit
-rwxr-xr-x 1 pubuntu pubuntu  57588 2010-07-02 18:18 zjsdecode
-rwxr-xr-x 1 pubuntu pubuntu  92320 2010-07-02 18:18 zsoelim
pubuntu@pubuntu:~$ rm `cat ~/file`
pubuntu@pubuntu:~$ ls
Desktop  examples.desktop  file
pubuntu@pubuntu:~$
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.