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.
What you want is:
cp -R t1/. t2/
The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.
cp dir1/* dir2
Or if you have directories inside dir1 that you'd want to copy as well
cp -r dir1/* dir2