A command-line interface, also known as command-line user interface or console user interface, is a means of interacting with a computer program where the user (or client) issues commands to the program in the form of successive lines of text (command lines). The command line interface is usually implemented with a command line shell, which is a program that accepts commands as text input and converts commands to appropriate operating system functions.
The command line can be rather intimidating to some developers, particularly those coming from a design background who may be used to GUIs, but fear not, the command line is your friend. If you want to try learning the command line or you are new to it, we have provided some basic commands to help you get started.
An Introduction to the Command Line
Isn’t it dangerous in there?
It is true that the command line is very powerful and we all know that great power bears great responsibility. The key is to start small, gain an understanding of how the command line works and progress at your own pace.
Surely my GUI will do the job?
While there are many great apps out there that can help you with your Git workflow or compile your Sass for you, more and more development tools are command line technologies, so skills in this area will benefit you greatly.
OK, where do I start?
I will say now that the command line differs on various operating systems and my experience is from using a Mac, so I will be focusing on that, however the commands in this article should work on the Windows command line also.
I like to think of the command line as a window into the workings of my computer. The primary reason I use it is to navigate the files in my projects. Through my terminal I can create directories and new files, move or copy them and open them in either my text editor or browser.
Finding your way around
The first thing to understand is how to navigate between directories. By default, when you open your terminal, you will be in your home directory which would typically be your name.
To change directory you need to use the cd
command followed by the path to the directory you wish to navigate to. For example, lets say we want to navigate to your Documents
directory:
co cd Documents
Tip: You don’t have to type the entire path yourself. Just type the first couple of letters and hit tab until the directory you want is displayed.
Now we’re inside our Documents directory, but we’re not sure what’s in there. Well, to list the files and/or folders in Documents
simply use the ls
command:
ls -l
This will show you all the files and folders in your Documents
directory.
As well as navigating into your various folders, you can also navigate backwards, or go straight to your home directory.
To go back one level you would simply type into your terminal:
cd ..
The two periods represent the parent directory. To go even further you would type:
cd ../..
But what if you want to go straight back to the home directory? Simple, just type:
cd or cd ~
The tilda (~) always represents your home directory, so if you were already in your Documents
folder and you wanted to go to your Downloads
folder, you would type:
cd ~/Downloads
Opening files
So now you know how to find your way around, you will want to actually do something with these files. Lets start with opening them.
By typing open
, followed by the path of the file you wish to open, it will open in its default application. For example the following command will open the file in a web browser:
open Documents/website/index.html
You can even unzip files this same way like so:
open Downloads/mydownload.zip
Creating files and folders
What if you want to create a file or a folder. Simple. To create a new directory, navigate to where you want it to be, and then use the mkdir
command followed by whatever you want to call your directory:
mkdir myDirectory
To create a file, just go to the directory that you want to create the file in and use the touch
command followed by the filename and extension:
touch myDirectory/newfile.txt
Moving, renaming and copying files
To move a file, you use the mv
command, followed by the path to the file, followed by the new path to the file:
mv ~/Downloads/myDownload.zip ~/Documents/myDownload.zip
You can also rename a file using the mv
command if you use it within the same directory like so:
mv myFile.txt yourFile.txt
Copying files works in the same way except you use the cp
command:
cp ~/Downloads/myFolder ~/Documents/myFolder
Deleting files
Here is the part where you really need to be careful. When you delete files with the command line, they cannot be recovered. They do not go to your recycle bin, so only do this when you are 100% sure that you want to delete something.
To delete a file you use the rm
command followed by the path to the file:
rm Downloads/unwantedFile.txt
To delete a directory you use the rmdir
command, again, followed by the path the directory:
rmdir Downloads/oldDirectory
As I said before, only use these commands if you are sure you do not need to recover these files. There is no recycle bin on the command line.
Searching
Searching via the terminal can quickly become very complicated, especially when you start to look into grep
or ack
, but there are some simpler ways to do basic searching.
Most likely, you would want to find a file that you know the name of, but are unsure of where it is located. To search for this you can use the find
command with the name parameter:
find ~/ -name myFile.txt
If the file is located, you will see the path show up in the terminal. You can then use the commands from earlier in this article to navigate to that file and open it in its default application. But what if you know the file name but can’t remember the file extension?
find ~/ -name myFile*
By replacing the file extension (.txt
in the previous example) with the wildcard selector (*), you will see a list of all files named myFile
.
The other scenario could be that you know roughly where the file might be, and what it’s extension is, but not what its called. To search for all files of a particular type within a directory you use the following command:
find ~/Documents/ -name *".txt"
This will list all the files with an .txt
extension.
Other useful commands
You can also use the command line to find out information about your system. For example, if you wanted to know your IP address, you would type the following command:
ifconfig
Another thing you can do is see how long your machine has been active by typing:
uptime
Another useful command is to prevent your machine from going to sleep, perhaps whilst you’re installing an application or downloading a large file:
pmset noidle
When you have finished and want to return the machine to its normal state, just hit CTRL + C.
Best practices
Commands will only work if they are typed correctly. If you mistype, the chances are you will simply see a message in your terminal telling you that the command is not recognised.
To make commands easier to type and remember, you can set up what are known as aliases. You can set these in your bash_profile
. On a Mac, you can use the following command to open your bash_profile
in TextEdit:
open -a TextEdit .bash_profile
You can then add your aliases at the end of the file:
alias myAlias="cd ~/Documents"
So you define an alias with the word alias
, then add the alias name, which can be anything you want it to be, followed by the action you want your alias to take.
You can also take a look at this command line cheat sheet.
Image credit: osxdaily
More Resources
Welcome to the command line…
So now you can find your way around, open, create, move, rename, copy and delete files, all from the command line. Once you’ve become used to this way of working, you’ll never want to go back.