Getting Started With The Command Line
What is Command Line?
A user interface where one can enter text prompts to execute commands or automate tasks.
The terms: Command Line, Bash, Shell, Terminal, Command Prompt, are used interchangeably but they all generally mean the same thing. Below are some quick descriptions for each term.
Command Line Interface (CLI) is a user interface where users type in commands and view outputs printed on the screen.
Bash is a shell and command language in Linux.
Shell is a software that interprets and executes the various commands that we type in the CLI.
Terminal, is a CLI application in Mac OS.
Command Prompt, is a CLI application in Windows OS.
Benefits of using the Command Line
While many users are accustomed to using a graphical user interface (GUI), the command line offers the following benefits:
- Direct interaction with the computer, with no overhead processing from the GUI, allowing faster and more efficient execution of tasks.
- Automation of commands and tasks by running scripts.
- Makes you feel like a hacker (sort of)
What can we use it for?
In my day-to-day, I use the command line to:
- Inspect details of my localhost/servers/applications/processes
- Navigate and manipulate files and folders (Filesystem Management)
- Access servers over networks
- Run applications
- Debug logs
- Write and run scripts to automate tasks/applications
- Version Control (Git)
Note: The above list is not exhaustive, use cases will differ between individuals
Cheatsheet
The below cheatsheet is a compilation of commands that I either frequently used or found useful along the way when I approach the command line.
General Usage | |
---|---|
Command | Usage |
date | Print current date and time |
exit | Close current session |
history | Check the history of used commands |
hostname | Print name of host system |
sudo | Run command as an root/administrator |
sudo shutdown -h now | Shutdown computer |
sudo shutdown -r now | Restart computer |
sudo su - | Log in as an root/adminstrator |
whoami | Prints current user |
Filesystem management | |
---|---|
Command | Usage |
cat filename | Read and print content from file (Fast for reading small contents) Use “less filename ” for viewing large contents page by pageFor Windows, use “ filename ” |
cd | Change directory to home |
cd - | Change to previous directory |
cd .. | Change to parent directory |
cd dirname | Change directory to specific directory |
cd ~ | Change to home directory |
clear | Clear the screen/Remove previously typed commands For Windows, use “cls” |
cp -r dirname1 dirname2 | Copy contents recursively from directory 1 to directory 2 For Windows, use “robocopy dirname1 dirname2 ” |
cp filename dirname | Copy file to a directory For Windows, use “copy filename dirname ” |
ditto -V dirname1 dirname2 | Copy all contents of one folder to another folder |
echo “Hello World” | Display message “Hello World” |
echo “Hello World” > filename | Create a file with “Hello World” or appends “Hello World” to file |
find dirtosearch -name filename | Find location of a program |
grep regex dirname | Search for text patterns in directory |
grep regex filename | Search for text patterns in filename |
head -100f filename | Show the first 100 lines of file in real-time |
head -n 5 filename | Show first 5 lines of file |
ls | List directory contents. For Windows, use “dir” |
ls -a | List contents including hidden files (Files that begin with a dot) |
ls -l | List contents with more info including permissions (long listing) |
ls -r | List contents reverse order |
mkdir dirname | Make directory |
mv dirname1 dirname2 | Move directory from directory 1 to directory 2 For Windows, use “move” instead of “mv” |
mv filename dirname | Move file to directory |
mv filename1 filename2 | Rename file or folder from file 1 to file 2 For Windows, use “ren filename1 filename2 ” |
mv filename1 filename2 -v | Rename Verbose - print source/destination directory |
open dirname | Open a file/folder. For Windows, use “start filename “For Linux, use “xdg-open filename ” |
ps -ef » filename | Append the piped output of ps -ef to file ”>” : Overwrite existing file or creates a file if the file is not present in directory ”»”: Append existing file or creates a file if the file is not present in directory. |
pwd | List path to the working directory For Windows, use “cd” |
rm -i filename | Remove directory |
rm -r dirname | Remove directory with contents recursively |
rm -rf dirname | Remove directory with contents recursively and ignore non-existent files and arguments, no confirmation prompt |
rm ./* | Remove everything in the current folder |
rm filename | Remove file For Windows, use “del filename ” |
rmdir dirname | Remove empty directory |
tail -10 input_file | grep “Hello World” » output_file | Pipe last 10 lines of input_file and grep texts matching “Hello World” to output_file . |
tail -100f filename | Show the last 100 lines of file in real-time. Useful for inspecting running logs |
tail -n 5 filename | Show last 5 lines of file |
tar -czvf dirname .tar.gz dirname | Create tarball |
tar -tzvf dirname | See what is in the tarball |
tar xzvf dirname .tar.gz | Extract tarball |
tar flags | c : Creates Archive x : Extract the archive f : creates archive with given filename t : displays or lists files in archived file u : archives and adds to an existing archive file v : Displays Verbose Information A : Concatenates the archive files z : Zip - tellstar command that creates tar file using gzip j : filter archive tar file using tbzip W : Verify a archive file r : update or add file or directory in already existed .tar file |
touch filename | Create a file |
vi filename | Create/Edit a file in VIM |
Server and Network Management | |
---|---|
Command | Usage |
curl -o path to file | Download file |
nc -v serverName | Initiate a telnet connection to server |
netstat | Show network status and protocol statistics |
ping/curl serverName | Ping or curl a server |
scp dest1 dest2 | Remote copy filepath from dest 1 to dest 2 where dest1 and dest2 can be written as user @remote_server :filepath |
ssh serverName | Initiate SSH connection to server |
Disk Management | |
---|---|
Command | Usage |
df -h | Check free disk space |
du | List subdirectory memory usage |
du -s | Check storage for specific file |
Process Management | |
---|---|
Command | Usage |
kill PID | Kill processes using PID |
ps -ax | Show all running processes |
ps -ax | grep <name/pid/regex> | Search process using name/PID/regex |
top | Display live process statistics |
top -o rsize | Display processes with the highest memory usage |
nohup file > logFile & | Run process in the background (&) and pipes the standard output to log file |
nohup file > logFile 2>&1 & | Run process in the background (&) and pipes both standard output and error (2>&1) to log file |
Conclusion
The command line is the bread and butter application for all computer systems. Not only will it automate and speed up your day-to-day development work, it serves as a starting point for you to run, inspect and monitor the applications and filesystems of servers directly. For many who are initially starting out, this application looks and feels like a “black box”. But rest assured, with sufficient exposure and practise, you will eventually master it and enjoy the many benefits this application brings to your everyday workflow.
Sources
- 11 Terminal Commands You Should Know
- bradtraversy/terminal-commands
- Most useful Terminal commands for macOS (2023 Updated)
- Windows vs. Mac: 70+ Common and Helpful Keyboard Shortcuts
- The Difference Between “>” and “»” in Linux
- An A-Z Index of the Linux command line: bash + utilities.
- Stackoverflow - What does “2>&1” mean?