Skip to content

Cheat Sheet Linux


CLI Commands and shortcuts

Command Description Example Explanation
!! Repeat last command $ apt-get install curl
Permission Denied
$ sudo !!
No retyping required
!$ Reuse last argument $ mkdir logs
$ cd !$
!$ becomes logs
^old^new Replace typo in last command $ cat fil.txt
$ ^fil^file
No retying required
Alt + . Cycle last arguments $ cat config.yaml
cat <Alt+.>
inserts server_config.yml
xargs Turns outputs into arguments $ ls *.log | xargs rm xargs takes all the .log files and gives them to rm for deletion
tee Save outout whilst viewing it $ male build | tee output.log Watch the output on screen whilst saving to a log
grep -R "lookup" Search inside files $ grep -R "error" . Scans all files in current DIR recursively for "error"
fc Edit and re-run last command $ fc Opens the last command in Vim so you can more easily edit it
!!:n Get a specific argument from last command $ echo dog cat monkey
$ echo !!:2
$ cat
2 represents the argument placement
Ctrl + a Move around CLI Ctrl + a Moves cursor to the start of the line
Ctrl + e Move around CLI Ctrl + e Moves cursor to the end of the line
CTRL + w Quick delete CTRL + w Deletes word before the cursor
CTRL + u Quick delete CTRL + u Deletes everything after the cursor
!!:gs/old/new Replaces text $ echo dog dog
$!!:gs dog/cat
$ cat cat
Find keyword and replace
df -h Check disk usage $ df -h Disk free space in human readable format
df -sh * Check disk usage $ df -sh * Files and folder sizes in current DIR
lsof -i :<port> What's using the port $ lsof -i :80 What's running HTTP on the server
nc -zv <host> <port> Is this port accessible $ nc -zv grinntec.net 443 Can i reach Grinntec on HTTPS

Alias

Open the bash config file

sudo nano ~/.bashrc

Add your alias to the end of the file, for example:

alias ll="ls -lh"
alias cd..="cd .."

Restart your shell

List running services

systemctl list-units --type=service --state=running

Create command alias

Edit the shell config file

nano ~/.bashrc

Add the alias to the end of the file. This example allows you to use just ls to get a full ls -la output.

alias ls='ls la'

Restart the shell

source ~/.bashrc

Rename a directory

To rename a directory in Linux, you can use the mv command with the current directory name and the new directory name.

mv [current directory name] [new directory name]

Display all users

To display all the users who exist on the server and sort them by name, you can use the "cat" command to read the "/etc/passwd" file and then pipe the output to the "cut" and "sort" commands.

cat /etc/passwd | cut -d: -f1 | sort

List file including hidden

ls -la ~

Check status of Apache

systemctl status apache2

Show the OS release

lsb_release -a

apt-get error: repository suite error

error when using apt-get update; the suite has changed to oldstable and needs to be accepted before you can use it

Reading package lists... Done
E: Repository 'http://archive.raspberrypi.org/debian buster InRelease' changed its 'Suite' value from 'testing' to 'oldstable'
N: This must be accepted explicitly before updates for this repository can be applied. See apt-secure(8) manpage for details.
E: Repository 'http://raspbian.raspberrypi.org/raspbian buster InRelease' changed its 'Suite' value from 'stable' to 'oldstable'
N: This must be accepted explicitly before updates for this repository can be applied. See apt-secure(8) manpage for details.

To accept the change to the suite value run the following command

sudo apt-get update --allow-releaseinfo-change