Copying and deleting files/folders within a terminal client

Disclaimer. When using a sudo/root mode be extremely careful and really think about what you are doing. The sudo/root mode should never be used without considerations and it is certainly not the correct place to test out things. Also, even as I do make my best effort to produce high quality instructions, I do not take any responsibility if anything goes wrong or if there is some sort of error in the commands below.

Before I begin talking about the official topic of today, I will list the simple way of doing things. I will use the case relating to Audax 0.2. and Emelfm2 usage on the example below.

Ever since Audax 0.2. originally came out Emelfm2 was included to the distribution. If you find Emelfm2 too hard to grasp then you can also install some other (more traditional) file manager like pcmanfm (sudo apt-get install pcmanfm) and use it together with “Simple mount” coming with Audax 0.2. If you want to copy/remove something from a mounted usb device with pcmanfm then execute a command  gksudo pcmanfm within, for example, Fbrun dialog (hit F2 button). In any case, it is strongly recommended that you read the entire post presented here. Throughout the post I will be using an example username tester.

Important note. Audax 0.2.5 is just around the corner. Within the 2.5. version mounting will work out of the box and without trouble as Audax will ship with a bug-fixed and functional pcmanfm.

And now let’s get to the topic of today.

Copying and deleting files/folders within a terminal client

Warning. Remember that a terminal client does not usually prompt you with any “Do you want to continue?” questions. Never ever copy and paste anything to a terminal client when you are removing things. On some occasions the copy+paste has resulted to an immediate deletion of files and folders. This is very bad news if you have made some sort of an error. As a result you might have ended up cleaning your device completely without any possibilities to recover the data since sudo/root mode does not have a trash bin feature.

Let’s say we have mounted our usb stick to a folder called device located in our home directory. Now, we want to replace an old file in the device directory with a newer version. In order to do that we shall open a terminal client and delete the old copy of our example file screen.png and then add a newer copy of that same file back to the device. Entries with # are comments.

The rationale of the commands presented below is as follows:

copy/remove a file/folder named x from a certain location called y.

Scenario 1. Copying and removing single files.

Note. The -v means the same as verbose. We will get feedback about our commands with it. As such the -v option is quite useful to have on board.

cd device       #cd into the mount point.

sudo rm -v screen.png       #Remove the file screen.png from the usb device.

sudo cp -v /home/tester/screen.png .       #Copy a new screen.png back to the device. Notice that the  . (dot) means the current path you are in (due to the usage of the cd command above).

Scenario 2. Copying and removing folders

Now, let’s presume that you already have a folder called stuff within your device and you want to replace it as quickly and as easily as possible with a more updated copy.

cd device #cd into the mount point.

sudo cp -R -v /home/tester/stuff .

On the above, we copied the folder recursively. Copying folders recursively means that you will be copying them (and their sub-folders) from a specified location to an alternative location while preserving the exactly same directory structure. If we would have used the following command:

cp -v /home/tester/stuff .

nothing would have gotten copied to our device since the recursive option was not around.Now, if we would like to completely remove the folder named as stuff from our device it would go like this:

cd device      #cd into the mount point.

sudo rm -R -v stuff

You can also use the recursive option to copy files to devices. And generally speaking, if the sudo cp -v fails to copy it is more than likely that sudo cp -R -v command will get the job done and nothing will be left out. On my daily terminal client tasks I always use cp -R -v command because with recursive option I can usually be sure that everything gets done.

Tip.

If you want to make absolutely sure that you copy a file or a folder to a correct location then you can use full paths:

sudo cp -R -v /home/tester/screen.png /home/tester/device/

Similarly, if you want to be completely sure that you remove a right folder or a file you can do something like:

sudo rm -R -v /home/tester/device/usb