Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
How to Edit Multiple Files in Vi Editor in Linux
Next article icon

How to Copy a File to Multiple Directories in Linux

Last Updated : 05 Jul, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will discuss how to copy a file to multiple directories in Linux. xargs, find, tee, shell loop, and parallel are the commands that can help you to copy a File to Multiple Directories.

Suppose we have a file with the name “xyz.txt” and we want to copy it into 3 directories with the name dir1, dir2, and dir3 then we use the following methods:

Using xargs

xargs command on Unix/Linux operating system converts input from standard input into an argument list for a specified command.

Syntax:

xargs -n 1 cp -v xyz.txt<<<"dir1 dir2 /home/kalilinux/dir3"                           OR  echo "dir1 dir2 /home/kalilinux/dir3" | xargs -n 1 cp -v xyz.txt

Output:

Using xargs

Using xargs

In the second command, working will be the same as the first one but the target folders and directory(dir1,dir2,dir3) are first echoed and then fed as input to the command xargs.

Output:

Using xargs

Using xargs

  • -n 1:  It is instructing xargs to use one argument per command line at a time.
  • cp: It is basic copy command.
  • -v: Display informative messages as the copy is performed.
  • xyz.txt: It is the file name that we want to copy.
  • dir1 dir2 are the folders name and /home/kalilinux/dir3 is the directory name with its proper location and these folders and directory are the destinations where to copy that file.
  • Using the pipe operator |, the standard output of one command can be sent into the standard input of another.

Note: If the file to be copied exists already in one of the destination folders, the old file will be replaced without prompting the user.

It is compulsory to specify the full path for the directories otherwise you get the following message:

xargs

xargs 

You need to be in that directory where the folders or files are present for directly copying to that folder or file and don’t need to specify its full path or location. If you specify the full location of the file or folder then no matter where you are, you won’t get any errors.

Example:

 

Using find

Here, the command find initiates a search and allows actions to be performed based on the search results.

Syntax:

find dir1 dir2 /home/kalilinux/dir3 -maxdepth 0 -exec cp xyz.txt {} \;

Were,

  • dir1 dir2 are the folders name and /home/kalilinux/dir3 is the directory name with its proper location where we want to perform the copy action.
  • -maxdepth: Set the maximum number of levels (like here we have set 0) that find will descend into a directory tree when performing tests and actions.
  • -exec: It is used to perform User-defined actions. In addition to the predefined actions, we can also invoke arbitrary commands with the help of the -exec command.
  • cp: It is the basic copy command.

Output:

Using find

Using find

Using Shell loop

Here, we are using bash for loop to copy the xyz.txt file in the /home/kalilinux/dir2 /home/kalilinux/dir3 directories and also a folder dir1, directly where:

  • for: It is illustrating the folders and directories.
  • dest: It is used to create a stream for writing metadata objects to the file system.
  • dir1: It is the folder 
  •  /home/kalilinux/dir2  /home/kalilinux/dir3: These are the directories that are separated by the spaces that we give between them.
  • do: It is executing the cp command.
  • -v: It is used to display informative messages as the copy is performed.
  • ‘xyz.txt‘: It is the file name that we want to copy in the folder and directories.
  • done: is used to end the shell script.

Syntax:

for dest in dir1 /home/kalilinux/dir2 /home/kalilinux/dir3 ; do cp -v xyz.txt “$dest” ; done

Output:

Using Shell loop

Using Shell loop

 Using tee

The tee command reads standard input and copies it to both standard outputs and to one or more files.

Syntax:

tee /home/kalilinux/dir1/xyz.txt /home/kalilinux/dir2/xyz.txt /home/kalilinux/dir3/xyz.txt< xyz.txt

If you just want to read standard input and copy it into one or more files without showing the standard output then you can use >/dev/null at the end of the above command. For example:

tee /home/kalilinux/dir1/xyz.txt /home/kalilinux/dir2/xyz.txt /home/kalilinux/dir3/xyz.txt< xyz.txt >/dev/null

Output:

Using tee

Using tee

Note: It is a must to give any file name at the end of the destination after giving the directory’s location. It can be different from the name of the file that we copy into the directories or can be the same as the file name.

Using GNU parallel

First, you need to install this on your pc/laptop because it may not be pre-installed in your Linux operating systems. You can install it by using the command:

sudo apt install parallel

then after we use:

parallel cp -v xyz.txt ::: /home/kalilinux/dir1 /home/kalilinux/dir2 dir3

GNU Parallel is a shell utility for executing jobs in parallel which makes running the command against sets of data at the same time easier. Here we are using:

  • parallel: This keyword uses the GNU Parallel.
  • cp: to perform the copy task.
  • -v: to display informative messages as the copy is performed.

Output:

Using GNU parallel

Using GNU parallel

Conclusion

In this article we have discussed how to copy a file to multiple directories in Linux and how it can be achieved using various methods, such as utilizing the `cp` command with brace expansion, leveraging `find` and `xargs`. Each method provided flexibility and efficiency in different scenarios. Overall, we can say that after understanding these methods and adapting them to our specific requirements, we can save time and effort when dealing with the directories in Linux.



Next Article
How to Edit Multiple Files in Vi Editor in Linux

T

tusharndraj
Improve
Article Tags :
  • Linux-Unix
  • linux-command
  • Linux-file-commands

Similar Reads

  • How to Copy a Directory in Linux
    There is this famous quote for Linux by Jamie Zawinski that goes like this "Linux is only free if your time has no value." Certainly, that's true as people start using Linux they have to learn more about it, and when encountering it we have to solve it by ourselves, and that way we learn more about
    5 min read
  • How to Copy Files and Directories in Linux | cp Command
    The cp (copy) command is your go-to tool in Linux for duplicating files and folders quickly. Whether you’re backing up data, organizing files, or sharing content, cp lets you copy items between locations while keeping the original intact. The cp command requires at least two filenames in its argumen
    8 min read
  • How to Edit Multiple Files in Vi Editor in Linux
    Vi, short for "visual editor," is a powerful and ubiquitous text editor in the Linux ecosystem. Among its many features, one of the most useful is its capability to edit multiple files simultaneously. This ability streamlines the editing process, especially when dealing with projects that span multi
    4 min read
  • How to Edit Multiple Files in Vim Editor in Linux
    Vim, short for "Vi Improved," is a highly configurable and powerful text editor built upon the foundation of Vi. Like its predecessor, Vim offers robust support for editing multiple files simultaneously, making it an indispensable tool for developers, system administrators, and anyone working extens
    4 min read
  • How to Manage Directories in Linux?
    Directories in Linux or any operating system help to organize the data and files, which is then easier to find and manage. In this article, you will learn everything you will need to manage directories in Linux. We will try to cover every topic from creating, and copying to deleting the directories
    6 min read
  • How to Count Files in Directory Recursively in Linux
    When exploring directories on your server, you may have come across folders with a large number of files in them. You might wish to know how many files exist in a certain directory or across many directories at times. To put it another way, you wish to count the number of files saved in a directory
    5 min read
  • How to Check the Size of a Directory in Linux
    Many times during working on Linux we may encounter situations, where we want to check the total size occupied by a directory, especially when working with large files and directories. Knowing the file or directory size can help a user perform tasks like storage allocation, size comparison, etc. At
    7 min read
  • How to Sync File and Directories to Cloud Storage in Linux Using Rclone Tool?
    Rclone is a command-line tool for synchronizing files and directories between Google Drive, Amazon S3, Openstack Swift / Rackspace cloud files / Memset Memstore, Dropbox, Google Cloud Storage, and the local filesystem. It is a single binary file that contains a Go program. Rclone is an MIT-licensed
    3 min read
  • How to Create a File in the Linux Using the Terminal?
    In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files. Using the touch commandUsing the cat commandUsing redirection operatorUsing the echo commandUsing the heredocUsing the dd c
    4 min read
  • How to compress file in Linux | Compress Command
    Linux, renowned for its powerful command-line utilities, provides users with various tools to streamline file management tasks. One such tool, the compress `command`, is specifically designed to compress individual files, reducing their size for efficient storage and transfer. In this comprehensive
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences