Linux copy files with path

Find and copy files

If your intent is to copy the found files into /home/shantanu/tosend , you have the order of the arguments to cp reversed:

find /home/shantanu/processed/ -name '*2011*.xml' -exec cp "<>" /home/shantanu/tosend \; 

Please, note: the find command use <> as placeholder for matched file.

find -iname ‘*.mp3’ -mtime -1 -exec cp <> /home/my_path/ \; is there anything wrong with this command ? it’s not working

In Ubuntu 18 the curly braces also have to be put into single quotes: find -iname ‘*.mp3’ -mtime -1 -exec cp ‘<>‘ /home/my_path/ \;

i faced an issue something like this.

Actually, in two ways you can process find command output in copy command

  1. If find command’s output doesn’t contain any space i.e if file name doesn’t contain space in it then you can use below mentioned command: Syntax: find | xargs cp -t Example: find -mtime -1 -type f | xargs cp -t inner/
  2. But most of the time our production data files might contain space in it. So most of time below mentioned command is safer: Syntax: find -exec cp ‘<>‘ \; Example find -mtime -1 -type f -exec cp ‘<>‘ inner/ \;

In the second example, last part i.e semi-colon is also considered as part of find command, that should be escaped before press the enter button. Otherwise you will get an error something like this

find: missing argument to `-exec' 

In your case, copy command syntax is wrong in order to copy find file into /home/shantanu/tosend . The following command will work:

find /home/shantanu/processed/ -name '*2011*.xml' -exec cp <> /home/shantanu/tosend \; 

Источник

copy the file and keep full path

I need to copy the file to destination folder with it’s full path. I can do it on Linux (Red Hat/Centos) easily like this:

cp --parents /some/path/to/file /newdir 

I’d need exactly the same feature on AIX 6.1. I tried few things but didn’t succeed yet. Any ideas for handy command to do the job?

If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!

3 Answers 3

AIX’s native cp utility does not include the —parent option, as you’ve discovered.

Читайте также:  Find files linux extension pack

One option would be to install and use rsync from the AIX Toolbox for Linux Applications software collection. You’d also need to install the popt RPM (as a dependency of rsync).

rsync -R /some/path/to/file /newdir/ 

To end up with /newdir/some/path/to/file .

As a home-grown option, you could write a wrapper function using ksh93 (for the array support) to emulate the behavior. Below is a bare-bones function as an example; it assumes you want to copy files with relative paths, and does not support any options:

relcp() < typeset -a sources=() [ "$#" -lt 2 ] && return 1 while [ "$#" -gt 1 ] do sources+=("$1") shift done destination=$1 for s in "$" do if [ -d "$s" ] then printf "relcp: omitting directory '%s'\n" "$s" continue fi sdir=$(dirname "$s") if [ "$sdir" != "." ] && [ ! -d "$destination/$sdir" ] then mkdir -p "$destination/$sdir" fi cp "$s" "$destination/$sdir" done unset sources s sdir > 

Источник

Copy files and folders with full path in linux bash

I was traying to make a simple script that takes a file or folder path and copies it, preserving the full path, in a folder called system next to the script. Exaple: Imagine I just have this folders in my system:

/home/my_user/copy_script /home/my_user/folder1/file1 /home/my_user/folder1/file2 /home/my_user/file3 

If I pass as arguments the paths /home/my_user/folder1 and /home/my_user/file3 I want to get this folder structure as output: Files before runing the script

/home/my_user/copy_script /home/my_user/folder1/file1 /home/my_user/folder1/file2 /home/my_user/file3 
/home/my_user/system/home/my_user/folder1/file1 /home/my_user/system/home/my_user/folder1/file2 
/home/my_user/system/home/my_user/file3 

The idea to have a copy of the config files in a folder that will be sync with gitlab preserving the same structure as my system. My script till now looks like this:

#!/bin/bash #need to implement relative paths #https://stackoverflow.com/questions/3963716/how-to-manually-expand-a- special-variable-ex-tilde-in-bash/29310477#29310477 #clears the old folder rm -r ./system #goes through all the arguments for var in "$@" do #It is a folder if [[ -d $var ]]; then mkdir -p "$PWD/system/$var" cp -R $var "$PWD/system/$var/.." echo "$var FOLDER" #It is a file elif [[ -f $var ]]; then mkdir -p "$PWD/system/$var" cp -R $var "$PWD/system/$var/.." echo "$var FILE" #The path is not ok else echo "$var ERROR" fi done 

When it is a folder i think it works fine: Example: With the same folder structure as the beggining when we call copy_script /home/my_user/folder1 First creates the /home/my_user/system/home/my_user/folder1 path Then copies folder1 in /home/my_user/system/home/my_user/ But with files this does not work because it creates folders that I do not need and the I can’t copy the files. Example: calling copy_script /home/my_user/file3 will create /home/my_user/system/home/my_user/file3 path but then I can’t create the file because there is a folder with the same name. Could someone kindly explain me how to do this? Thanks.

Читайте также:  Terraria on linux steam

Источник

How can I copy the contents of a folder to another folder in a different directory using terminal?

I am trying to copy the contents of a folder to another folder in a different directory using terminal. Would somebody be able to provide me an example of the command line syntax required to achieve this?

8 Answers 8

You can copy the content of a folder /source to another existing folder /dest with the command

The -a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.

The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.

@DylanValade: -a already implies —preserve=all , that is wider than -p = —preserve=mode,ownership,timestamps .

@BennyNeugebauer: scp is used to copy over a network (through ssh ) and only encrypts the communication channel, not the files on the destination filesystem.

«The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.», this is not related to cp, but related to bash. The dot means «this location» and avoids the use of bash globbing, where by default files/directories starting with a . are not expanded.

Can someone explain the need behind use of mypath/. instead of the existing bash wildcard expansion character, mypath/* ?

rsync -a source/ destination 

The advantages of rsync are:

  1. After the initial sync, it will then copy only the files that have changed.
  2. You can use it over a network, convenient for files in $HOME, especially config files.

This will not copy hidden files, since bash expands * only to non-hidden file. The solution by @Joschua is safer.

If it’s a big folder you may wish to use one of these options to view progress while it’s copying askubuntu.com/questions/609303/…

Lets say you have a folder called folder1 in your ~ , inside folder1 is 1 file called file1 and 2 folders called sub1 and sub2 each with other files and folders inside them.

To copy all the contents of ~/folder1 to ~/new_folder1 you would use

new_folder1 would then contain all the files and folders from folder1 .

cp is the command to copy using a terminal, -r makes it recursively (so, current directory + further directories inside current) ~/folder1 is the origin folder, ~/new_folder1 is the destination folder for the files/folders inside the origin.

Читайте также:  Linux arch macbook pro

Thank you Bruno! It helped me to understand the syntax, though I had to change it a bit(removing ~ sign). Maybe because the destination folder was in /opt, which resides in another file system. And thank you Portablejim to remember the hidden file thing!

The trailing period is important. Without it, sometimes it may create a new subdirectory ~/new_folder1/folder1 instead of copying the contents over.

@Alex78191 [root@ home]# mkdir food [root@ home]# cd food/ [root@ food]# mkdir .fruit [root@ food]# mkdir veggies [root@ food]# touch veggies/carrots [root@ food]# touch .fruit/apple [root@ food]# ls * carrots [root@ food]#

Simple example.

Copy the directory dir_1 and its contents (files) into directory dir_2:

cp -r ./dir_1 ./dir_2 # or cp -r ./dir_1/ ./dir_2/ # Results in: ./dir_2/dir_1/_files_ 

Copy only the contents (files) of dir_1 into directory dir_2:

cp -r ./dir_1/. ./dir_2 # or cp -r ./dir_1/. ./dir_2/ # Results in: ./dir_2/_files_ 

_files_ is a placeholder for the actual files located in the directory.

Check this http://www.cyberciti.biz/faq/copy-folder-linux-command-line/ for more information on copying folder. Hope this helps.

cp is a Linux command for copying files and directories. The syntax is as follows:

cp source destination cp dir1 dir2 cp -option source destination cp -option1 -option2 source destination 

In this example copy /home/vivek/letters folder and all its files to /usb/backup directory:

cp -avr /home/vivek/letters /usb/backup 

-a : Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.

-v : Explain what is being done.

-r : Copy directories recursively. Example

Copy a folder called /tmp/conf to /tmp/backup:

This code with Flag «-R» copies perfectly all the contents of «folder1» to existing «folder2»:

Flag «-R» copies symbolic links as well but Flag «-r» skips symbolic links so Flag «-R» is better than Flag «-r».

-R, --dereference-recursive For each directory operand, read and process all files in that directory, recursively, following all symbolic links. 
-r, --recursive For each directory operand, read and process all files in that directory, recursively. Follow symbolic links on the command line, but skip symlinks that are encountered recursively. Note that if no file operand is given, grep searches the working directory. This is the same as the ‘--directories=recurse’ option. 

Источник

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