-
Notifications
You must be signed in to change notification settings - Fork 1
Introduction to the Command Line
An externally-hosted full guide can be found here.
The command-line interface, sometimes referred to as the CLI, is a tool into which you can type text commands to perform specific tasks as opposed to using the mouse to point and click on menus and buttons. Since you can directly control the computer by typing, many tasks can be performed more quickly, and some tasks can be automated with special commands that loop through and perform the same action on many files—saving you, potentially, loads of time in the process.
The application or user interface that accepts your typed responses and displays the data on the screen is called a shell, and there are many different varieties that you can choose from, but the most common these days is the Bash shell, which is the default on Linux and Mac systems in the Terminal application. By default, Windows systems include the Command Prompt application, which has nowhere near the power or wide use of Bash (PowerShell is a powerful command line interface but it does not use Bash). You can use the open source Cygwin tool or MobaXTerm as your Windows command line if needed. You can also install the Windows Subsystem for Linux and use a 'native' iteration.
To get started with the command line, you'll need to open up a terminal window and get ready to start typing commands. Here's a list of basic commands you can use, organized by the type of activity that you might want to perform.
When you run your terminal application (Cygwin/MobaXTerm on Windows, Terminal on Mac and Linux), your command prompt will start up pointing to a specific folder on your hard drive. You can navigate between folders, act on files inside those folders, or perform other actions.
First, let's display a list of files inside the active folder. For this task, you'll need to use the ls
command. You can pass a number of parameters to the command to display extra details or change the sorting. For instance, if I add -l
to the end of my ls command, I'll see a detailed listing; -t
will sort the results by file time; -S
will sort by file size; and -r
will reverse the sorting. You could use a combination of these together, like this command, which will show all files sorted by file size with the largest files at the bottom:
$ ls -lSr
If you use the –a
option, you can see hidden files, and you'll also notice something else in the listing: there are two entries for "." and ".." at the beginning of the list. These represent the current folder—the "." folder—and the parent folder—the ".." folder.
You can change between directories using the cd
command, and using what we just learned about the ".." folder, you can use the following command to switch to the directory directly above the current one:
$ cd ..
You can navigate to either full or relative paths. For example, the command above navigates to a relative path—one above the current folder. If you're in /path/to/ and you want to navigate to the folder stuff inside that folder, you can simply type:
$ cd stuff
You can also navigate to absolute paths. Again, if I were in /path/to/ and I want to navigate to /another/path/, I would simply type:
$ cd /another/path
To swap directories to the previous working directory, the '-' (hyphen) shortcut is handy to have on hand. For example, if you were in the /first/folder/path/ directory and then switched over to /etc/ to make a configuration change, you might not want to type in the full path to switch back. You can quickly switch back to the previous working directory with this command:
$ cd -
To create a new folder, you can simply use the mkdir <foldername>
command. You can then remove any folder with the rmdir <foldername>
command—as long as the folder is empty. If there are files in the folder, you'll have to delete those files before you can remove the folder.
You can use the touch <filename>
command to create a new, blank file, and then use the rm <filename>
command to delete files:
$ rm filename
You can quickly remove all files in a directory by using the '*' (asterisk) wildcard—another simple tool that will come in very handy during your time in the command line. For example, if you're in a folder and want to delete every file inside that folder, just type:
$ rm *
If you want to delete a list of files and folders, including all files from subdirectories, without prompting you for every single entry, you can use the -r option for recursive, and the -f option for force. This command will wipe out every instance of a matching filename pattern (note the slightly different use of the wildcard) from the current directory and below:
$ rm -rf filename.*
A typical command might look like the following. Type the following in your terminal and press Enter to view the current date.
date
Also try the following to see the monthly calendar
cal
For an illustration of fixing or replacing text in a command, we'll use the following command. Type this but don't press Enter. In fact, we won't use Enter anymore since we'll be operating on this single command.
namo ~/mycatisweird.txt
Now, the cursor will be at the end of the command. We notice that instead of nano
, we typed namo
. The normal method would be to use the left arrow key to go to the beginning of the command and replace m with n. However, a faster way is to use Esc-b (type Escape key and the b key in immediate succession). Alt-b also works (Alt-{left arrow} should work on a Mac). This key takes your cursor one word back. Repeat the key sequence few times to go to the start of the command. The navigation in opposite direction is Esc-f (Alt-f) which takes you one word forward (Alt-{right arrow} should work on a Mac). Shuttle between the beginning and the end a few times by taking word jumps.
However, a faster way is to simply use Ctrl-a which takes you to the beginning of the command directly. Use this to go to the beginning and change m to n. Its counterpart Ctrl-e takes you to the end of the command. Shuttle between the two boundaries of the command a few times.
Ctrl-l is a special sequence which is used to clear the screen. It'll clear the screen and place your prompt and the command in the first line of the cleared screen. Press Ctrl-l now and clear the screen. Note that the command is still intact.
Esc-b : one word back
Esc-f : one word forward
Ctrl-a : Go to the beginning of the command. (Can remember this because a is the first letter of alphabet)
Ctrl-e : Go to the end of the command.
Ctrl-l : Clear (C-l-ear)
Now, go to the end of the command using Ctrl-e. You should be at the end of mycatisweird.txt
. Suppose you want to edit a different file mycatisawesome.txt
. Press Esc-Backspace (or Alt-Backspace) to delete from the cursor to the start of the word. For this particular example you will need to press it twice. Type in mycatisawesome.txt
. Your command should look like this now:
nano ~/mycatisawesome.txt
Many times, you would want to delete characters till the end of the current line. Suppose you are between the words nano
and ~/mycatisawesome.txt
. ( Navigate there using the sequences.) You want to change the output to a different text file in a different directory. Ctrl-k does just that. It kills everything from cursor to end. Try and delete till end of command and type such that your new command looks like:
nano ~/Documents/whythisisaboutcats.txt
Think of the key Esc (or alt) as a substitute for word. We have seen that Esc-b and Esc-f take you back and forward by one word respectively.
Esc-Backspace : Delete a word back
Esc-d : Delete a word forward ( Normal delete )
Ctrl-k : kill till the end of command.
This tutorial touches upon only a tiny subset of command line navigation key sequences. These are pretty common sequences, however. Also, introducing too many sequences can be overwhelming and counter-productive.
The command line interfaces use a library called Readline. Is has extensive documentation.
The Esc key is called Meta key in Readline parlance. If Esc doesn't work, Alt should (and might be preferable).
Instead of learning more sequences, it's better to master these basic sequences (practice!) and then build upon once you are comfortable with these.
For reference, a set of commands are below.
Bash Keyboard Shortcuts
Moving the cursor:
Ctrl + a Go to the beginning of the line (Home)
Ctrl + e Go to the End of the line (End)
Ctrl + p Previous command (Up arrow)
Ctrl + n Next command (Down arrow)
Alt + b Back (left) one word
Alt + f Forward (right) one word
Ctrl + f Forward one character
Ctrl + b Backward one character
Ctrl + xx Toggle between the start of line and current cursor position
Editing:
Ctrl + L Clear the Screen, similar to the clear command
Ctrl + u Cut/delete the line before the cursor position.
Alt + Del Delete the Word before the cursor.
Alt + d Delete the Word after the cursor.
Ctrl + d Delete character under the cursor
Ctrl + h Delete character before the cursor (backspace)
Ctrl + w Cut the Word before the cursor to the clipboard.
Ctrl + k Cut the Line after the cursor to the clipboard.
Alt + t Swap current word with previous
Ctrl + t Swap the last two characters before the cursor (typo).
Esc + t Swap the last two words before the cursor.
Ctrl + y Paste the last thing to be cut (yank)
Alt + u UPPER capitalize every character from the cursor to the end of the current word.
Alt + l Lower the case of every character from the cursor to the end of the current word.
Alt + c Capitalize the character under the cursor and move to the end of the word.
Alt + r Cancel the changes and put back the line as it was in the history (revert).
Ctrl + _ Undo
TAB Tab completion for file/directory names
For example, to move to a directory 'sample1'; Type cd sam ; then press TAB and ENTER.
type just enough characters to uniquely identify the directory you wish to open.
History:
Ctrl + r Recall the last command including the specified character(s)
searches the command history as you type.
Equivalent to : vim ~/.bash_history.
Ctrl + p Previous command in history (i.e. walk back through the command history)
Ctrl + n Next command in history (i.e. walk forward through the command history)
Alt + . Use the last word of the previous command
Ctrl + s Go back to the next most recent command.
(beware to not execute it from a terminal because this will also launch its XOFF).
Ctrl + o Execute the command found via Ctrl+r or Ctrl+s
Ctrl + g Escape from history searching mode
Process control:
Ctrl + C Interrupt/Kill whatever you are running (SIGINT)
Ctrl + l Clear the screen
Ctrl + s Stop output to the screen (for long running verbose commands)
Ctrl + q Allow output to the screen (if previously stopped using command above)
Ctrl + D Send an EOF marker, unless disabled by an option, this will close the current shell (EXIT)
Ctrl + Z Send the signal SIGTSTP to the current task, which suspends it.
To return to it later enter fg 'process name' (foreground).
This was copied from this post, which also shows how to enable the option/alt key on a Mac keyboard as a meta key.
The following commands will be extremely useful to know when trying to run a whole set of participants at once. These commands go beyond the basic command lines.
If you have an application or shell script in the current folder, you can't simply type the name of the command and expect it to start. You'll need to add a ./ to the beginning of the command in order to start it. Why? Because in the Bash shell, the current directory, or "." folder, is not included in the system path. So to launch scriptname.sh in the current folder, you'll need to use:
$ ./scriptname.sh
You can use the very powerful find
command to search for files on your system. For instance, if you wanted to find all files with .txt in the name that were modified in the last 5 days, you would use this command:
$ find . -name "*.txt" -mtime 5
The grep
command can be used to quickly find text within files, even searching through subdirectories. For instance, if you wanted to search through all files in the current directory and below it for "text string", you could use this command:
$ grep -ir "text string" *
A loop is a block of code that proliferates a process over a series of values until certain conditions are met. This can be very useful in quickly performing a common process on a series of files all at once.
A basic format for a loop command is:
$ for <argument> in <value>
> do
> <process executed>
> done
To help you understand loops better we'll go through a couple examples of how they can be used.
Uncompressing a series of .tar.gz files in your "Downloads" folder
$ FILES=/home/username/Downloads/*.tar.gz
$ for file in $FILES
> do
> tar xvzf $file
> done
In the statement "FILES=/home/username/Downloads/.tar.gz" the variable "FILES" is assigned the list of files in directory (folder) "/home/username/Downloads/", whose name ends with ".tar.gz". The "" (wild card character) stands for any sequence of characters.
In the for-statement "file" is used as the name of the loop variable, which is assigned each of the file names in the list assigned to "FILES". The body of the for-loop (the statements between the "do" and the "done") is executed once for each file name.
If you want to loop through a set of filenames and perform an action on each one, you can use the for
command to loop through a set of files. For instance, to loop through all the .txt files in the current directory and display them on the console, you could use:
$ for f in *.txt
> do
> echo $f
> done
You can also combine the find command and the loop action to find specific files and then perform an action:
$ for i in $(find . -name "*.txt" -mtime 5)
> do
> echo $i
> done
You can use the history
command to show a list of all the recently used commands, or the up/down arrows to loop through them. The Ctrl+R shortcut key will start a search mode where you can type the first few characters of a command to search through your recent history.
Read this page https://help.ubuntu.com/community/FilePermissions
The basic message is that in Ubuntu you need to do
$ sudo chmod ugo+rwx <mango>
To get files to be accessible to everyone.
In other Linux environments you don't need the sudo
command.
When we talk about environment variables in coding we refer to a unique set of functions and code that effect how programs are run beyond what is simply part of the typical operating system.
An example of one you will most likely run into here is:
$ APPHOME=/usr/local/bin
$ export APPHOME
If you type this into the command line these values will be active for the remainder of that session (the time that specific command line is open). However, if you want to add an environment variable permanently you need to create a file that contains it. It may be helpful to use one document to store all of your environment variables for you MRI projects. In order to do this you can use the nano
command.
When you use this command you'll want to type the name of the command following it. We'll add this to the command line profile to make it available every time you use Bash.
$ nano .~/.bash_profile
You're now in the Nano editor, where you edit text files. If the file is empty, just type the APPHOME commands above. When you're done press Control+o to "write out [save]". Press enter to confirm the file name (or you can use this to create a new file name [Save As]). Then press Control+x to exit. If you want more details on how to use Nano you can go here.
You can also create a file with the chunk of coding you want and apply ".sh" to the end. If you make such a file in the bin directory you only need to type source nameoffile.sh
to activate those environment variables in a sessions. This can be far easier than having to retype environment variables in for every session. You also typically need to make the file executable: sudo chmod +x nameoffile.sh