Try:
cp main/*/* /path/to/otherfolder/
If you want to be warned before overwriting a file, use the -i option:
cp -i main/*/* /path/to/otherfolder/
Answer from John1024 on Stack OverflowTry:
cp main/*/* /path/to/otherfolder/
If you want to be warned before overwriting a file, use the -i option:
cp -i main/*/* /path/to/otherfolder/
If I understood your requirement correct, you can try this find command:
cd main
find . -mindepth 1 -type f -exec cp '{}' /dest/dir +
Assuming your filenames are not duplicate across the sub folders in main directory.
Videos
Try using xargs:
cat list.txt | xargs -J % cp % new_folder
Update:
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
No need for cat at all:
xargs -a list.txt cp -t new_folder
Or using long options:
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.
Bash:
for file in $(<list.txt); do cp "$file" new_folder; done
or
cp $(<list.txt) new_folder
sh (or Bash):
# still no cat!
while read -r line; do for file in $line; do cp "$file" new_folder; done; done < list.txt
or
# none here either
while read -r line; do cp $line new_folder; done < list.txt
or
# meow
for file in $(cat list.txt); do cp "$file" new_folder; done
or
# meow
cp $(cat list.txt) new_folder
If this is for interactive use: type and run the first command normally. Then type cd Space M-. (you can type M-. either as Esc . or as Alt+.). The keyboard shortcut M-. (yank-last-arg) inserts the last word of the previous command.
If this is for scripting: put the directory name in a variable. Don't forget double quotes when you use the variable.
target_dir=~/my_first_subfolder/my_second_subfolder/more_subfolders
cp fileFromMyFriend.txt "$target_dir"
cd "$target_dir"
If you find that you use the same sequence of commands a lot, define a function. The bash function below calls cp and treats the last argument as a directory to change to. If the last argument is not a directory or a symbolic link to a directory, it changes to the directory containing the last argument. This works for most ways to use cp, but not with the -t option to GNU cp.
cp_cd () {
cp "$@"
if [ -d "${!#}/." ]; then
cd -- "${!#}"
else
cd -- "$(dirname -- "${!#}")"
fi
}
I'm going to just answer this question of yours: Rather I'm trying to avoid retyping my very long folder path.
If that's what you are trying to do, you can just do this:
cp fileFromMyFriend.txt ~/my_first_subfolder/my_second_subfolder/more_subfolders
cd !$
1) By using -i for interactive you will be asked if you would like to replace the file:
cp -i /home/levan/kdenlive/untitelds.mpg /media/sda3/SkyDrive/
or you can use -b to create a backup of your file:
cp -b /home/levan/kdenlive/untitelds.mpg /media/sda3/SkyDrive
2) Same as the above:
cp (-i or -b) /media/sda3/SkyDrive/untitelds.mpg /home/levan/kdenlive
3) Use -R for recursive and -i for interactive:
cp -Ri ~/MyFolder /sda3/
4) This last one can be done via the mv command, move is like cutting:
mv -i ~/MyFile ~/OtherFolder/MyFile
if you want to move a directory, use:
mv -Ri ~/MyDirectory ~/OtherDirectory/
When ~/Dropbox/RECENT/ is your current directory:
cp input.txt SORT/
And I want to copy
input.txtwith another name in my current directory.
Again with ~/Dropbox/RECENT/ as current directory:
cp input.txt newname.txt
Existing filenames can be auto-completed using TAB.
Long version of the same copy command (when you are not in ~/Dropbox/RECENT/):
cp /home/$USER/Dropbox/RECENT/input.txt /home/$USER/Dropbox/RECENT/SORT/
I put a / behind every directory. If SORT does NOT exist a cp will also create a file named SORT making you think something went wrong. Adding the / will have cp error out and not copy the file.
You can use cp with the -r (copy recursively) flag and specify the new directory name:
cp -r /path/to/directory /path/to/location/new-name
In the above command replace the given paths with the real ones.
For example, to copy stuff from my home directory to an existing directory named backup and name the new directory stuff-backup (if this directory already exists, note that stuff will be copied into it, not overwrite it), you run:
cp -r ~/stuff ~/backup/stuff-backup
~ is a shortcut for your home directory /home/$USER or /home/zanna in my case. You can omit it if the current working directory is your home directory - you can see this from your prompt
zanna@monster:~$
^--the ~ here is where I am - home!
You can add the -v (verbose) flag to make cp report each copy being performed:
$ cp -vr stuff backup/stuff-backup
'stuff/thing1' -> 'backup/stuff-backup/thing1'
'stuff/thing2' -> 'backup/stuff-backup/thing2
...
The command you need is simply cp which stands for "copy".
You can use it for example with one of these syntaxes:
cp SOURCEFILE TARGETFILE
cp SOURCEFILE TARGETDIRECTORY
The first variant allows you to specify a new file name for the target file, while the second variant creates a copy with the same name in the target directory. You must of course substitute the place holders in capital letters with valid paths first.
However, cp by default operates on files only, not on directories. To get it to copy a complete directory with all its content recursively, you must add the -r option:
cp -r SOURCEDIRECTORY TARGETDIRECTORY
You can learn more about the cp command by typing man cp in your terminal.