Linux copy all txt files

Copy list of files

I have a list of files separated by spaces in a file list.txt . I’d like to copy them to a new folder. I tried to do:

cp `cat list.txt` new_folder 

but it did not work. How would you do this ? Update: Thank you for all your very interesting answers. I did not ask for that much 🙂 After having tried the solution above a second time, it seems that cp printed the usage help because the folder new_folder did not exist yet! Sorry, it seems to be a stupid mistake. When I create the folder before, it works. However, I have learnt much from all your answers and solutions, thank you for the time you take to write them.

Let me get one thing clear: the list has all the file names on one line, separated only by a space? Or are the names each on a separate line?

4 Answers 4

cat list.txt | xargs -J % cp % new_folder 

I did this on OS X which has an different version than GNU/Linux versions. The xargs which comes from GNU findutils doesn’t have -J but it has -I which is similar (as Dennis Williamson pointed out in a comment). The OS X version of xargs has both -I and -J which have slightly different behaviors — either will work for this original question.

$ cat list.txt one two.txt three.rtf $ cat list.txt | xargs -J % echo cp % new_folder cp one two.txt three.rtf new_folder $ cat list.txt | xargs -I % echo cp % new_folder cp one new_folder cp two.txt new_folder cp three.rtf new_folder 

My xargs doesn’t have -J it has -I which seems to do the same thing. What version of xargs and what OS/distribution do you have?

I tested this on Mac OS X 10.6 (aka snow leopard), so it’s whatever version of xargs ships with the OS. Running xargs —version returns a usage error message. It’s likely a BSD version of xargs. The use of -I and -J are subtly different. I’ll update my answer with nicely formatted examples.

Using same above command with necessary changes only, how do I copy the files along with creating their directories and subdirs they are in, separated by slash ?

xargs -a list.txt cp -t new_folder 
xargs --arg-file=list.txt cp --target-directory=new_folder 

Here are some shell versions. Note that some variables are unquoted or for loops are used specifically because the OP specified that the filenames are space delimited. Some of these techniques won’t work for filename-per-line input in which filenames contain white space.

# still no cat! while read -r line; do for file in $line; do cp "$file" new_folder; done; done < list.txt 
# none here either while read -r line; do cp $line new_folder; done < list.txt 
# meow for file in $(cat list.txt); do cp "$file" new_folder; done 
# meow cp $(cat list.txt) new_folder 

@YiboYang: The -t is an option of cp . It copies all source arguments into the specified target directory. Here, it allows the target to be specified before the source which is added by the xargs command.

This is useful but what if list.txt contains mixed files and folders path ? In that case the command doesn't works properly. It reports "omitting directory . " and if -R is specified to the cp command then the "is not a directory" error is reported as soon as the first file in the list is encountered.

Читайте также:  Canon scan utility linux

I had an embarrassingly round-about answer here earlier, but Denis's answer reminded me that I missed the most basic thing. So, I deleted my original answer. But since nobody has said this very basic thing, I think it's worth putting it here.

The original question is "I have a text file with a list of space-separated names of files. How can I copy those to one target directory." At first this might seem tricky or complicated, because you think you have to somehow extract the items from the file in a specific way. However, when the shell processes a command line, the first thing it does is separate the argument list into tokens and (here's the bit nobody has said outright) spaces separate tokens. (Newlines also separate tokens which is why Doug Harris's test with a newline separated list had the same result.) That is, the shell expects and can already handle a space-separated list.

So all you need to do here is put the space-separated list (that you already have) into the right place in your command. Your command is some variation on this:

cp file1 file2 file3. file# target 

The only wrinkle is that you want to get the list of files 1 through # from your text file.

As Dennis points out in his comment, your original attempt ( cp cat list.txt new_folder ) should have worked already. Why? Because the internal command cat list.txt is processed first by the shell and expands into file1 file2 file3. file# , which is exactly what the shell expects and wants at that part of the command. If it didn't work then either (1) you had a typo or (2) your filenames were somehow strange (they had spaces or other unusual characters).

The reason that all Dennis's answers work is simply that they provide the necessary list of files for cp to work on, placing that list where it belongs in the entire command. Again, the command itself is this in structure:

cp list-of-files target_directory 

It's easy to see how this all comes together in this version:

$() causes the shell to run the command inside the parentheses and then substitute its output at that point in the larger line. Then the shell runs the line as a whole. By the way, $() is a more modern version of what you were already doing with backticks (`). Next: < is a file redirection operator. It tells the shell to dump the contents of list.txt to standard input. Since the $() bit gets processed first, here's what happens in stages:

Obviously step 2 is simply the normal cp command you wanted.

I realize that I'm beating this (perhaps very dead) horse a lot, but I think it's worth doing. Understanding exactly how the shell processes a command can help you to write it better and simplify a lot. It will also show you where problems are likely to be hiding. In this case, for example, my first question to you should have been about funny filenames or a possible typo. No acrobatics were necessary.

Источник

How to Copy All Files from a Directory to another Directory in Linux

Copying the file or folder means creating a new file having duplicate content as in the existing file.

Читайте также:  Linux get number lines

Sometimes, we need to copy the files or folders rather than having a backup program. The files can be copied with the same name, or you can change the name as well.

Copying a file, folder, or directory is a simple and basic task in the Linux operating system. Rename, delete or copy commands are used as daily purpose operations while working with the command-line interface.

Although there are multiple commands to copying the files, the “cp” and “rsync” command are widely used simplest approaches.

How to Copy Files with “cp” Command in Linux:

The “cp” command is one of the commonly used commands to perform the copy operation. You can copy files or folders from source to destination, i-e, one directory through this command.

The syntax of the “cp” command is:

Let’s take a look at an example to understand the “cp” command tool better.

In the home directory, create a “temp” folder with the text file named “text_file1.txt” and add random content to it.

Copy a file with the same name:

To copy a “text_file1.txt” file directory with the same name, open the terminal and type the mentioned “cp” command with the right path.

Get the folder’s path by right-clicking on the file and navigate to the “Properties” option (it is the easy way to get the path link).

A dialogue box will open with the complete path of a text file:

Use this path with the “cp” command to copy file:

This command will copy the “text_file1.txt” file to the “temp2” folder.

To verify it, type the “ls” command in the terminal:

Copy a file with a different name:

To copy the file in the current working directory with the different name, type the following “cp” command with file location:

Verify it using the “ls” command”:

Copy Multiple Files with “cp” Command:

To copy multiple files with the “cp” command, navigate the terminal to the directory where files are saved and then run the “cp” command with the file names you want to copy and the destination path.

$ cp text_file1.txt text_file2.txt text_file3.txt / home / wardah / temp2

Run the mentioned command to verify if files are copied successfully:

Above mentioned scenarios are how to copy a single or selected file in a directory. Now, use the wildcard character (*) to copy present files of one directory to any other specific directory.

Run the “ls” command to check how many files exist in the temp directory:

Instead of mentioning all file names in the terminal, use the wildcard (*) with the directory path to copy all the files into destination:

Now, run the “ls” command again to check if all files are copied in the “temp2” directory:

How to Copy files with the “rsync” command in Linux:

The “rsync” command is another versatile Linux tool to synchronize and copy files and directories locally as well as remotely.

The syntax of the “rsync” command is to copy files is:

It is a pre-built tool in many Linux distribution. However, if you don’t get it on your system, install it by executing the following command:

Читайте также:  Firefox touch screen linux

To copy a file from one place to another, run the following command:

To copy all the directory files to another location, the command would be:

(The “-a” with the “rsync” command is used to copy directories recursively)

Here are the two concepts:

If you add a trailing slash (/) with the path, it will copy the content of the source directory to the destination directory, just like shown in the image:

But, if you don’t add it, it will copy the source directory inside the destination directory, like:

The above command will copy a “dir1” directory to the “dir2” directory.

Conclusion:

Copying a file or directory is the basic command one can operate. One can use it multiple times while using Linux operating system.

This guide has seen the two simplest approaches, the “cp” command and the “rsync” command. Using these commands, we have learned how to copy a single file, multiple files, and even copy one directory to another.

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.

Источник

Copy .txt files in a certain directory

I have an issue in copying my file in my directories. I have .txt and .jpeg files in a lot of directories, and I want to copy only the .txt files according to the directory. For example, I have this:

direct/direct1 direct/direct2 direct1: file.txt file2.txt file.jpeg file2.jpeg direct2: file3.txt file4.txt file3.jpeg file4.jpeg 

You mean cp direct/direct*/*.txt /destination or printf '%s\0' direct/direct*/*.txt | pax -rw0 /destination ?

2 Answers 2

If you want to copy all the .txt files in a directory, use a wildcard pattern:

cp direct/direct1/*.txt target 

This copies all the .txt files that are in the directory direct/direct1 to the directory target (which must already exist).

You can pass multiple patterns to copy files from multiple directories:

cp direct/direct1/*.txt direct/direct2/*.txt target 

If you want to copy the .txt files from all the subdirectories of direct , you can use a wildcard for the directory as well:

If you only want to copy from certain directories, you can use a wildcard pattern that matches only these directories. For example, if direct has four subdirectories foo , bar , baz and qux and you only want to copy files from bar and baz , you can use

None of the examples so far copy files from direct itself, or from subsubdirectories of direct . If you want to copy the .txt files from direct , you need to include it in the list, e.g.

cp direct/*.txt direct/*/*.txt target 

If you want to copy files from direct , all of its subdirectories, all of their subdirectories, and so on recursively, you can use the ** wildcard, if your shell supports it. It works out of the box in zsh, but in bash, you need to enable it first with shopt -s globstar .

Note that all the files are copied into target itself, this does not reproduce the directory structure. If you want to reproduce the directory structure, you need a different tool, such as rsync (tutorial) or pax.

rsync -am --include='*.txt' --include='*/' --exclude='*' direct/ target/ cd direct && pax -rw -pe -s'/\.txt$/&/' -s'/.*//' . target/ 

Источник

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