The command line is the ultimate power tool on your computer. Using it can feel a bit intimidating, overwhelming and complex, but it allows you tonavigate through files and folders, create, copy, move and delete files and directories, execute commands with multiple options, install and run programs, etc.
You can be more productive using a terminal than a GUI, and you can easily automate tasks, such as creating, copying, and converting files, backing up files or folders to a portable hard drive, cleaning up your system, installing multiple programs at once, etc.
Open your terminal
- Windows: Open the start menu and type in terminal, cmd or PowerShell. Alternatively, press Windows and R, then type cmd or PowerShell in the Run window.
- macOS: Open spotlight search by hitting command and the space bar and type in terminal. If you are going to use the Terminal very often, you should add it to the Dock. Right click on the terminal icon, choose Options, Keep To Dock.
- GNU/Linux: Use the keyboard shortcut Ctrl + Alt + T. Besides, you can open a terminal from the Activities menu in GNOME. Simply type terminal and click on the result that pops up.
Basic commands
- Change directory: cd newDirectory; cd .. (it goes up one level of the directory tree).
- Moving files and directories.
- Windows: move myFile.doc c:\myDirectory; move myDirectoryTarget myDirectoryDestination
- macOS/Linux: mv myFile.txt myDirectory; mv myDirectoryTarget myDirectoryDestination
- Copying files and directories.
- Windows: copy myFile.doc c:\myDirectory; copy *.txt c:\myDirectory -it copies all the txt files in the current directory to c:\myDirectory-; xcopy myDirectoryTarget myDirectoryDestination /e -this command copies all files and folders recursively even empty directories-.
- macOS/Linux: cp myFile.doc myDirectory; cp *.txt myDirectory; cp -r myDirectoryTarget myDirectoryDestination -copying a directory is the same as copying a file, but you need to use the recursive -r option.
- Windows: del myFile.txt; del *.txt; rmdir myDirectory /s. /s deletes a directory tree (the specified directory -myDirectory- and all its subdirectories, including all files).
- macOS/Linux: rm myFile.txt; rm *.txt; rm -r myDirectory. rm -r removes directories and their contents recursively.
- Windows: dir (lists all files and directories contained in the current directory), tree (displays the directory structure).
- macOS/Linux: ls, ls -a (list hidden files, i.e., do not ignore entries starting with .)
- Making a new folder: mkdir newDirectory
- Print the path of the current working directory: Windows (cd), macOS/Linux (pwd).
- Clear screen: Windows (cls); macOS/Linux (clear).
- Get help on the shell: Windows (help); macOS/Linux (man, whatis, tldr, apropos, cheat).
- To show the content of a file: Windows (type myFile.txt); macOS/Linux (cat/more/less myFile.txt)
- Edit text files: Windows (notepad somefile.txt); macOS/Linux (vi/vim/nano myFile.txt)
Windows commands
- Compares two files and displays the differences between them, e.g., fc c:\myDirectory\myFile1 c:\myDirectory\myFile2
- Manage Processes in Windows. tasklist displays a list of currently running processes on your computer. Each task has a PID (process ID). Kill a process: taskkill -t -f -pid pidProcess (-f specifies the process to be forcefully ended; -t ends the process and any child processes started by it), taskkill -im “process name” -f
- Network commands
- ipconfig /all, it displays the full TCP/IP configuration for all adapters; ipconfig /flushdns, it flushes and resets the contents of the DNS client resolver cache; ipconfig /release followed by ipconfig /renew forces your Windows into asking for a new IP address.
- netstat -an helps you identify currently open ports and related IP addresses.
- ping myWebDomain/myIPAdress checks if a web domain or IP address is accessible or not.
- tracert myWebDomain.com/myIPAdress shows the number of steps or hops between your PC and the target, along with how much time each hop takes.
- Shutdown your computer: shutdown. Restart the computer after shutdown: shutdown /r. Restart the computer and launch the Advanced Start Option: shutdown /r /s
- Repair missing or corrupted system files: sfc scannow (Run it as an administrator). Check your file system for logical and physical errors: chkdsk c: /f /r /x; /f fixes errors on the disk; /r locates bad sectors and recovers readable information; /x forces the volume to dismount first, if necessary.
- systeminfo displays detailed configuration information about your PC and its operating system. ver displays your operating system version. time/date displays/sets the system time/date.
- Install applications: winget install appName / choco install appName -y. Update all installed applications: winget upgrade ‐‐all / choco upgrade all -y
- start . opens the current directory in File Explorer.
start c:\MyFiles\MyDocument.doc # Opens the file MyDocument.doc in Word or WordPad.
start https://www.justothepoint.com # Opens JustToThePoint in your default browser.
macOS/Linux commands
- history shows all commands you have used recently.
- Manage Processes. To display all running processes, type ps -aux | less or top. To kill a process, type sudo kill -9 pidProcess (-9 is a special kill signal that nearly always guarantee to kill the nasty process) or sudo killall nameProcess
- Network commands
- ifconfig -a or ip a displays information for all network interfaces. To enable/disable a network interface, type ifconfig eth0 up/down To assign an IP Address to a network interface, write: ifconfig eth0 192.168.0.7
- netstats or ss | less gives you information about your network connections, the ports that are in use, and the processes using them.
- ping myWebDomain/myIPAdress checks if a web domain or IP address is accessible or not.
- traceroute myIPAddress/myWebPage prints the route packets take to reach the destination specified by the user. It displays each network hop and the time taken to reach it. To install it, use the following command: sudo pacman -Sy traceroute (Arch), sudo apt-get install traceroute (Ubuntu).
- Stop, poweroff, and reboot your machine: sudo halt/poweroff/reboot
- Repair file system errors.
- View all mounted disks and partitions (df -h) and unmount the disk: unmount /dev/sdb (/dev/sda is the first hard drive; /dev/sdb is the second drive, etc.)
- Repair errors automatically with fsck: fsck -y /dev/sdb
- Run fsck on Linux Root Partition: Reboot (sudo reboot); Press and hold the shift key during boot-up so that the grub menu is shown. Select the Advanced options, then Recovery mode, fsck. You will be asked the following question, Continuing will remount your / filesystem in read/write mode and mount any other filesystem defined in /etc/fstab? Do you wish to continue? Confirm by selecting Yes. Finally, select resume to boot up your machine.
-
Install applications. macOS: brew install appName. Update all installed applications: brew upgrade. Install/Update all packages (Arch): sudo pacman -S appName/sudo pacman -Syu. Install/Update all packages (Ubuntu): sudo apt install appName/sudo apt update && sudo apt upgrade -y.
sudo softwareupdate -ia && sudo reboot updates your macOS, then reboots.
-
Information about the system
- macOS: diskutil list lists all disks and their partitions. system_profiler reports on the hardware and software configuration of your macOS system. sw_vers prints your macOS operating system version information.
- GNU/Linux: The lshw is a nifty command-line tool to extract detailed information on the hardware configuration of your system (sudo apt-get install lshw). lsblk lists out all the storage blocks you have on your system, which includes hard drive partitions, flash drives, and optical drives. uname is a command-line utility that prints basic information about the operating system and system hardware: uname -a
Other commands for macOS and GNU/Linux
- cal: displays a simple calendar. By default, if no arguments are specified, the current month is displayed.
- neofetch is a command-line system information tool. It displays information about your system next to your OS logo. Installation: brew install neofetch (macOS), sudo apt install neofetch (Ubuntu), pacman -S neofetch (Arch).
You can change Neofetch’s config file (nvim .config/neofetch/config.conf) to customize it and display exactly what you want it to.
- fortune-mod is a command-line utility which displays a random quotation from a collection of quotes. Installation: brew install fortune (macOS), sudo pacman -S fortune-mod (Arch Linux), sudo apt install fortune-mod (Ubuntu, Linux Mint).
We are going to add our favorite quotes. The format needs to be as follows:
You cannot achieve the impossible without attempting the absurd.
% \
nvim .zsh_aliases: alias fortune=’fortune ~/.config/myquotes/fortunes'
mkdir myquotes && cd myquotes
wget https://github.com/ruanyf/fortunes/blob/master/data/fortunes
strfile fortunes # strfile creates a random access file for storing strings
- cowthink/cowsay are programs that generate ASCII pictures of a cow with a message, e.g. fortune | cowsay -f tux.
- bc is a command line calculator.
user@pc:~$ bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2*4
8
((7+5)/2)^2
36
(3^2)*(9)/3
27
- defaults is a tool that is used to access (view and modify) the Mac OS X user defaults system.
user@pc:~$ user@pc:~$ defaults write com.apple.finder AppleShowAllFiles -bool true # It shows hidden files and folders.
user@pc:~$ user@pc:~$ defaults write com.apple.finder AppleShowAllFiles -bool false # It disables the display of hidden files and folders.
defaults write com.apple.screencapture location ~/Dropbox # It changes the default location for screenshots.
defaults write com.apple.screencapture type JPG # It changes the default file type of screenshots.
defaults write com.apple.screencapture disable-shadow -bool true # It disables shadows for all window screenshots captured on your macOS.
killall SystemUIServer
- Download a file from the internet on the command line: curl https://your-domain/your-file.ext. Installation: sudo apt install curl (Debian, Ubuntu), brew install curl (macOS), pacman -S curl (Arch).
- open . will open the current working directory in a Finder windows. open mydocument.rtf will open mydocument.rtf with the default application for rtf files.
open -a gimp # Opens Gimp, the GNU Image Manipulation Program.
open https://justothepoint.com # Opens an URL with the default browser.
- caffeinate prevents your Mac from going to sleep: caffeinate (To stop it, just type Ctrl+C)/caffeinate -t 18000 (it prevents your Mac from going to sleep for 5 hours=18000 seconds).
- Say is a command-line program that will speak anything you type after the command, e.g., say hi my master
- Do you want to be the coolest nerd ever? bash-insulter randomly insults you when typing wrong command.
git clone https://github.com/hkbakke/bash-insulter.git bash-insulter # First, clone the repository
sudo cp bash-insulter/src/bash.command-not-found /etc/ # The script is quite simple, do not worry about it.
vim .zshrc (Zsh) or vim etc/bash.bashrc (Bash)
if [ -f /etc/bash.command-not-found ]; then
. /etc/bash.command-not-found
fi
echo 'Defaults insults' >> /etc/sudoers # Sudo insults you if you enter an incorrect password.