Skip to content

You Don't Need GUI

Original Source

Graphical user interfaces are super friendly to computer users. They were introduced in reaction to the perceived steep learning curve of command-line interfaces (CLIs).

However, they often require more resources, are less powerful and hard to automate via scripting.

As a computer expert, we want to be more efficient and do our jobs better. We know that command words may not be easily discoverable or mnemonic, so we try to list some common tasks that you might be tempted to do in GUI.

  1. copy a file
  2. duplicate a file
  3. copy a directory
  4. duplicate a directory
  5. move a file
  6. rename a file
  7. move a directory
  8. rename a directory
  9. merge directories
  10. create a new file
  11. create a new directory
  12. show file/directory size
  13. show file/directory info
  14. open a file with the default program
  15. open a file in any application
  16. zip a directory
  17. unzip a directory
  18. peek files in a zip file
  19. remove a file
  20. remove a directory
  21. remove all files of certain criteria
  22. list directory contents
  23. tree view a directory and its subdirectories
  24. find a stale file
  25. show a calendar
  26. find a future date
  27. use a calculator
  28. force quit a program
  29. check server response
  30. view content of a file
  31. search for a text in a file
  32. search in all files in current working directory, quickly (entire disk in less than 15 minutes)
  33. view an image
  34. show disk size
  35. check cpu usage, processes and RAM
  36. know whether your computer is under load, and whether itโ€™s due to memory or CPU
  37. poweroff or reboot your computer
  38. locate USB drives
  39. unmount USB drives
  40. format USB drives
  41. check USB format
  42. run command on all files of a directory
  43. check network connectivity to a remote address and port
  44. check DNS config of a domain
  45. check the ownership and registration of a domain
  46. Quick tips
  47. Hotkeys
  48. I canโ€™t remember these cryptic commands

copy a file

STOP DRAG AND DROPPING A FILE, OR CMD/CTRL + C, CMD/CTRL + V A FILE :-1:

Copy readme.txt to the documents directory

Terminal window
$ cp readme.txt documents/

Go to table of contents ๐Ÿ”ผ

duplicate a file

STOP RIGHT CLICKING AND DUPLICATE A FILE :-1:

Terminal window
$ cp readme.txt readme.bak.txt

More advanced:

Terminal window
$ cp readme{,.bak}.txt
# Note: learn how the {} works with touch foo{1,2,3}.txt and see what happens.

Go to table of contents ๐Ÿ”ผ

copy a directory

STOP DRAG AND DROPPING A DIRECTORY, OR CMD/CTRL + C, CMD/CTRL + V A DIRECTORY :-1:

Copy myMusic directory to the myMedia directory

Terminal window
$ cp -a myMusic myMedia/
# or
$ cp -a myMusic/ myMedia/myMusic/

Go to table of contents ๐Ÿ”ผ

duplicate a directory

STOP RIGHT CLICKING AND DUPLICATE A DIRECTORY :-1:

Terminal window
$ cp -a myMusic/ myMedia/
# or if `myMedia` folder doesn't exist
$ cp -a myMusic myMedia/

Go to table of contents ๐Ÿ”ผ

move a file

STOP DRAG AND DROPPING A FILE, OR CMD/CTRL + X, CMD/CTRL + V A FILE :-1:

Terminal window
$ mv readme.txt documents/

Always use a trailing slash when moving files, for this reason.

Go to table of contents ๐Ÿ”ผ

rename a file

STOP RIGHT CLICKING AND RENAME A FILE :-1:

Terminal window
$ mv readme.txt README.md

Go to table of contents ๐Ÿ”ผ

move a directory

STOP DRAG AND DROPPING A DIRECTORY, OR CMD/CTRL + X, CMD/CTRL + V A DIRECTORY :-1:

Terminal window
$ mv myMedia myMusic/
# or
$ mv myMedia/ myMusic/myMedia

Go to table of contents ๐Ÿ”ผ

rename a directory

STOP RIGHT CLICKING AND RENAME A DIRECTORY :-1:

Terminal window
$ mv myMedia/ myMusic/

Go to table of contents ๐Ÿ”ผ

merge directories

STOP DRAG AND DROPPING TO MERGE DIRECTORIES :-1:

Terminal window
$ rsync -a /images/ /images2/ # note: may over-write files with the same name, so be careful!

Go to table of contents ๐Ÿ”ผ

create a new file

STOP RIGHT CLICKING AND CREATE A NEW FILE :-1:

Terminal window
$ touch 'new file' # updates the file's access and modification timestamp if it already exists
# or
$ > 'new file' # note: erases the content if it already exists

Go to table of contents ๐Ÿ”ผ

create a new directory

STOP RIGHT CLICKING AND CREATE A NEW DIRECTORY :-1:

Terminal window
$ mkdir 'untitled folder'
# or
$ mkdir -p 'path/may/not/exist/untitled folder'

Go to table of contents ๐Ÿ”ผ

show file/directory size

STOP RIGHT CLICKING AND SHOW FILE/directory INFO :-1:

Terminal window
$ du -sh node_modules/

Go to table of contents ๐Ÿ”ผ

show file/directory info

STOP RIGHT CLICKING AND SHOW FILE/DIRECTORY INFO :-1:

Terminal window
$ stat -x readme.md # on macOS
$ stat readme.md # on Linux

Go to table of contents ๐Ÿ”ผ

open a file with the default program

STOP DOUBLE CLICKING ON A FILE :-1:

Terminal window
$ xdg-open file # on Linux
$ open file # on MacOS
$ start file # on Windows

Go to table of contents ๐Ÿ”ผ

open a file in any application

STOP RIGHT CLICKING AND OPEN WITH :-1:

Terminal window
$ open -a appName file

Go to table of contents ๐Ÿ”ผ

zip a directory

STOP RIGHT CLICKING AND COMPRESS DIRECTORY :-1:

Terminal window
$ zip -r archive_name.zip folder_to_compress

Go to table of contents ๐Ÿ”ผ

unzip a directory

STOP RIGHT CLICKING AND UNCOMPRESS DIRECTORY :-1:

Terminal window
$ unzip archive_name.zip

Go to table of contents ๐Ÿ”ผ

decompress files of any format

STOP RIGHT CLICKING AND UNCOMPRESS DIRECTORY :-1:

Terminal window
$ unar archive_name.zip
$ unar archive_name.7z
$ unar archive_name.rar
$ unar archive_name.ISO
$ unar archive_name.tar.gz

Go to table of contents ๐Ÿ”ผ

peek files in a zip file

STOP USING WinRAR :-1:

Terminal window
$ zipinfo archive_name.zip
# or
$ unzip -l archive_name.zip

Go to table of contents ๐Ÿ”ผ

peek files in a compress file of any format

STOP USING WinRAR :-1:

Terminal window
$ lsar -l archive_name.zip
$ lsar -l archive_name.7z
$ lsar -l archive_name.ISO
$ lsar -l archive_name.rar
$ lsar -l archive_name.tar.gz

Go to table of contents ๐Ÿ”ผ

remove a file

STOP RIGHT CLICKING AND DELETE A FILE PERMANENTLY :-1:

Terminal window
$ rm my_useless_file

IMPORTANT: The rm command deletes my_useless_file permanently, which is equivalent to move my_useless_file to Recycle Bin and hit Empty Recycle Bin.

Go to table of contents ๐Ÿ”ผ

remove a directory

STOP RIGHT CLICKING AND DELETE A DIRECTORY PERMANENTLY :-1:

Terminal window
$ rm -r my_useless_folder

Go to table of contents ๐Ÿ”ผ

remove all files of certain criteria

Terminal window
$ find . -name "*.bak" -type f -delete

IMPORTANT: run find . -name "*.bak" -type f first to see exactly which files you will remove.

Go to table of contents ๐Ÿ”ผ

list directory contents

STOP OPENING YOUR FINDER OR FILE EXPLORER :-1:

Terminal window
$ ls my_folder # Simple
$ ls -la my_folder # -l: show in list format. -a: show all files, including hidden. -la combines those options.
$ ls -alrth my_folder # -r: reverse output. -t: sort by time (modified). -h: output human-readable sizes.

Go to table of contents ๐Ÿ”ผ

tree view a directory and its subdirectories

STOP OPENING YOUR FINDER OR FILE EXPLORER :-1:

Terminal window
$ tree # on Linux
$ find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g' # on MacOS
# Note: install homebrew (https://brew.sh) to be able to use (some) Linux utilities such as tree.
# brew install tree

Go to table of contents ๐Ÿ”ผ

find a stale file

STOP USING YOUR FILE EXPLORER TO FIND A FILE :-1:

Find all files modified more than 5 days ago

Terminal window
$ find my_folder -mtime +5

Go to table of contents ๐Ÿ”ผ

show a calendar

STOP LOOKING UP WHAT THIS MONTH LOOKS LIKE BY CALENDAR WIDGETS :-1:

Display a text calendar

Terminal window
$ cal

Display selected month and year calendar

Terminal window
$ cal 11 2018

Go to table of contents ๐Ÿ”ผ

find a future date

STOP USING WEBAPPS TO CALCULATE FUTURE DATES :-1:

What is todays date?

Terminal window
$ date +%m/%d/%Y

What about a week from now?

Terminal window
$ date -d "+7 days" # on Linux
$ date -j -v+7d # on MacOS

Go to table of contents ๐Ÿ”ผ

use a calculator

STOP USING CALCULATOR WIDGET :-1:

Terminal window
$ bc -l

Go to table of contents ๐Ÿ”ผ

force quit a program

STOP CTRL + ALT + DELETE and choose the program to kill :-1:

Terminal window
$ killall -9 program_name

Go to table of contents ๐Ÿ”ผ

check server response

STOP OPENING A BROWSER :-1:

Terminal window
$ curl -i umair.surge.sh
# curl's -i (--include) option includes HTTP response headers in its output.

Go to table of contents ๐Ÿ”ผ

view content of a file

STOP DOUBLE CLICKING A FILE :-1:

Terminal window
$ cat apps/settings.py
# if the file is too big to fit on one page, you can use a 'pager' (less) which shows you one page at a time.
$ less apps/settings.py

Go to table of contents ๐Ÿ”ผ

search for a text in a file

STOP CMD/CTRL + F IN A FILE :-1:

Terminal window
$ grep -i "Query" file.txt

Go to table of contents ๐Ÿ”ผ

search in all files in current working directory, quickly (entire disk in less than 15 minutes)

STOP CMD/CTRL + F IN A DIRECTORY :-1:

Terminal window
$ ripgrep -i "Query"
# brew install ripgrep

Go to table of contents ๐Ÿ”ผ

view an image

STOP USING PREVIEW :-1:

Terminal window
$ imgcat image.png
# Note: requires iTerm2 terminal.

Go to table of contents ๐Ÿ”ผ

show disk size

STOP RIGHT CLICKING DISK ICON OR OPENING DISK UTILITY :-1:

Terminal window
$ df -h

Go to table of contents ๐Ÿ”ผ

check cpu usage, processes and RAM

STOP OPENING YOUR ACTIVITY MONITOR OR TASK MANAGER :-1:

Terminal window
$ top

if you want some more details:

Terminal window
$ htop

Go to table of contents ๐Ÿ”ผ

know whether your computer is under load, and whether itโ€™s due to memory or CPU

Terminal window
$ glances
# brew install glances

Go to table of contents ๐Ÿ”ผ

poweroff or reboot your computer

This can be useful when youโ€™re patching a server that is acessed via SSH and you donโ€™t have a GUI.

Terminal window
# poweroff
$ sudo shutdown -h now
# reboot
$ sudo shutdown -r now

Go to table of contents ๐Ÿ”ผ

locate USB drives

Terminal window
$ df

Go to table of contents ๐Ÿ”ผ

unmount USB drives

Terminal window
$ sudo umount /dev/sdb1

Go to table of contents ๐Ÿ”ผ

format USB drives

Terminal window
# FAT32
$ sudo mkfs.vfat /dev/sdb1
# NTFS
$ sudo mkfs.ntfs /dev/sdb1
# exFAT
$ sudo mkfs.exfat /dev/sdb1

Go to table of contents ๐Ÿ”ผ

check USB format

Terminal window
$ sudo fsck /dev/sdb1

Go to table of contents ๐Ÿ”ผ

run command on all files of a directory

STOP CLICKING THE FILES ONE BY ONE :-1:

Terminal window
$ for FILE in *; do echo $FILE; done

Go to table of contents ๐Ÿ”ผ

check network connectivity to a remote address and port

STOP USING NETWORK UTILITY

Terminal window
$ nc -vz www.google.com 443
$ nc -vz 1.1.1.1 53

Go to table of contents ๐Ÿ”ผ

check DNS config of a domain

STOP USING NETWORK UTILITY

Terminal window
$ dig www.google.com

Go to table of contents ๐Ÿ”ผ

check the ownership and registration of a domain

STOP USING NETWORK UTILITY AND THE WEBSITE OF DOMAIN REGISTRATION PROVIDERS

Terminal window
$ whois www.google.com

Go to table of contents ๐Ÿ”ผ

Quick tips

Go to table of contents ๐Ÿ”ผ

Hotkeys

Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + L Clears the Screen, similar to the clear command
Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H Same as backspace
Ctrl + R Lets you search through previously used commands
Ctrl + C Kill whatever you are running
Ctrl + D Exit the current shell
Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W Delete the word before the cursor
Ctrl + K Clear the line after the cursor
Ctrl + T Swap the last two characters before the cursor
Ctrl + F Move cursor forward one character
Ctrl + B Move cursor backward one character
Esc + T Swap the last two words before the cursor
Alt + T Same as Esc + T
Alt + F Move cursor forward one word on the current line
Alt + B Move cursor backward one word on the current line
Esc + F Same as Alt + F
Esc + B Same as Alt + B
Alt + . Paste the last word of the most recently command
Tab Auto-complete files and directory names

Go to table of contents ๐Ÿ”ผ

I canโ€™t remember these cryptic commands

You can always google or man the commands you are not familiar with. Or, checkout tldr, a collection of simplified and community-driven man pages.

Go to table of contents ๐Ÿ”ผ