Contents
The command line interface might, at first, seem primitive. It's actually one of the most powerful tools in your toolbox. In fact, one would rarely look over the shoulder of an experienced developer and not see a couple of open terminal sessions running on their desktop.
When you fire up the command-line environment on either bare-metal Linux, Ubuntu on Windows, a Mac, or in some cloud instance, a GUI terminal window pops up and a special interpreter called a shell (by default, Bash shell) is automatically started. Then you can issue commands into the terminal to create, move, or rename files or directories, peruse your file-system, run programs, compile programs, (re)move/rename files, etc.
Essential Bash commands
Description | Command | Example usage |
---|---|---|
create a subdirectory of the current directory | mkdir <dirname> | $ mkdir project1 |
list contents (of the current directory) | ls | $ ls |
list contents (of subdirectory of the current directory) | $ ls project1/ | |
list contents with details | $ ls -l | |
list contents including dot files | $ ls -a | |
change (to a sub)directory of the current directory | cd <dirname> | $ cd project1/ |
change to your home directory | $ cd ~ or just cd | |
change directory (from any other directory) | $ cd ~/project | |
move up one directory in the directory tree | $ cd .. | |
change to the previous directory | $ cd - | |
print working directory (your current directory) | pwd | $ pwd |
edit a file in the current directory using vim | vim <file> | $ vim filename |
create a new file and start editing it using vim | $ vim filename | |
edit a file in the current directory using nano | nano <file> | $ nano filename |
create a new file and start editing it using nano | $ nano filename | |
move (rename) a file or directory | mv <source> <target> | $ mv foo bar |
copy a file | cp <source> <target> | $ cp foo bar |
copy a directory and, recursively, all its subdirectories | $ cp -R foo/ bar/ | |
remove a file | rm <file> | $ rm foo |
remove an empty directory | rmdir <directory> | $ rmdir project1/ |
remove a nonempty directory (be careful with this one) | rm -rf <directory> | $ rm -rf tmp/ |
Note: all of the commands above are already available in any Bash shell implementation.
More commands
Description | Command | Example usage |
---|---|---|
start the Python3 interpreter | python3 | $ python3 |
run a Python3 program in the current directory | python3 <file> | $ python3 program.py |
page through (i.e., view) a file (type q to quit) | less <file> | $ less foo |
A nice way to view a file a file (type :q or ZZ to quit) | view <file> | $ view foo |
spew out a file (use less, instead, for large files) | cat <file> | $ cat foo |
view the man(ual) page for a command (hit q to quit) | man <command> | $ man ls |
Notes:
- All of the commands above are very likely already available by default except possibly the last one, man, the
database of which may need to be installed. Issue the command
sudo apt install man
to install the man pages. - The view command is really just vim -R; that is, vim with read-only switched on. To install vim, if it isn't
already installed, type
sudo apt install vim
.
Other hints/tips/tricks
- use the TAB key to autocomplete names of files and directories on the command line after typing the first few letters of the desired file or directory.
- use the up arrow to retrieve the previous command from your command history.
- For a short, basic introduction to the command line see linfo's page How to use the Command Line -- A First Lesson.
- Some slightly more technical references for Linux (in no particular order):
- Learning the Shell
- A beginner's guide to the Linux command line
- This shell and scripting page is pretty good, from MIT's Hacker Tools course.
- Whenever Simmons creates a new Linux install, he adds at least these lines to the end of the .bashrc
file in his home directory and then issues the command
source ~/.bashrc
:alias rm='rm -i'alias mv='mv -i'alias cp='cp -i'set -o vi- The first three commands add some safety features; namely, the requirement that you confirm when removing a file and when moving or copying a file that results in overwriting another file.
- The last command causes the command line in bash shell to behave like vim; so, for example, you can move through your history by hitting ESC and then j and k instead of the arrow keys.
- Whenever you modify .bashrc, for those changes to take effect you must either exit your bash
session and start it anew or enter
source ~/.bashrc
.
- Once you become handy with the basics of the command line, you can learn all sorts of wildly powerful techniques including: composing operations using pipes and redirects, working in multiplexers such as tmux or screen, backgrounding/foregrounding processes, etc.
Editing files
- A great solution for editing and running your Python code is to use the Python IDE called IDLE. To install
IDLE on Ubuntu, run the command
sudo apt install idle3
. You start IDLE by enteringidle
on the command line -- or better yet, enteridle &
on the command line. After you start IDLE, create a new file and write or paste some code into the window that pops up. Then clickrun
on the IDLE menu. Your workflow is just edit your code in IDLE and then clickrun
. - Feel free to use any text editor you wish to create and modify your Python code. But realize that we typically run our
programs from the command line. So if you use a GUI editor, point it to the correct directory. That way your workflow
looks something like:
- Open the program on which you are working in the editor you are using.
- Work on the program.
- Save the program.
- Switch to the bash shell window.
- Run the program.
- Re-edit the program and repeat, as necessary.
- To edit files, Simmons typically uses vim from the command line.
- To start an editing session type
vim <filename>
in the directory in which the file resides, orvim program1.py
, say, if you want to create a new file calledprogram1.py
and start editing it. - You can type vimtutor at the linux commandline for a tutorial on using vim.
- Here are some more vim tutorials:
- To start an editing session type
- Nano is a rudimentary but intuitive text based editor. On Ubuntu, if it's not already installed, type
sudo apt install nano
. - The basic gui editor gedit gets the job done. On Ubuntu, install with
sudo apt install gedit
and make sure you have the X server running.gedit
is also available for Macs: trybrew install gedit
. - micro is a modern and intuitive terminal-based text editor.
- On a Mac, the simplest way to create, edit, and run python is to use Idle, the default IDE that ships with Python3.
- There are all sorts of fancy gui editors, like Atom and Sublime Text, not to mention IDEs such as Visual Studio.
- Lastly, Geany is an editor that is also a lightweight IDE.
Moving files between WSL Linux and Windows 10
- If you are running the Ubuntu WSL under Windows 10, then the partition in which your Windows 10 filesystem resides is mounted in Ubuntu under the /mnt directory.
- Suppose that you, for example, download a file to your Windows Downloads directory.
You might want to move that file to your home directory in your Ubuntu. One way to do that is
- type
cd /mnt/c
, to get the top directory in your Windows file system - then
cd
down through the Windows directory tree to your Windows Downloads directory - type
cp <filename> ~
- type
cd
to return to your Ubuntu home directory -- the file should now be there.
- type
- Another way to do this is to use an ncurses based file manager like the popular Midnight Commander
to move files around. Type
sudo apt install mc
to install Midnight Commander and thenmc
to start it.
Using Git
You don't need to deep dive into git right now unless you want to version control your own code, in which case the following references might be helpful.
- A standard, fairly comprehensive reference
- Git magic
- Git interactive tutorial on branching