How to copy files from the folder without the folder itself
Solution 1
advanced cp
cp -r /home/username/A/. /usr/lib/B/
This is especially great because it works no matter whether the target directory already exists.
shell globbing
If there are not too many objects in the directory then you can use shell globbing:
mkdir -p /usr/lib/B/
shopt -s dotglob
cp -r /home/username/A/* /usr/lib/B/
rsync
rsync -a /home/username/A/ /usr/lib/B/
The /
at the end of the source path is important; works no matter whether the target directory already exists.
find
mkdir -p /usr/lib/B/
find /home/username/A/ -mindepth 1 -maxdepth 1 -exec cp -r -t /usr/lib/B/ {} +
or if you don't need empty subdirectories:
find /home/username/A/ -mindepth 1 -type f -exec cp --parents -t /usr/lib/B/ {} +
(without mkdir
)
Solution 2
If on a GNU system, from man cp
:
-T, --no-target-directory
treat DEST as a normal file
This allows you to write cp -rT /home/username/A/ /usr/lib/B/
to do exactly the right thing.
Solution 3
Tell cp
to copy the directory's contents and not the directory itself:
sudo cp -r /home/username/A/* /usr/lib/B/
Related videos on Youtube
![pushandpop](https://i.stack.imgur.com/hqWzA.jpg?s=256&g=1)
Comments
-
pushandpop almost 2 years
I'm trying to copy files and subfolders from A folder without the A itself. For instance, A folder contains next:
| file1.txt | file2.txt | subfolder1
Executing next command gives me wrong result:
sudo cp -r /home/username/A/ /usr/lib/B/
The result is
/usr/lib/B/A/...copied files...
instead of..
/usr/lib/B/...copied files...
How can I reach the expected one without origin-folder
-
pushandpop over 9 yearsThanks! But it says: /usr/lib/B/ is not a directory
-
talkloud over 9 yearsYou will need to
shopt -s dotglob
for this to work if there are any dotfiles in/home/username/A/
. -
terdon over 9 years@pushandpop well, yes. That's the target you had in your question so I assumed it was a directory. You need to create the target before attempting to copy files into it.
-
pushandpop over 9 yearsThe first one works just fine! Any ideas why home/username/A/* (with star-symbol) doesn't make sense? Variant with dot at the end helped me, thanks!
-
Stéphane Chazelas over 9 yearsYou also want
-maxdepth 1
(-mindepth
and-maxdepth
being GNU extensions now also supported by a few others. Portablyfind .../. ! -name . -prune -exec ....
) -
Hauke Laging over 9 years@StéphaneChazelas I guess there is a typo somewhere.
find .../.
causes an error here. -
Stéphane Chazelas over 9 years
...
was an ellipsis for/home/username/A
. It shouldn't give you a syntax error though, just a file not found one (unless you have a directory called...
). -
noraj over 5 yearsThis should be the accepted answer, this is proper than shell globbing or using something else than
cp
. But that's true that-T
won't work with a non-GNUcp
. -
Veverke over 3 yearsis adding
*
after the/
is mandatory ? seems to work without it -
Veverke over 3 yearsis adding
.
after/
mandatory ? seems to work without it -
terdon over 3 years@Veverke without it, you copy the directory. With it, you copy only what is inside the directory.
-
Veverke over 3 years@that's why I asked, I always thought the same - my script had /*, but using only / seems to copy the files inside it (and adding it to a cp will create the dir as well). I am a beginner in linux but this is what I see when looking at the results running the command with both syntaxes.
-
terdon over 3 years@Veverke please post a new question if you need more details and show us the exact commands you used. That said, if you run
cp -r foo/* bar
one of two things will happen: ifbar
does not exist, or if it exists but is not a directory, you will get an error message. If it does exist and is a directory, then all non-hidden files/dirs fromfoo
will be copied intobar
. If you runcp -r foo/ bar
, then ifbar
exists and is a directory, that will copy the directoryfoo
and place it as a subdirectory ofbar
. Ifbar
does not exist or is not a directory, you will get an error. -
Hauke Laging over 3 years@Veverke It does work if you run the command only once (and know for sure that
B
doesn't exist). But run both commands twice and compare the results.