Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8036493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:38:21+00:00 2026-06-05T02:38:21+00:00

I have a folder that contains sub-directories A,B,C and D . I need to

  • 0

I have a folder that contains sub-directories A,B,C and D. I need to copy directories A and D to another directory called 'copy' while excluding B and C(i.e. B and C doesn’t get copied over). I was thinking about doing the following (in command-line pseudocode):

ls (selective ls on the source directory) | 
   scp -r {returned value from the ls} {target directory}

Is there a Linux command-line way to accomplish the above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:38:22+00:00Added an answer on June 5, 2026 at 2:38 am

    The simple answer is to only copy the directories you want:

    scp -r A D anotherhost:/path/to/target/directory
    

    This will do exactly what you’ve described in your example. A more general solution might look something like this:

    scp -r $(ls | egrep -v '^(B|C)$') anotherhost:/path/to/target/directory
    

    This command will work as long as the number of files in your source directory is not large. As the number of files goes up, you’ll eventually run into a “command too long” error.

    Instead of using scp, you could use rsync, which has a variety of mechanisms for including/excluding files. For example:

    rsync --exclude='B/' --exclude='C/' . anotherhost:/path/to/target/directory
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a folder/directory that contains some sub directories. Only those sub folders contain
I have a script that is copying a folder that contains a couple sub
I have the path /home/mine/new that contains files and sub directories. I want to
I've an ASP.NET MVC project that has a sub folder called emails. This contains
I have a folder that contains 1000+ sub folders. In each subfolder there are
I have an application that contains a sub folder that contain xml file ,that
I have a folder that contains multiple word documents. I need to monitor this
I have a folder that contains more than 200K images. Some images will follow
I have a folder that contains many different files: folder1: somthing.php somthingelse.php blah.php ect
I have a folder \folder\ above the webroot that contains .php , .inc ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.