Copy Files to Multiple Directories
Copy Files to Multiple Directories

Copy Files to Multiple Directories


Copy Files to Multiple Directories

While learning Linux, it is always the norm for newbies to keep typing several commands to accomplish a simple task. This is understandable especially when one is just accustomed to using the terminal.

However, as you look forward to becoming a Linux power user, learning what I would refer to as “shortcut commands” can significantly reduce time-wasting tendencies.

In this article, I will explain an easy way, using a single command to copy a file into multiple directories in Linux.

In Linux, the cp command is used to copy files from one directory to another, the easiest syntax for using it is as follows:

# cp [options….] source(s) destination

Alternatively, you can also use the advanced-copy command, which shows a progress bar while copying large files/folders in Linux.

Consider the commands below, normally, you would type two different commands to copy the same file into two separate directories as follows:

# cp -v /etc/hosts /home/sophia/backup

# cp -v /etc/hosts /tmp/backup

Assuming that you want to copy a particular file into up to five or more directories, this means you would have to type five or more cp commands.

To do away with this problem, you can employ the echo command, a pipe, and xargs command together with the cp command in the form below:

$ echo /home/sophia/backup/ /tmp/backup |xargs -n 1 cp -v /etc/hosts

In the form above, the paths to the directories (dir1,dir2,dir3…..dirN) are echoed and piped as input to the xargs command where:

-n 1 – tells xargs to use at most one argument per command line and send to

the cp command.

cp – used to copy a file.

You can also copy multiple files from source to different destinations for example:

$echo /home/sophia/backup/ /tmp/backup |xargs -n 1 cp -v /etc/hosts*

In the form above, all files that start with "hosts" such as "hosts.allow", "hosts.deny" will copy to different destinations.

'/etc/hosts' -> '/home/sophia/backup/hosts'

'/etc/hosts.allow' -> '/home/sophia/backup/hosts.allow'

'/etc/hosts.deny' -> '/home/sophia/backup/hosts.deny'

'/etc/hosts' -> '/tmp/backup/hosts'

'/etc/hosts.allow' -> '/tmp/backup/hosts.allow'

Try to read through the man pages of cp, echo and xargs commands to find useful and advanced usage information:

man cp

man echo

man xargs

Please comment with your point of view and re-share this article with your network.




要查看或添加评论,请登录

Sophia Alikhani的更多文章

  • Secure Files/Directories using ACLs (Access Control Lists) in Linux

    Secure Files/Directories using ACLs (Access Control Lists) in Linux

    Secure Files/Directories using ACLs (Access Control Lists) in Linux Let’s say, you have three users, ‘student1‘…

  • Linux Process & Threads

    Linux Process & Threads

    We always hear people using two terms very often. One is ?Process? and the other is ?thread?.

  • PAM-The Login access control table

    PAM-The Login access control table

    The Login access control table On a server environment where authorized and legitimate logins can come from everywhere,…

  • PAM-Controlling access time to services

    PAM-Controlling access time to services

    Controlling access time to services As the Linux-PAM system said, running a well-regulated system occasionally involves…

  • PAM-Disable Console Access

    PAM-Disable Console Access

    Tighten console permissions for privileged users The console.perms security file of Linux, which use the pam_console.

  • Blocking su to root

    Blocking su to root

    Blocking; su to root, by one The su (Substitute User) command allows you to become other existing users on the system…

  • #Hardening #Security #Tips for #Linux #Servers

    #Hardening #Security #Tips for #Linux #Servers

    1. Physical System Security Configure the BIOS to disable booting from CD/DVD, External Devices, Floppy Drive in BIOS.

    1 条评论
  • Linux Physical Memory Concept: Zone

    Linux Physical Memory Concept: Zone

    Zones Each zone is described by a struct zone_struct. zone_structs keep track of information like page usage…

    2 条评论
  • Linux physical memory concept:NODE

    Linux physical memory concept:NODE

    Nodes As I have mentioned, each node in memory is described by a pg_data_t, which is a typedef for a struct…

    1 条评论
  • Describing Physical Memory in Linux

    Describing Physical Memory in Linux

    Describing Physical Memory Linux is available for a wide range of architectures, so an architecture-independent way of…

社区洞察

其他会员也浏览了