Post

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 
CommandUsage
datePrint current date and time
exitClose current session
historyCheck the history of used commands
hostnamePrint name of host system
sudoRun command as an root/administrator
sudo shutdown -h nowShutdown computer
sudo shutdown -r nowRestart computer
sudo su -Log in as an root/adminstrator
whoamiPrints current user
Filesystem management 
CommandUsage
cat filenameRead and print content from file (Fast for reading small contents)
Use “less filename” for viewing large contents page by page
For Windows, use “filename
cdChange directory to home
cd -Change to previous directory
cd ..Change to parent directory
cd dirnameChange directory to specific directory
cd ~Change to home directory
clearClear the screen/Remove previously typed commands
For Windows, use “cls”
cp -r dirname1 dirname2Copy contents recursively from directory 1 to directory 2
For Windows, use “robocopy dirname1 dirname2
cp filename dirnameCopy file to a directory
For Windows, use “copy filename dirname
ditto -V dirname1 dirname2Copy all contents of one folder to another folder
echo “Hello World”Display message “Hello World”
echo “Hello World” > filenameCreate a file with “Hello World” or appends “Hello World” to file
find dirtosearch -name filenameFind location of a program
grep regex dirnameSearch for text patterns in directory
grep regex filenameSearch for text patterns in filename
head -100f filenameShow the first 100 lines of file in real-time
head -n 5 filenameShow first 5 lines of file
lsList directory contents.
For Windows, use “dir”
ls -aList contents including hidden files (Files that begin with a dot)
ls -lList contents with more info including permissions (long listing)
ls -rList contents reverse order
mkdir dirnameMake directory
mv dirname1 dirname2Move directory from directory 1 to directory 2
For Windows, use “move” instead of “mv”
mv filename dirnameMove file to directory
mv filename1 filename2Rename file or folder from file 1 to file 2
For Windows, use “ren filename1 filename2
mv filename1 filename2 -vRename Verbose - print source/destination directory
open dirnameOpen a file/folder.
For Windows, use “start filename
For Linux, use “xdg-open filename
ps -ef » filenameAppend 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.
pwdList path to the working directory
For Windows, use “cd”
rm -i filenameRemove directory
rm -r dirnameRemove directory with contents recursively
rm -rf dirnameRemove directory with contents recursively and
ignore non-existent files and arguments, no confirmation prompt
rm ./*Remove everything in the current folder
rm filenameRemove file
For Windows, use “del filename
rmdir dirnameRemove 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 filenameShow the last 100 lines of file in real-time.
Useful for inspecting running logs
tail -n 5 filenameShow last 5 lines of file
tar -czvf dirname.tar.gz dirnameCreate tarball
tar -tzvf dirnameSee what is in the tarball
tar xzvf dirname.tar.gzExtract tarball
tar flagsc : 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 filenameCreate a file
vi filenameCreate/Edit a file in VIM
Server and Network Management 
CommandUsage
curl -o path to fileDownload file
nc -v serverNameInitiate a telnet connection to server
netstatShow network status and protocol statistics
ping/curl serverNamePing or curl a server
scp dest1 dest2Remote copy filepath from dest 1 to dest 2
where dest1 and dest2 can be
written as user@remote_server:filepath
ssh serverNameInitiate SSH connection to server
Disk Management 
CommandUsage
df -hCheck free disk space
duList subdirectory memory usage
du -sCheck storage for specific file
Process Management 
CommandUsage
kill PIDKill processes using PID
ps -axShow all running processes
ps -ax | grep <name/pid/regex>Search process using name/PID/regex
topDisplay live process statistics
top -o rsizeDisplay 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

  1. 11 Terminal Commands You Should Know
  2. bradtraversy/terminal-commands
  3. Most useful Terminal commands for macOS (2023 Updated)
  4. Windows vs. Mac: 70+ Common and Helpful Keyboard Shortcuts
  5. The Difference Between “>” and “»” in Linux
  6. An A-Z Index of the Linux command line: bash + utilities.
  7. Stackoverflow - What does “2>&1” mean?
This post is licensed under CC BY 4.0 by the author.