Linux Commands Cheat Sheet 2234
Linux Commands Cheat Sheet 2234
65
Linux
Commands
From
FILES
DECEMBER 2016 IMPRINTMAGAZINE.COM
MANAGEMENT
These commands are the basics that every Linux
beginner should learn to browse the Linux files tree
from a terminal
Reminder:
For example, on the image you can see the tree for
this folder : /home/pat
. CD <FOLDER> . LS (FOLDER)
Changes directory, go to the specified folder Lists files and directory, in the current or
specified folder
Absolute path: cd /home/pat/test Current directory: ls
Relative path: cd test Specific: ls /home/pat/test
NB: "Absolute" is when you use the entire path
For "relative" you only enter the path from your current NB: You can use options with ls to get a more detailed view of
directory (in the second example, you need to already be in the files and folder, ex: ls -latr /home/pat
/home/pat folder)
NB: A pattern allows you to display all files content for similar
NB: The mv command is always in recursive mode
files
https://raspberrytips.com
FILES
DECEMBER 2016 IMPRINTMAGAZINE.COM
MANAGEMENT
(2)
. GREP
Grep is a powerful (and complex) tool to There are also advanced usages possible:
search string in a text or file
Regular expressions: grep "dhcp\|dns" /var/log/syslog
Find string in a file grep "dhcp" /var/log/syslog
Command options: grep -A2 -B4 'Fatal error' /var/log/syslog
Filter a command output ls -latr | grep ".php"
Inverted search: grep -v 'Notice' /var/log/syslogi
With a script: /home/pat/script.sh | grep error
The | in the regular expressions allows you to use OR (one or more condition)
NB: The | option (pipe), allows you to run a command on another one output The -A option also catch X lines "after" the matched condition, -B is for
You need to use quotes for complex search with space or special characters "before"
Finally, the -v option is to filter lines that don't match the condition
Options:
NB: The -r option is to compress all the folder content
-c is to Compress, -x to eXtract
You can use the -d option to extract files in a specific folder
-v: verbose mode, -z: use gZip to compress, -f specify the file name
Use "man zip" or "man unzip" for all available options
Use "man tar" for more information
https://raspberrytips.com
FILES
DECEMBER 2016 IMPRINTMAGAZINE.COM
MANAGEMENT
(3)
. PWD . TREE
An easy command to display you current Another tool to get details on your current
directory location, in a tree format
Example: pwd Current directory: tree
Specific folder: tree /home/pat/
. FIND
Find allows you to search files on your More advanced options:
computer, there are many options
File size: find / -size +10M
Find a file name: find /home/pat -iname test.txt
Recently modified files: find /home -mtime -2
Filter extensions: find /home/pat -iname *.php
find / -type d -iname test Run command on results:
Find only directories: find /var/log -iname *.log.gz -exec rm {} \;
The first command display all files over 10M on the disk
NB: -iname stands for "insensitive case", you can use -name if you prefer The -mtime -2 checks files modified in the last two days
You can use "-type f" to find only files The {} parameter in the last command will be replaced by the file name
Check the "man find" for more information
https://raspberrytips.com
NETWORK
DECEMBER 2016 IMPRINTMAGAZINE.COM
COMMANDS
Here are the main commands to know to manage and
use the network on your computer or server.
Reminder:
Most computers come with two interfaces or more : Ethernet and Wi-Fi.
In general, Ethernet is named eth0 and the Wi-Fi one is wlan0.
. IFCONFIG / IP . IWCONFIG
Displays your current network Shows information about the wireless
configuration (IP Address, Mac Address, ...) network configuration (SSID, speed, ...)
Usage: ifconfig Usage: iwconfig
NB: Ifconfig is no longer included with all distributions. NB: You can also display a specific interface with iwconfig wlan0
Use "ip a" instead if it doesn't work.
NB: It can help to disable the wireless interface while connected by cable NB: Read the "man ping" to see all available options
. RSYNC
Similar to SCP with more options like delta
comparison and some other optimizations
Local copy: rsync /home/pat/* /media/usb/
Syntax: rsync <file> <user>@<ip>:<path> Remote recursive rsync -auzr /home/pat/Documents/*
Example: rsync test.txt pat@192.168.1.1:/home/pat/ copy: pat@192.168.1.1:/home/pat/Documents/
https://raspberrytips.com
PACKAGES
DECEMBER 2016 IMPRINTMAGAZINE.COM
MANAGEMENT
Once you have the network working, you'll probably
update your system and install the packages you need.
On this page, you have all the required commands to
do this from a terminal.
Vocabulary:
NB: You need to run apt update before doing this, to get the
NB: To add a new repository, you can edit the apt configuration
latest versions
in /etc/apt/sources.list, or follow the instructions from the
The -y option allows you to automatically accept the installation
software editor
NB: Read the "man dpkg" output to get all possible options from NB: Use the following search command to know the exact name
this command of a package
.
NB: You don't need sudo for this one
MANUAL INSTALLATION
Sometimes, you need to install packages manually,
if the editor doesn't provide a repository
Download the file with wget:
wget https://www.realvnc.com/download/file/viewer.files/VNC-
Viewer-6.19.325-Linux-amd64.deb
Manual installation:
sudo dpkg -i VNC-Viewer-6.19.325-Linux-amd64.deb
https://raspberrytips.com
SYSTEM
DECEMBER 2016 IMPRINTMAGAZINE.COM
MANAGEMENT
Now that you have all packages installed, you may
need to learn more advanced commands on how to
manage your operating system.
. REBOOT . SHUTDOWN
This command will restart your computer Stops the computer, now or at a specific
immediately time
Usage: sudo reboot Stop now: sudo shutdown -h now
At a specific time: sudo shutdown -h 20:00
. HTOP . DF
A great alternative to top, to display system load Displays your partition list, a good way to
and process in an intuitive interface check the remaining disk space
Usage: htop
Basic usage: df
More readable: df -h
NB: htop is not installed by default, install it with Specific partition: df -h /media/usb
"apt install htop"
https://raspberrytips.com
SYSTEM
DECEMBER 2016 IMPRINTMAGAZINE.COM
MANAGEMENT
(2)
. DU . MOUNT
Displays the disk space usage in the current Mount a new partition (usb key for example)
or specified folder
Basic usage: du Mount disk: sudo mount /dev/sda1 /mnt/usb
Specific folder: du /home/pat Unmount: sudo umount /mnt/usb
Summarize: du --summarize /home/pat
20 biggest files: du -ak | sort -nr | head -20
NB: It's a complex command for beginner, but this post will give
NB: There are a lot more options, check the "man du" to find
you all the needed informations
more help about this one
https://raspberrytips.com/mount-usb-drive-raspberry-pi/
. DATE . UPTIME
As the name says, display the current date Displays the current uptime of the computer
and time or server (how much time on)
Full output: date
Specific format: date +%m-%d-%Y Basic usage: uptime
Last boot date: uptime -s
NB: The "man date" command gives you the list of all availables
options and format
. CHOWN . CHMOD
Changes file owner and group Changes file or folder permissions
. MAN <COMMAND>
I already give it many times in this document,
but man allows finding help for any command
https://raspberrytips.com
MISCELLANEOUS
DECEMBER 2016 IMPRINTMAGAZINE.COM
COMMANDS
In this part, I want to give you all others useful
commands that don't fit into the others categories.
. HISTORY . CRONTAB
Linux stores any command you type in an Allows you to schedule tasks on your
archive file, you can read it with "history" computer
All commands: history List current tasks: crontab -l
Last 20: history | tail -n 20 Edit tasks: crontab -e
Clear all history: history -c
Clear one line: history -d 123 NB: The crontab syntax is a tough to understand for beginners,
use this tool to check your line is correct:
https://webinpact.com/crontab-generator/
. | . SCREEN
I already show you the pipe in a lot of Run a virtual terminal, to let a session
examples, it allows you to combine multiple running in background
commands to find exactly what you want screen -S <name>
Start a screen:
Syntax: <command1> | <command2> Exit a screen: CTRL+A CTRL+D
Grep example: cat test.txt | grep error Resume a screen: screen -r <name>
Double: du -ak | sort -nr | head -20 Stop a screen: CTRL+D
. ! . !!
Run a specific command from the history Similar to ! but to run the last command
again
Syntax: !<history_id> Usage: !!
Example: !123
. > . >>
Create a file to store the command output Add the command output at the end of a file
Syntax: <command> > <filename> Usage: cat test.txt | grep error >> error.log
Example: cat test.txt | grep error > error.log
https://raspberrytips.com
WARRIORS
DECEMBER 2016 IMPRINTMAGAZINE.COM
COMMANDS
And finally, now that you're an expert with a terminal, let's see
some tricky commands to push your limits :)
They can be hard to use, with a lot of options, or hard to analyze
. AWK . SED
Awk is close to a programming language Similar to awk, but for regular expressions
Allows you to search string and transform them to display only
differently
Syntax: awk [-F] [-v var=value] 'program' file Syntax: sed <option> <script> <file>
Basic example: awk -F":" '{print $1}' /etc/passwd Basic example: sed '/^#/d' /etc/apache2/apache2.conf
NB: The last command displays only the first column
I can't explain to you the awk usage in detail in a few lines NB: The last command remove comments from the configuration
Check this guide to learn more about this: As for awk, you'll need serious tutorials and experience to
https://do.co/2VC8mnm master this one.
. CUT . WC
Another way to transform text in a command line, WC stands for "Words Count" and also gets lines
probably easier to understand count, characters count and file size
Syntax: cut <option> <file> Syntax: wc <options> <file>
Example: cut -d : -f 1 /etc/passwd Lines count: wc -l /var/log/syslog
NB: -d set the delimiter to use, and -f the field to keep NB: -l is for lines, -w for words and -m for characters
Use "man cut" to learn more about other options You can also use it after a pipe (to count lines from a grep
command for example)
. LSOF . WATCH
Stands for "LiSt Open Files", displays all Monitors a command output, by running it
currently opened files on your computer at each specified interval
Usage: lsof Basic usage: watch date
Specific time: watch -n10 date
. NETSTAT . DMESG
Monitors your network activity Shows a log file of every events happening
in the last boot sequence
Listening ports: netstat -l Usage: dmesg
Add the process ID: netstat -lp
Same thing in real-time: netstat -lpc
https://raspberrytips.com