Learning how to use the command-line for copying files and folders (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 your desktop 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 for copying a file or a folder, 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 source-file‘s-path destination’s-path
To do the same to a folder, I’ll use the ‘-a’ argument as shown below.
cp -via source-folder’s-path destination’s-path
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).
What is that ‘-v’ argument?
The ‘-v’ argument gives you a simple output (basically tells you what file it’s currently copying).
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.
What is the ‘-i’ argument’?
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.
If your file or the folder has spaces in its name …
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.
By 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 Music
By 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.
In 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.
So 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 /home/gayan/Music/audio-recording1.mp3 /media/GAYAN
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 Music/audio-recording1.mp3 /media/GAYAN
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 Music /media/GAYAN
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 Music /media/GAYAN/music-backup
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 manuals.