How to Use the Command-line to Copy files and folders in Ubuntu Linux?Learning how to

(or backing up etc) in GNU/Linux sometimes can come in real handy. Because the GNU/Linux operating system is fundamentally designed and run through the command-line interface, thus even if

fails to load, there’s a good chance that you should still be able to use the command-line to login to your user account and backup your data (supposing that you don’t have an Ubuntu Live CD/USB flash drive around :)).So if your desktop fails to load at the login screen and doesn’t takes you to a ‘login shell’ (a ‘Terminal’ in full-screen, without a desktop), then you can open one manually by pressing the ‘Alt’ + ‘Ctrl’ + ‘F1’ keys on your keyboard and it should open a new login shell for you.Then you can use your existing user name and password to login to a non graphical interface and begin copying files.You can also use these commands while the desktop is working, by using ‘Terminal’ just as you’d use it to install software using the ‘apt-get install …’ as well. For this example I’m using Ubuntu 12.04 Precise Pangolin and running the ‘Terminal’ (‘Terminal emulator’) inside the ‘Unity’ desktop rather than the ‘login-shell’.How to do it …Basically , we have to know three main things. First you have to know its name and the ‘path’ (location) and also the path of the destination (where you want it to be copied). After you’ve figured that out, the rest is just bloody easy :).For copying files and folders we’ll use a built in command-line utility called ‘cp’ and for figuring out the names and the path we’ll use another one called ‘dir’.To copy a file into another location I’d use a built in command called ‘cp’ in the below format.cp -vi — To do the same to a folder, I’ll use the ‘-a’ argument as shown below.cp -via

You can also use ‘-r’ instead of ‘-a’ but that will replace some of the data of the files (not their content but things like ‘created date’ and ‘accessed date’ etc thus I prefer to use ‘-a’ so the data will be copied as it is. It’s also the way file managers of the desktops seem to be copying files and folders, I think).The ‘-v’ argument gives you a simple output (basically tells you what file it’s currently copying).Running it with ‘Mr. -v’ 😉 …This is very useful because by default, while copying one or multiple files, ‘cp’ doesn’t give you any output (unless there’s an error). So other than the HDD’s LED indicator, you’d wouldn’t know what going on.As you can see, without the ‘-v’, until it finishes, you wouldn’t know anything about the process …If some of your files in the destination have the same names as the files that are being copied, then ‘cp’ automatically overwrite them!. But when you use the ‘-i’ argument, it’ll ask you before overwriting. So I highly recommend that you use it, all the time.Let’s say I have a file called ‘test audio.mp3’ (note the space between ‘test’ and ‘audio.mp3’), then in the command-line, this file will be listed as  ‘test\ audio.mp3’ not ‘test audio.mp3’.This is because, before each space in a file’s name, the command-line assigns a ‘\’ to it. If you have another file called ‘Ubuntu 12.04 review.avi’, then in the command-line it’ll be listed as ‘Ubuntu\ 12.04\ review.avi’. So by remembering this rule, if you know the file’s name, than you can easily figure its name in CLI.Even if you don’t know the name, you can use the ‘dir’ command to list and find folder/file name with ease. Let’s say that I wanted get a list of all the files in my ‘Music’ folder, then I’ll use the below command.dir MusicBy default, ‘dir’ does not show hidden files. For that, you’ll have to use it with the ‘-a’ attribute. So to view all the files inside the ‘Music’ folder, I’ll use the below command.dir -a MusicBy simply using ‘dir’ will show you the folders and files inside your current location. So if I used it after logging into the shell (or in ‘Terminal’) then it’ll just show me the content inside my ‘Home’ folder as shown below.Also remember that, whenever you insert a removable media (USB drive for example), or while accessing other partitions of your HDD or opening optical disc’s content etc, they’ll all first be ‘mounted’ (think of it as temporarily opening the content of a storage device into a folder where your OS is installed) in a location called ‘/media’.So, after inserting your USB disk (or any other), because you’re gonna be needing its ‘mounted’ (opened) folder’s path while coping files to it later, then you can simply enter the below command in your Terminal and it’ll list all the currently mounted storage devices (=paths) in your OS.dir /mediaIn this case, my USB drive is called ‘GAYAN’ and it’s opened on a folder called ‘GAYAN’ under the ‘/media’ location.Let’s say that I need to copy a recorded audio file inside the ‘Music’ folder and don’t remember the full file name, then I’ll use the above mentioned ‘dir’ command to get its full name.dir MusicSo the full path of the source file that I want to copy in this case is:As I need to copy this audio file to my USB drive mounted at ‘/media/GAYAN’, the full path of the destination is:So in order to copy the ‘audio-recording1.mp3’ into ‘GAYAN’ USB drive, I’ll use the below command.cp -vi

For any file or a folder that’s inside my ‘Home’, I can bypass typing ‘/home/gayan’ part (in your computer, ‘gayan’ will be replaced by your user name), so the short version of the above command is:cp -vi

If I wanted to copy all the content of the folder ‘Music’, as it’s a folder, I’ll use the ‘cp’ command with the ‘-a’ argument:cp -via

Make sure to use ‘-v’ argument and ‘-a’ when copying folders, and replace source file name (and path) and destination path with your own ones. If you want to automatically create a folder inside the destination and copy the file/folder inside it, then add a folder name to the end of the destination’s path.So if I wanted to copy the ‘Music’ folder to a new folder called ‘music-backup’, then I’ll use the below command instead of the above one.cp -via

Well, that’s pretty much it. Once you get familiar with finding the name and the path of the file/folder that you want to copy, there isn’t a whole lot to it. If you want to know more about these two commands, then use the below commands to read their cpman dirGood luck.Related PostsAn RHCE, ‘Linux’ user with 14+ years of experience. Extreme lover of Linux and FOSS. He is passionate to test every Linux distribution & compare with the previous release to write in-depth articles to help the FOSS community.Thank you for this information, it is very helpful.However I am having an issue with my desktop–it does not load, so I must use Ctrl+Alt+f1 to log in (as in your example), however when I try dir /media the thumb drive I have (waiting to copy files) does not show up–NOTHING shows up (I also have a disk in the drive to write to–if that is even possible.) Do I have to mount them to use them? if so, how do I do that? Thanks!Apparently I’m missing something (I’m sorry to be so dense). I attempted multiple times to copy files to /media/pen with no success, so I made a live USB to try that. I have my 150GB partition mounted and attempted to drag and drop files. I keep getting a message that “the folder ____ cannot be handled because you do not have permissions to read it” (I have tried a file on the desktop as well as my .thunderbird folder in my home folder). I was able to drag and drop a picture from the desktop to the pen drive…but no file folder or open office file can be moved. I guess more instruction is needed if you don’t mind walking me through more of this…? Thank you.BTW, when I right click on the 150GB drive and go to permissions, it says Owner is root with folder access to create and delete files (which is grayed out and cannot be changed). Group is root with folder access to access files (also grayed out). Others has nothing listed and folder access is to access files (also grayed out) and at the bottom of the box it says “you are not the owner, so you cannot change these permissions”Ok, I tried using sudo nautilus and still got errors when trying to copy so I looked it up and tried sudo nautilus &> /dev/nullSOMEHOW I got it to work–SOME of the time. However, I still can’t copy a couple folders from the desktop to the thumb drive. I was able to get to one of the (hidden) folders I wanted to copy for my son and actually could change the permissions, but they did not stick even though I clicked the button at the bottom to have permissions carry to all folders/files(?)I want to thank you for your help so far as it’s the farthest I’ve gotten in weeks!THANK YOU!! It’s working brilliantly!! 🙂I am new to Ubuntu and upgrading caused some trouble due to which the desktop was just showing a black screen. Thanks to you and your post I was able to log into the Virtual Terminal and backup all my important documents.Once again, many thanks.This site uses Akismet to reduce spam. .Recent PostsContactAbout HecticGeekWelcome to HecticGeek, it is an independent blog founded back in 2010. We cover in-depth Linux OS, product review with other all technology-related software and tools. Our mission is to provide valuable and trustful technology-related content to our users.

Leave a Reply

Your email address will not be published.