Cool little Command Line Trick
Ok so most of you might be a little beyond small little tricks like the one I am going to talk about here but, I found this incredibly useful and felt the need to share it.
Ok so first off I need to site the source here this command is not my work it is the brain child of the folks at Lifehacker.com specifically written by one lowell[AT]lifehacker[DOT]com
That being said on to the meat of this blog post.
OK so when browsing the net looking for guides on how to navigate around the Linux CLI and over all tips and tricks in this area, I was listening to a podcast and this site was mentioned, so I checked it out.
The scenario: Have you ever wondered what you can do to make creating a directory and then entering the directory easier and less time consuming?
For most beginner to intermediate users of Linux CLI tricks and macros are not something that comes easy, however with this little bit of code you can easily make a directory and instantly enter that directory. This alone is cool but this command does one more thing I love coming from a Windows background, it allows you to use folder names with spaces without the need to encapsulate them inside (‘’)!
Now this may sound like a trivial bit of code but to someone like myself this was a huge time saver, this may not make a difference to the hardcore Linux CLI power user.
So lets look at some sample code
mkdir ~/test
cd ~/test
That is a example of doing a simple folder creation in your home folder and then entering the folder the old way.
mkdir ~/’foldername with some space’
cd ~/‘foldername with some space’
The above would be an example of how to make a directory with spaces in the name and then enter that directory. As you can see this requires a little bit of time even if you use the ~ to represent your home folder as I have done above.
The article Create and Change to a New Directory in One Command says that if you edit your ~/.bashrc file and add the following code
# mkdir, cd into it
mkcd () {
mkdir -p "$*"
cd "$*"
}
You can then simple combine the mkdir and cd commands using the following command
mkcd New Folder/New Subfolder
or
mkcd New Folder
The first line above will make one folder and a subsequent sub-folder under it and then cd into the furthest most folder.
The second option would be the equivalent to making a folder called “New Folder” and then entering it.
While the above commands are not entirely necessary it helps to stream line the process of creating a large number of folders and then entering the last folder made. Some useful things i could see this being used for is say creating a set of folders for a backup archive where instead of having to manually create a folder for each hierarchal level you can simple do it all in one swoop and be in the last directory.
This as mentioned is more a time saver for new users and is extremely easy to setup. It could prove useful to some advanced members as well and is useable on any Linux distro that honors the .bashrc file within a users home folder. I suppose if you knew what your doing you could place this code into a global configuration file to allow every user of the system to utilize this shortcut but seeing as that is beyond my skills I will leave that to the comments should someone want to add how to do it.
As always comments are welcome, any other quick commands similar to this you wish to share feel free to drop them in a comment, I am always willing to save time and it helps users like myself get more comfortable with how to manipulate the .bashrc file to help customized our Linux.