LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-14-2011, 01:45 PM   #1
mpranav007
LQ Newbie
 
Registered: Mar 2011
Posts: 4

Rep: Reputation: 0
cp command help


I have around 500 files in a folder. But I don't want to copy all of them. The name of the files that I want to copy is in a txt file.

The question is how to use the names from the txt file as arguments for the cp command.
 
Old 03-14-2011, 01:52 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You need to read the lines from the file into a variable and then use that variable as an argument to the cp command. For example, consider the file "numbers":

1
2
3
4

A simple shell script to read the values from this file and print the numbers is as follows:

Code:
#!/bin/bash

exec < numbers # Replace "stdin" (i.e. the command line) with the file "numbers".

while read line # Read each line from the file
do
  echo $line # Print the number that was read
done
For more information about shell scipting (in Bash), see the Advanced Bash-Scripting Guide. In particular, chapters 11 and 20 are relevant.

Last edited by Nylex; 03-14-2011 at 01:54 PM.
 
Old 03-14-2011, 07:17 PM   #3
siranjeevi
Member
 
Registered: May 2010
Location: India
Posts: 79

Rep: Reputation: 7
Hi,

I hope you are trying to copy the files and not the lines in a file, if so, here is your option.


Quote:
cp /path/to/souce/folder/*.txt /path/to/destination/folder/
The above command will copy all the files ending with name .txt.
 
Old 03-14-2011, 08:02 PM   #4
z1p
Member
 
Registered: Jan 2011
Location: the right coast of the US
Distribution: Ubuntu 10.04
Posts: 80

Rep: Reputation: 23
In addition of writing a shell script as nylex suggested, you can easily do it from the command line. For example in the bash shell the following would copy all the files listed in my_file_list.txt to /my/dest_dir

Code:
for i in `cat my_file_list.txt`; do cp $i /my/dest_dir; done

Last edited by z1p; 03-14-2011 at 08:03 PM.
 
1 members found this post helpful.
Old 03-14-2011, 08:17 PM   #5
WilliamHerry
LQ Newbie
 
Registered: Oct 2010
Location: China
Distribution: openSUSE, SUSE, RHEL
Posts: 2

Rep: Reputation: 0
Smile

Quote:
Originally Posted by z1p View Post
In addition of writing a shell script as nylex suggested, you can easily do it from the command line. For example in the bash shell the following would copy all the files listed in my_file_list.txt to /my/dest_dir

Code:
for i in `cat my_file_list.txt`; do cp $i /my/dest_dir; done
If i see this early, i would get that job!
 
Old 03-15-2011, 02:48 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,254

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Depending on whether or not you are copying all the file to the same location, you could simply do:
Code:
cp $(< file) location/
This assumes that the file either contains the path to each file or that you are within the directory of all said files.
And of course that the destination 'location' exists.
 
2 members found this post helpful.
Old 03-15-2011, 06:32 AM   #7
siranjeevi
Member
 
Registered: May 2010
Location: India
Posts: 79

Rep: Reputation: 7
Quote:
Originally Posted by siranjeevi View Post
Hi,

I hope you are trying to copy the files and not the lines in a file, if so, here is your option.




The above command will copy all the files ending with name .txt.
Hi,

Sorry for that mis leading message, I took your question i a wrong sense. You can go with Nylex and z1p's option.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 12:21 PM
how to copy drive using dd and tee command parallely? source code of dd command mdfakkeer Linux - Software 1 02-10-2010 02:31 PM
URGENT! Is there any command to get a history command lines and time in SUSE Linux.? igsoper Linux - Software 5 06-25-2009 03:14 AM
LXer: The Linux Command Shell For Beginners: Fear Not The Command Line! LXer Syndicated Linux News 0 12-22-2008 07:30 PM
Key stroke/command to shut down x and go into the command prompt screen? Fear58 Linux - General 1 07-14-2004 08:14 PM


All times are GMT -5. The time now is 10:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration