{"id":21458,"date":"2023-09-30T12:22:03","date_gmt":"2023-09-30T12:22:03","guid":{"rendered":"https:\/\/linuxways.net\/?p=21458"},"modified":"2023-09-30T12:22:03","modified_gmt":"2023-09-30T12:22:03","slug":"how-do-i-copy-a-file-from-one-directory-to-another-in-ubuntu-terminal","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-do-i-copy-a-file-from-one-directory-to-another-in-ubuntu-terminal\/","title":{"rendered":"How do I Copy a File from One Directory to Another in Ubuntu Terminal?"},"content":{"rendered":"<p>The Ubuntu Terminal makes it more flexible to copy files and folders from one directory to another. As Linux is widely used by programmers and system administrators, copying files and folders from one place to another is a crucial task and may be hectic if copying is not done efficiently.<\/p>\n<p>This article explains how files can be copied from one directory to another using the Ubuntu Bash or Terminal.<\/p>\n<h2><strong>How do I Copy a File From One Directory to Another in Ubuntu Bash?<\/strong><\/h2>\n<p>The \u201c<strong>cp<\/strong>\u201d command in Ubuntu copies files and folders to other Directories more efficiently and easily. Copying Files can be performed in multiple methods with the \u201ccp\u201d command discussed below.<\/p>\n<h3><strong>Copy a File From One Directory to Another in Ubuntu Using the \u201ccp\u201d Command<\/strong><\/h3>\n<p>Copying files to other Directories from one Directory can be done with the \u201ccp\u201d command having the following syntax:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">cp<\/span> <span class=\"sy0\">&lt;<\/span>file-name<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>.<span class=\"sy0\">\/<\/span>destination-path<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>The \u201cfile-name\u201d is the file you want to copy from the current directory to another directory, which in the above syntax, is the destination path.<\/p>\n<p>Consider the example where you want to copy a file \u201cabc.txt\u201d from the current Directory to the FolderA in our case:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1656\" height=\"228\" class=\"wp-image-21459\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-1.png 1656w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-1-300x41.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-1-1024x141.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-1-768x106.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-1-1536x211.png 1536w\" sizes=\"auto, (max-width: 1656px) 100vw, 1656px\" \/><\/p>\n<p>To copy the \u201cabc.txt\u201d file, use the cp command followed by the file name or complete path (if you are not in the same\/current directory) and the destination directory path.<\/p>\n<p>If you are in the current \u201cDocuments\u201d directory, use the \u201c<strong>cp<\/strong>\u201d command as:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">cp<\/span> abc.txt .<span class=\"sy0\">\/<\/span>FolderA<\/div><\/div>\n<p>This will copy the \u201cabc.txt\u201d file from the Documents directory to the FolderA directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1157\" height=\"315\" class=\"wp-image-21460\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-2.png 1157w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-2-300x82.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-2-1024x279.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-2-768x209.png 768w\" sizes=\"auto, (max-width: 1157px) 100vw, 1157px\" \/><\/p>\n<p>The copied file can be verified by opening the FolderA directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1196\" height=\"180\" class=\"wp-image-21461\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-3.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-3.png 1196w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-3-300x45.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-3-1024x154.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-3-768x116.png 768w\" sizes=\"auto, (max-width: 1196px) 100vw, 1196px\" \/><\/p>\n<p>You can also verify using the Terminal by navigating to the FolderA directory using the \u201ccd\u201c command and then listing the files using the \u201cls\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">ls<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"221\" class=\"wp-image-21462\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-4.png 737w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-4-300x90.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/p>\n<p>To copy the file to a different directory (other than the current directory), you can mention the source file path with the \u201ccd\u201d command. In the example above, we will move the \u201cabc.txt\u201d file from the Home Directory to the \u201cdirB\u201d directory using the following cp command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">cp<\/span> .<span class=\"sy0\">\/<\/span>Documents<span class=\"sy0\">\/<\/span>abc.txt .<span class=\"sy0\">\/<\/span>Documents<span class=\"sy0\">\/<\/span>dirB<span class=\"sy0\">\/<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"738\" height=\"111\" class=\"wp-image-21463\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-5.png 738w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-5-300x45.png 300w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/p>\n<p>The file is moved to the \u201cdirB\u201d directory and can be verified using either the terminal or navigating to the directory using the GUI.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"186\" class=\"wp-image-21464\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-6.png 737w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-6-300x76.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/p>\n<p>Navigating to the directory through GUI, you will see the copied \u201cabc.txt\u201d file in the \u201cdirB\u201d directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1281\" height=\"138\" class=\"wp-image-21465\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-7.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-7.png 1281w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-7-300x32.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-7-1024x110.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-7-768x83.png 768w\" sizes=\"auto, (max-width: 1281px) 100vw, 1281px\" \/><\/p>\n<h3><strong>Copy Multiple Files from One Directory to Another Using the \u201ccp\u201d Command<\/strong><\/h3>\n<p>Multiple files can also be copied to other Directories. In our case, we have to copy the \u201cfileA.txt\u201d, \u201cfileB.txt\u201d, and \u201cfileC.txt\u201d from the Documents directory to the \u201cMultDir\u201d directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1223\" height=\"237\" class=\"wp-image-21466\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-8.png 1223w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-8-300x58.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-8-1024x198.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-8-768x149.png 768w\" sizes=\"auto, (max-width: 1223px) 100vw, 1223px\" \/><\/p>\n<p>To copy all three files, use the \u201ccp\u201d command as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">cp<\/span> fileA.txt fileB.txt fileC.txt .<span class=\"sy0\">\/<\/span>MultDir<\/div><\/div>\n<p>This will copy all three text files from the Documents Directory to the MultDir Directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"125\" class=\"wp-image-21467\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-9.png 734w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-9-300x51.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/p>\n<p>The copied files can be verified either with the command line or through GUI. List files in the \u201cMulttDir\u201d Directory with the \u201cls\u201d command:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">ls<\/span><\/div><\/div>\n<p>This will display all the files available in the directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"134\" class=\"wp-image-21468\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-10.png 734w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-10-300x55.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/p>\n<p>You can also verify through GUI by opening the directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1194\" height=\"134\" class=\"wp-image-21469\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-11.png 1194w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-11-300x34.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-11-1024x115.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-11-768x86.png 768w\" sizes=\"auto, (max-width: 1194px) 100vw, 1194px\" \/><\/p>\n<p><strong>Copy the Same Type of Files From One Directory to Another Using the \u201ccp\u201d Command<\/strong><\/p>\n<p>Multiple files with the same extension can also be copied to other Directories with the \u201ccp\u201d command. The syntax of the \u201ccp\u201d command for copying multiple files of the same type is:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">cp<\/span> .<span class=\"sy0\">&lt;\/<\/span>DirectoryA-Path<span class=\"sy0\">&gt;*<\/span>.txt .<span class=\"sy0\">&lt;\/<\/span>DirectoryB-Path<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>The *.txt will ensure all the files with the \u201c.txt\u201d extension are copied. Consider our case where we have multiple \u201c.txt\u201d files in the Documents directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1664\" height=\"284\" class=\"wp-image-21470\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-12.png 1664w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-12-300x51.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-12-1024x175.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-12-768x131.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-12-1536x262.png 1536w\" sizes=\"auto, (max-width: 1664px) 100vw, 1664px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>To copy all these files to the \u201cAllTextFiles\u201d Directory, use the \u201ccp\u201d command as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">cp<\/span> <span class=\"sy0\">*<\/span>.txt .<span class=\"sy0\">\/<\/span>AllTextFiles<\/div><\/div>\n<p>Upon doing so, all available \u201c.txt\u201d files will be copied to the \u201cAllTextFiles\u201d Directory and the cursor will move to the next line in the terminal:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"737\" height=\"106\" class=\"wp-image-21471\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-13.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-13.png 737w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-13-300x43.png 300w\" sizes=\"auto, (max-width: 737px) 100vw, 737px\" \/><\/p>\n<p>To verify, change the directory using the \u201c<strong>cd<\/strong>\u201d command and then list all the files using the \u201c<strong>ls<\/strong>\u201d command. All the files are copied in our case:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw3\">cd<\/span> AllTextFiles<br \/>\n<span class=\"kw2\">ls<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"734\" height=\"193\" class=\"wp-image-21472\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-14.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-14.png 734w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-14-300x79.png 300w\" sizes=\"auto, (max-width: 734px) 100vw, 734px\" \/><\/p>\n<p>You can also verify through the GUI by opening the directory:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1662\" height=\"172\" class=\"wp-image-21473\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-15.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-15.png 1662w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-15-300x31.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-15-1024x106.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-15-768x79.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-15-1536x159.png 1536w\" sizes=\"auto, (max-width: 1662px) 100vw, 1662px\" \/><\/p>\n<h2><strong>Bonus Tip: Copy Directory From one Directory to Another<\/strong><\/h2>\n<p>Copying a Complete Directory to Other Directories is also achievable with the \u201ccp\u201d command. The \u201c-r\u201d flag is used with the \u201ccp\u201d command to specify the command to move the directory:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">cp<\/span> <span class=\"re5\">-r<\/span> <span class=\"sy0\">&lt;<\/span>source-directory<span class=\"sy0\">&gt;<\/span> <span class=\"sy0\">&lt;<\/span>destination-directory<span class=\"sy0\">&gt;<\/span><\/div><\/div>\n<p>In our case, if we want to copy the \u201cFolderA\u201d directory to the \u201cdirB\u201d directory, the \u201ccp\u201d command will be executed as follows:<\/p>\n<div class=\"codecolorer-container bash blackboard\" style=\"width:100%;\"><div class=\"bash codecolorer\"><span class=\"kw2\">cp<\/span> <span class=\"re5\">-r<\/span> .<span class=\"sy0\">\/<\/span>FolderA .<span class=\"sy0\">\/<\/span>dirB<\/div><\/div>\n<p>This will copy the directory and the cursor will move to the next line:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"108\" class=\"wp-image-21474\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-16.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-16.png 736w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-16-300x44.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/p>\n<p>To verify, you can open the directory in the GUI:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1219\" height=\"185\" class=\"wp-image-21475\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-17.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-17.png 1219w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-17-300x46.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-17-1024x155.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2023\/09\/word-image-21458-17-768x117.png 768w\" sizes=\"auto, (max-width: 1219px) 100vw, 1219px\" \/><\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Copying files from one directory to another is a crucial task for system administrators when copying\/transferring useful data or creating a backup of data. The \u201ccp\u201d command can be used to copy a single file, multiple files, or all global files with the same extension from one directory to another. This article explained the methodologies in detail for copying files to other Directories.<\/p>","protected":false},"excerpt":{"rendered":"<p>The \u201ccp\u201d command can be used to copy a single file, copy multiple files, or copy all global files with the same extension from one directory to another.<\/p>","protected":false},"author":110,"featured_media":21476,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-21458","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/21458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/110"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=21458"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/21458\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/21476"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=21458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=21458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=21458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}