How to copy files from the folder without the folder itself

116,090

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/
Share:
116,090

Related videos on Youtube

pushandpop
Author by

pushandpop

T-1000 is my worsest nightmare.

Updated on September 18, 2022

Comments

  • pushandpop
    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
    pushandpop over 9 years
    Thanks! But it says: /usr/lib/B/ is not a directory
  • talkloud
    talkloud over 9 years
    You will need to shopt -s dotglob for this to work if there are any dotfiles in /home/username/A/.
  • terdon
    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
    pushandpop over 9 years
    The 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
    Stéphane Chazelas over 9 years
    You also want -maxdepth 1 (-mindepth and -maxdepth being GNU extensions now also supported by a few others. Portably find .../. ! -name . -prune -exec ....)
  • Hauke Laging
    Hauke Laging over 9 years
    @StéphaneChazelas I guess there is a typo somewhere. find .../. causes an error here.
  • Stéphane Chazelas
    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
    noraj over 5 years
    This 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-GNU cp.
  • Veverke
    Veverke over 3 years
    is adding * after the / is mandatory ? seems to work without it
  • Veverke
    Veverke over 3 years
    is adding . after / mandatory ? seems to work without it
  • terdon
    terdon over 3 years
    @Veverke without it, you copy the directory. With it, you copy only what is inside the directory.
  • Veverke
    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
    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: if bar 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 from foo will be copied into bar. If you run cp -r foo/ bar, then if bar exists and is a directory, that will copy the directory foo and place it as a subdirectory of bar. If bar does not exist or is not a directory, you will get an error.
  • Hauke Laging
    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.