Here are some command line commands everyone should know (mostly Unix-centric):
cd – change directory. It means to go from one folder to another. For example, cd /some/path/whatever
~/ – your home directory, wherever it may be. ~/ will always link to your home directory, even if there are many different ways you can have your home directory set up. A common thing to do is cd ~/ to go to your home directory.
mkdir – make directory. Example: mkdir my_folder
touch – create a blank file with a certain name. Example: touch example.py
rm – remove a file or directory (delete, basically).
rmdir – remove directory. This works with empty directories. If you want to delete a folder that has files in it, use rm -rf some_folder/ but be very careful with this command, as you could accidentally delete something you didn’t mean to get rid of.
If you want to delete absolutely everything on your computer, including your personal files and even the operating system, use this command:
sudo rm -rf –no-preserve-root /
But be warned: the above command is very destructive. It’s often used as a joke or to trick people who are new to Linux. There’s really no point in doing it unless you want to wreck your files and OS.
cp – copy. It copies a file to a new location and also preserves the existing one.
mv – move a file from one location to another. It’s like copy, except that the original no longer exists.
ls – list files in a directory. To view all files, including hidden ones, use ls -a. If you want human-readable output, you can use ls -ah.
top – it’s like the Windows task manager or macOS Activity Monitor, but text-only. Press q or ctrl+c to quit.
ifconfig or ipconfig or ip – depending on your OS, you might use one or the other. It shows information about your network interfaces and how they’re set up. What your IP and MAC addresses are, default gateway, and things like that. I started using Linux a very long time ago, so I’m used to ifconfig. However, in more modern versions of Linux, the ip command has largely replaced ifconfig. They’re basically the same thing though.
You can turn network interfaces on or off, such as in the following example:
ip link set dev eth0 up
ip link set dev eth0 down
up means enable it, while down means turn it off.
ping – test a connection between your computer and another device. On Windows, ping will default to 4 pings. But if you want to only limit to a count of 4 pings on a Unix-like OS, use this:
ping -c 4 www.google.com
If you just did ping www.google.com on Linux, without -c 4, it will just ping indefinitely, or until you hit ctrl+c or ctrl+x to quit. I’d rather not ping something indefinitely. You don’t have to use 4. You could do ping -c 100 www.google.com or whatever you want.
Some servers won’t respond to ping requests, even if they’re online. Sometimes you can use ping to try and see what kinds of connection issues you’re having. If you can ping 18.104.22.168 but not www.google.com, then you most likely have a DNS server issue. If you can ping 192.168.1.1 (or whatever your router’s LAN IP address is), but not 22.214.171.124, then there is an issue with your internet connection.
uname -a – view all system information, such as your OS and its version number.
uptime – show how long the computer has been up. I’ve known people who bragged about having crazily high uptimes, like over a year straight for a server or computer. But these days, I’d say security and patching are much more important than continuous uptime. If you need to reboot for a patch, do it. Restarting in a production environment can be tricky though, especially if it’s something like restarting a hypervisor when people might be using the VMs on it. But uptime isn’t as cool as it used to be.
echo – kind of like print, echo displays text. By default, echo will include a line break afterward, but if you use echo -n, it won’t. For example: echo -n hello
You can also echo variables. Here’s a command I sometimes use: echo $SHELL
echo $? – show the error code of the last program that was run. Remember return 0 in the C++ chapter? You can actually write a shell script that can run C/C++ programs and then check if it ended in an error or not. If echo $? echoes out 0, it means the last command ran successfully. Any non-zero number indicates that there was a problem though.
echo * – kind of like ls.
clear – clears the screen, which is useful if you want to get rid of clutter to focus only on the output of a particular program and not the stuff you did previously, or if you need to take a screenshot but want to hide personal (or otherwise irrelevant) information from a previous command’s output.
history – a command that lets you view your previous commands. If, for whatever reason, you want to clear your command line history, you can use history -c or unset HISTFILE. The latter stops any commands from being logged. I recommend history -c. One reason you might want to clear your command line history is if you accidentally entered an SSH password as a command instead of when you’re being prompted to log in for an SSH session.
The history command doesn’t save an indefinite number of commands. The default is 500 commands. If you want it to be a different amount, you can use export HISTSIZE=5000. However, it might not persist when you end your session. So a better way to change the HISTSIZE environment variable is to change your .bashrc or .bash_profile file. Add a line to it where you type HISTSIZE=5000.
If you ever want to search through your command history, use something like this:
history | grep searchterm
vi or vim – simple command line terminal-based text editors. vi is the original and vim mean vi improved. They are pretty old and not beginner-friendly to people who are used to GUI apps, but it’s not that hard to use. Here are some essential tips you should know to start with vim. To open a file in vim, type vim whatever.txt in a terminal window to open it (assuming you even have it installed).
When it’s open, hit “i” to enter insert mode, and then you can type to add text to it. To quit, type “:q” but this will only work if you haven’t made any changes that have yet to be saved. To write out the changes (saving) and quit, type “:wq” and then hit enter. Write and quit. To write the file to disk without exiting, type “:w”. To exit and only write if a change has been made, type “:x”. To force a command, use an exclamation point! For example, if you made some changes that you want to discard, type “:q!” and you will quit without saving what you changed. Another useful command is “:syntax on” to turn syntax highlighting on when you’re writing your code. Syntax highlighting changes the color of the code depending on what it is, which makes it easier to read. If you are viewing code in an editor with syntax highlighting enabled, you will notice that the text is many different colors, but each color is used to represent a different thing and to break up the monotony of just having a single color for everything.
You can also configure your vi/vim installation instead of leaving it with the defaults – you can change things like word wrap, line numbering, or syntax highlighting on by default so you don’t have to do it every time you run the program. While it’s true that you can use a more graphically-oriented IDE these days to accomplish much of the same tasks, vim is still very useful. It’s especially useful if you have to remotely administrate a server, because you will be using SSH (Secure Shell), which is text-based, meaning you can’t use an IDE this way. Well, you could do complicated X.11 (GUI) forwarding over SSH, but that’s the exception, not the rule, and I wouldn’t recommend it.
To learn more about vim, you can run the command vimtutor in a console. It will help you learn more about the editor. Alternatively, you can watch someone else do it on Youtube.