Command line file management

June 19, 2010 by Vincent

I have been a fan of the command line for a while now, about from the time I moved from Ubuntu to Arch. The most difficult thing to get used to was command line file management but I think I can do anything now comfotably from the command line and I think I am a lot faster doing it thanks to a couple of wonderful applications.

First of all there is coreutils, this is the thing every one knows and is used to. It has the regular things like cp, rm, mv and ls. It is however the basic of everything. Combined with shell globs this is a very powerful thing, and it can do any I wont go into the basics of those commands but I will share some examples that I found very useful and weren’t as obvious as others.

When you have a list of pictures, named pic001.png through pic732.png and you just want to remove pic051 through pic521, you can use shell ranges. They are defined like pic{<startno.>..<endno.>}, so in our example it will be like:

rm pic{051..521}.png

Also ls isn’t as useful as it could be by default. I think every distro has some basic options set for ls like --color=auto but there is a lot more. For example, I also like to have my directories and files seperate, and there is an option for that called --group-directories-first. I also like to see the size of the file as well as the permissions on the file, and ls -l is perfect for that, but the filesizes is in bytes by default, which isn’t really human readable. I mean, I don’t care my file is 45234786 bytes, I just want to know it’s 4MB. ls -lh is for that. I think by now we have a very useful ls, you can try it out by executing ls -lh --color=auto --group-directories-first in your favourite shell. Or, a lot more useful, you can make an alias.

Then we have cd which just does what it should, which is change to the directory specified. But as we all know, we mostly hang around a couple of directories, and we just want to switch to those easily and fast. Some archer named Joel Thelion made an application called autojump which he describes as The cd command that learns and that is exactly what it does. It keeps track of in what directory you are and how much you do in there and it calculates a weight of that. That weight is later used to determine where to move. I can explain it but it’s better to just try it, it is awesome!

I think that makes you able to do all the things you do in a normal file manager, but just with your fingers on your lovely clicky keyboard. We all know that you dislike the mouse, like you should according to a lot of linux elitists, although I have to be honest and I like it a lot for laid back browsing sessions. But to the point, and that is shell based file management. And the shell is so powerful because it’s scriptable, you can chain things together very easily. And the most amazing application for that is find.

For example, you have a directory filled with rars which you all want to unrar. Instead of unrarring them each on their own you can use find. We are trying to find the files ending in .rar in the directory “Archives”, so just to test what we find we can execute find Archives -name "*.rar" which should print out the path to those files. But we want to do something with those rars, not just know where they are, and find has a flag for that called -exec . -exec needs two things, the binary to execute, in our example it’s unrar e, and {} \;. The {} is replaced by the path it finds and \; mimics a return key being pressed.

I really hope I learned you something and if you have ANY comments/criticism please reply to this post!

No Comments »

No comments yet.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>