How to Manage Your Files From the Command Line


file manage from the command line

This is the third article in the ‘Break the GUI’ series, and it’s all about managing files and directories without having to open the file manager.

One great advantage of using the command line interface (CLI) to perform such tasks is that you bypass the limitations of the file manager that comes with your distro. CLI is faster, less resource intensive, and as a result, more efficient. Learning CLI has its own perks: it also enables you to run your own headless servers.

I assume that you are familiar with the basics of command line, if not, then check out this article by Jack Wallen, “How to Move Files Using Linux Commands or File Managers.” In this article, we will learn how to do ‘almost’ everything with your files and directory without a file manager.

Without further ado, let’s get started.

To make it easier for users to understand, I am presenting a problem. I take a lot of photos and download them to my computer. Now all of those photos are piled up in the ‘Downloads’ directory and I want to organize my files.

How to create new directories with command line

There are two ways to do this. Either _cd_ to the directory where you want to create a new folder or use the full path. Let’s assume I have pics from the previous LinuxCon and I want to save all those images to a directory called ‘Linux_Con’ inside another to-be-created directory ‘images’, inside one of my partitions mounted at ‘/media/4tb’.

Since the directory ‘images’ doesn’t exist yet, we will use the ‘-p’ option with the mkdir command:

mkdir  -p /media/4tb/images/linux_con

The ‘-p‘ option will create all the directories given in the command in the parent-child order. If we used the mkdir command without ‘-p‘ and since ‘images’ directory didn’t exit, it would throw an error.

The second method is cd to the parent directory, which in this case is ‘4tb’:

cd /media/4tb

Then create the directories:

mkdir -p images/linuxcon

The directory has been created and we can now populate it by either copying or moving files to it. It’s important to understand the difference between the two: while copy, as implied by the name, makes a copy of the file at the new location, the ‘move‘ command deletes the file from the original location and places it in the new one.

Copy files:

cp path_of_file /path_of_new_destination

If I am copying the image ‘linux_con_2012.jpg’ from ‘Downloads’ folder to the newly created folder here is the command:

cp /home/swapnil/Downloads/linux_con_2012.jpg /media/4tb/images/linuxcon/

What if there is more than one image in the ‘Downloads‘ folder? In that case we can use a wildcard to copy everything from the folder to the images directory:

cp /home/swapnil/Downloads/* /media/4tb/images/linuxcon

Houston there is a problem

There were other files and not just images in the download folder and I wanted only images to be copied. In that case we will specify which file types to be copied. Use the file ‘extension’ with the wildcard so the command will copy only those files types. In this case the images are in .jpg format.

cp /home/swapnil/Downloads/*.jpg /media/4tb/images/linuxcon

You can use the same pattern for copying other file types. For example, if you want to copy images or documents or music you can use appropriate extensions such as .jpg, .odt,.doc, .pdf or .mp3.

Copy only certain files of the same file type

What if I don’t want to copy all images from the download folder; and want to copy only those that I took during the conference? Let’s say that the images were named by the camera as IMG with numbers, so they looked something like: IMG_2200123.jpg.

Now the first 325 images are from Linux Con. Let’s make it a bit complicated and say say from image number 50 until 400 are from LinuxCon and these are the images that I want to copy. Images before and after these are from some other event. Our challenge is to move only these images.

So we are going to move all the images from IMG_220040.jpg till IMG_220400.jpg. Here is the command:

cp /home/swapnil/Downloads/DCIM/IMG_220{40..400}.jpg /media/4tb/images/linuxcon/

In this case the name outside the bracket is common and those inside are changing. You can use the same pattern to copy images on your computer.

If I want to copy the next batch just use the same pattern. Let’s say I want to copy the next 100 images to the Chicago folder. The command would be:

cp /home/swapnil/Downloads/DCIM/IMG_220{401...500}.jpg /media/4tb/images/chicago

Move over cp, here comes mv

If you don’t want two copies of the same file, as it will take up unnecessary space, you can simply delete the copied files from the source location to free up space. Alternatively, instead of copying you can move files. That becomes important when you are running out of space and ‘copying’ won’t work because there isn’t enough space to have two copies of the same file.

You can use the above commands for the purpose, just exchange ‘cp’ with ‘mv’. Here mv stands for ‘move’.

So the command will be:

mv /home/swapnil/Downloads/DCIM/IMG_220{401...500}.jpg /media/4tb/images/chicago

How to rename files

It’s quite easy to rename files using a file manager like Dolphin. Although Gnome’s default file manager Nautilus (Files) can’t handle multiple file renaming so you are stuck due to that limitation. That’s where CLI comes into play as it can batch rename files on a Gnome system.

The problem.

Why do I need batch file renaming? I visited a conference and took hundreds of pictures. Now two years later, it will be an impossible task for me to find the images from Eben Upton’s keynote, as all images are in the default naming format such as “IMG-20133.jpg”. If I re-named the images from different events, it would be extremely easy to find them later. My son was born in 2012 and if I want to see the video or images of his first laughter I can simply search “first laughter” on my computer and the image will be there…because I named them appropriately. No more muss and fuss with trying to find the folder where I saved images that year.

It’s very easy to rename single images but if you have more than one image, then it’s a waste of time to rename hundreds of images one by one.

To rename images using the command line, we use the ‘mmv‘ command (there are other commands like renamer, etc).

If mmv is not installed on your PC, you can install it from the official repos. Once installed, cd into the directory where the files are saved. There is one caveat though: The command will rename all the files in the current directory so use it carefully.

mmv *.jpg new-name_#1.jpg

All files will be renamed to new-name.

If you want to rename one image, then simply run this command:

mv /path_of_Image/old-image-name.jpg /path_of_Image/new-image-name.jpg

Keep two directories synced

You must always have back-up or copies of your files on different physical storage devices so that the chances of losing data are low. I have a mail home server and then a local backup server, where drives are hot plugged and I can grab and run in case of fire (yes, I will save my data before I save my cats!). In addition to that I have a copy of my critical data on a remote server and I sync it using a tunnel. And that’s where ‘rsync’ tool plays a critical role.

So how do I keep my data synced across these devices or how do I automate the back-up? Well that’s the focus of the next article. The focus of today’s article is to keep two directories synced with each other.

There are many advantages of rsync over cp or mv commands. And in some cases you can only use rsync and not the other two.

One of the advantages of using rsync over cp is that if you delete any files from the primary source, rsync will delete those files from the secondary source. In a nutshell it keeps both both directories synced.

Let’s say we want to make a copy of /media/4tb/images/linuxcon on another drive mounted at /media/2tb/

rsync -avz /media/4tb/images/linuxcon /media/2tb/

As you can see, the pattern is simple:

rsync -avz /source /destination

Here a stands for ‘archive mode’. This mode preserves devices, symbolic links, attributes, permissions, ownerships, etc. V stands for verbose and z compresses the files during transfer. More options can be used for different tasks.

By default it won’t remove any directories from the destination if you deleted them at source. You can enable it to do so by adding the ‘–delete’ option. So the command will become:

rsync -avz --delete /media/4tb/images/linuxcon /media/2tb/

That’s pretty much the basics of using CLI to manage files and directories without touch the GUI based tools.

Keep yourself rsynced with this series!

How to Use the Linux Command Line: Software Management

How to Use the Linux Command Line: Basics of CLI