Linux copy multiple files to one

Copying Multiple files using cp

I want to copy multiple files in one go using cp. The problem is that the filenames contain spaces here and there. So I tried cp $(ls -1|tr ‘ ‘ ») dest but apparently I cannot truncate to nothing. Then I tried removing null space to spaces. That didnt work either. I tried running it over a for loop. That too didnt work. Can some one please help me.

tried this cp $(ls|tr ‘\ ‘ ‘\’) /home/user , cp $(ls|tr ‘ ‘ ‘\’) /home/user , cp $(ls|tr ‘ ‘ ‘\ ‘) /home/user . nthng works

cp * /home/user/ should work for just copying. The shell should treat each filename from the expansion as a single argument to the command.

3 Answers 3

You might want to try using find in combination with xargs and cp . See How can I use xargs to copy files that have spaces and quotes in their names? for more info.

for i in * do if echo $i | grep " " then NEWNAME=`echo $i | tr ' ' '_'` mv "$i" $NEWNAME fi done 

But in this case you rename the file (and of course you have to adapt the code to your needs) I have this function in my .bashrc

The problem is that you can’t tr the source file names for cp — you have to give it the names of the files as they exist on disk or it won’t find them. So it sounds like what you effectively want is to cp «file with spaces» destdir/filewithspaces for each individual file:

ls -1 | while read filename; do cp "$filename" "$dest/$" done 

The $ is an instance of the bash-ism $ (see shell parameter expansion in the Bash manual) to give the value of a variable with all instances of the search string replaced with the replacement string — so in this case replace all spaces with nothing.

Источник

How do I Copy Multiple Files Using CP in Linux

CP allows you to copy directories and command files using the command line. With this command, you can transfer multiple files or folders, preserve attribute information and create their backups. CP copies file independently from their originals. So, we can say that the CP command is useful for Linux. People still don’t know how to use this command and search for answers regarding copying multiple files using CP in Linux. That’s why we have written this article to briefly describe how to copy multiple files using CP in Linux.

How do I Copy Multiple Files Using CP in Linux?

Now we will explain different methods to use CP for performing various tasks.

Читайте также:  Linux with latest kernel

Copy a File Using CP Command

The file, which we will copy with the cp command, passes its name and destination. We will copy the Linuxhint.txt file to a new file named Linuxhintteam.txt using the cp command. During the operation, the cp command will also create a new file part of the operation.

First, select a folder/directory in which the file is available. In our case, the file is available in the Documents directory, so we use the following command:

After using the ls command to display the available file in the directory.

Now, execute the below command to make a copy of a specific file:

Here we have used the below command to make a copy of the Linuxhint.txt file:

To verify that the file is successfully copied, use the ls command again.

Copy Multiple Files Using CP Command

You must provide both the file name and the destination directory when using the cp command to copy multiple files.

First, open the specific directory in the terminal and execute the tree command. If you don’t know about the tree command, then please check out this blog.

In the Documents folder, we have two files, i.e., Linuxhint.txt and Linuxhintteam.txt, and one folder named Linuxtricks. Now, execute the below command in the terminal:

Here we used the following command to copy Linuxhint.txt and Linuxhintteam.txt in the Linuxtricks folder:

To verify that the files are copied, use the tree command again.

Copy a Directory Using CP Command

The CP command, by default, does not copy directories. On copying the directory, it shows an error.

To copy the directory with the help of the cp command, we have to pass -R flag. It creates a copy by copying the flagged folder recursively.

Here we have used the following command to copy the directories:

Finally, execute the tree command to verify that the system has successfully created multiple directories.

Copy Multiple Directories Using CP Command

To copy the multiple directories with the cp command, copy the path of the directories and pass it after the destination directory.

First, execute the tree command to see details about files and folders available in the directory:

Now execute the below command to copy multiple directories using the CP command:

Here we used the following command to copy Linuxtips and Linuxtricks into the LinuxOS folder:

Finally, we verified that the system copied the directories correctly.

Take a Backup when Copying a File

We can use the -b flag to back up the file if someone overwrites the copied file. It also creates a backup file by copying the file in place.

ls
Filename.txt Filename1.txt
cp -b Filename.txt Filename1.txt
ls
Filename.txt Filename1.txt Filename1.txt~

In the above example, Linuxhintteam.txt ~ represents the backup file.

Prompt for Confirmation when Copying a File

We can use the -i flag to prompt confirmation when we copy the file. Usually, a destination file is overwritten when using the CP command. This happens in that condition when the file is present at the time of copying. The command will prompt using the -i flag for overwriting the file.

Читайте также:  Горячие клавиши терминале linux

We can pass the -l flag while creating a hard link instead of copying with the help of the cp command. A new file is created by not copying the file, which is a hard link to the data on the disk. This is a primer on soft or symbolic and hard links.

ls
Filename.txt
Filname1.txt
cp -l Filname.txt Filname1.txt
echo ‘Filename1 text’ > Filename1.txt
Filname.txt
Filename text

Preserve File Attributes

The preserve option is passed to preserve the file attributes (i.e., user ownership, group, and permissions) along with the properties to be preserved, by default, a mode that will preserve timestamp and ownership.

-rw——- 1 Linux users 0 Sept 14 04:00 bar.txt
cp —preserve bar.txt foo.txt
-rw——- 1 Linux users 0 Sept 14 04:00 foo.txt
-rw——- 1 Linux users 0 Sept 14 04:00 bar.txt

Display All Files Copied

The -v option is used in the Cp command to show the files that are copied. This option prints folders and files that are copied to standard outputs.

cp -R -v Filename Filename1
‘Filename’ -> ‘Filename1’
‘Filename/Filename.txt’ -> ‘Filename1/Filename.txt’
‘Filename/Filename1.txt’ -> ‘Filename1/Filename1.txt’

Conclusion

This article described how we could copy one or more files, folders, and directories using the CP command. We explained to you many ways to use the CP command for copying the files. If there is any query or questions in your mind regarding this article, then feel free to contact us.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.

Источник

Copying multiple specific files from one folder to another

I have a large folder of pictures (thousands), and I have a long list of files, by exact file name, that I need to copy to another folder. I want to know if there is a way I can select several specific files from this folder, by name, and copy them to another folder, using the terminal, without copying them individually?

Fyi, for copying specific folders use: cp -rp /copying/from/ path/to/folder , where p is for copying the folder permission.

7 Answers 7

Simply copy multiple files at once from command line

There are several ways you could achieve this. The easiest I have seen is to use the following.

cp /home/usr/dir/ /home/usr/destination/ 

The syntax uses the cp command followed by the path to the directory the desired files are located in with all the files you wish to copy wrapped in brackets and separated by commas.

Make sure to note that there are no spaces between the files. The last part of the command, /home/usr/destination/ , is the directory you wish to copy the files into.

or if the all the files have the same prefix but different endings you could do something like this:

Where file1,file2,file3 and file4 would be copied.

From how you worded the question I believe this is what you’re looking for but it also sounds like you might be looking for a command to read from a list of files and copy all of them to a certain directory. If that is the case let me know and i’ll edit my answer.

Читайте также:  Online virtual machines linux

Dealing with duplicates with python

So I wrote a little python script that I believe should get the job done. However, I am not sure how well versed you are in python (if versed at all) so I will try explaining how to use this script the best I can and please ask as many questions about it as you need.

import os,sys,shutil ### copies a list of files from source. handles duplicates. def rename(file_name, dst, num=1): #splits file name to add number distinction (file_prefix, exstension) = os.path.splitext(file_name) renamed = "%s(%d)%s" % (file_prefix,num,exstension) #checks if renamed file exists. Renames file if it does exist. if os.path.exists(dst + renamed): return rename(file_name, dst, num + 1) else: return renamed def copy_files(src,dst,file_list): for files in file_list: src_file_path = src + files dst_file_path = dst + files if os.path.exists(dst_file_path): new_file_name = rename(files, dst) dst_file_path = dst + new_file_name print "Copying: " + dst_file_path try: shutil.copyfile(src_file_path,dst_file_path) except IOError: print src_file_path + " does not exist" raw_input("Please, press enter to continue.") def read_file(file_name): f = open(file_name) #reads each line of file (f), strips out extra whitespace and #returns list with each line of the file being an element of the list content = [x.strip() for x in f.readlines()] f.close() return content src = sys.argv[1] dst = sys.argv[2] file_with_list = sys.argv[3] copy_files(src,dst,read_file(file_with_list)) 

This script should be relatively simple to use. First off, copy the above code into the program gedit (should be pre-installed in Ubuntu) or any other text editor.

After that is complete, save the file as move.py in your home directory (it can be any directory but for ease of instruction lets just use the home directory) or add the directory the file is contained in to your PATH. Then cd to your home directory (or whatever directory you saved move.py in) from the terminal and type the following command:

python move.py /path/to/src/ /path/to/dst/ file.txt 

This should copy all of the files that are listed from the source directory to the destination directory with duplicates taking the format pic(1).jpg, pic(2).jpg and so on. file.txt should be a file that lists all the pictures you would like to copy with each entry on its own separate line.

In no way should this script effect the source directory, however just make sure to enter the correct paths to the source and destination directory and the worst that could happen is you copy the files to the wrong directory.

Notes

  • This script assumes that all of the original pictures are in the same directory. If you want it to check sub directories as well the script will need to be modified.
  • If you accidentally mistype a file name, the script will spit out the error
    «file does not exist» and prompt you to «press enter» to continue and the script will continue copying the rest of the list.
  • Don’t forget the trailing / on both the path to the source
    directory and path to the destination directory. Otherwise the script will spit an error back at you.

Источник

Оцените статью
Adblock
detector