DEV Community

Cover image for unix commands cheatsheet
Barbara (dev)
Barbara (dev)

Posted on • Updated on

unix commands cheatsheet

Unix was published in 1973. Since them there where many different versions build based on Unix. The most prominent are Linux and MacOS. More than 90% of cloud infrastructure and more than 80% of smartphones in the world are based on Linux.
If you want to see whats going on an EC2 instance or filter some log files you can always use the command line tools.

Using the command line tools for administrative work or lookups will save a lot of time and increase the productivity significantly compared to using only graphical interfaces.

As a webdeveloper we use the following commands on a daily basis. If you are new to this topic, I hope this will help you increase productivity.

Orientation

command explanation
history displays the history of the last used commands
pwd print working directory
man [command] manual
echo echoes an argument

Navigating in the terminal

When we keep our hands on the keyboard we are always faster, than when use the mouse or similar to navigate around. So we should make use of the following commands, when we are in a terminal.

ctrl + a: jump to the beginning of a line
ctrl + e: jump to the end of a line
ctrl + w: remove word
ctrl + u: remove whole line
option + left arrow: jump to the end of a word
option + right arrow: jump to the beginning of a word
tab: autocompletion
arrow up: show last used command
cmd + k: clear terminal screen

Navigating in the file system

command explanation
ls list - lists all files in the current directory
ls -a lists all but also shows the .hidden files
ls -la lists all files in the long file format
cd / navigate to the root directory
cd ~ navigate to the home directory
cd .. navigate one directory level up
cd - navigate to the previous directory

Administrative tasks

command explanation
sudo superuser do
mkdir [directory_name] make directory
rmdir removes a directory
touch [file_name] creates a file
cp old_file_name new_file_name copy
mv old_file_name new_file_name move
rm [file_name] remove
rm -r removes recursive, ie a Folder with all contents
vi [file_name] opens file with vi
nano [file_name] opens file with nano
code [file_name] opens file with vs code
program_name to start a program simply type the name, ie python
ctrl + c stop a program

Searching and filtering

command explanation
head [file_name] show the first 10 lines of a file
tail [file_name] show the las 10 lines of a file
sort [file_name] sorts the file alphabetically
sort [unsorted_file_name] > [sorted_file_name] sorts the content of the unsorted file and stores it in a new sorted file
cat [options] [file_names] reads files sequentially, writing them to standard output
grep [options] pattern [file_names] globally search for regular expression and print
the pipe The pipe send the output of one command as input of another command.

As grep, cat and | are powerful tools I recommend having a look at this awesome tutorial.

Permissions

In Unix Systems there are three groups of ownership.

  • USER: the one who created the file
  • GROUP: a group with assigned users
  • OTHER: any other who has not created the file and does not belong to a group.

The UNIX system has 3 permissions defined for the 3 different owner groups:

r = read permission
w = write permission
x = execute permission

– = no permission
d = directory
@ = extended attributes on macOS

image

Changing permissions

command explanation
chmod change mode
chown change owner

Detailed information for the permission system can be found here.

This post is outlining the most important commands (for me). If you want to see all commands you might want to have a look at this list or use the man in the terminal.

Discussion (2)

Collapse
jjokah profile image
John Johnson

Thanks. You reminded me of some commands I have forgotten and I also learnt new ones.


I think the ctrl + c for remove whole line is supposed to be ctrl + u


Here is one useful command I use often: ctrl + r to search for previous commands i typed on the terminal

Collapse
barbara profile image
Barbara (dev) Author • Edited

Hi John! Thanks for reading carefully and finding this typo. I will change it and at ctrl + r to the list. :)