Linux command line cheat sheet
Last few month, I’ve been dealing with Linux servers. I had experience with Linux, I even was using Linux distributives as a primary operation system for a few year in the past. But I feel the lack of skill of administration. Here, I’m going to collect all basic commands and command combinations which I use in the day-to-day routine. It’s really difficult to remember all the commands, parameters and keys when you don’t do it every day like a full-time system administrator. This article may be used as a Linux commands cheat sheet.
General shortcuts
- Ctrl + r commands execution history search.
- Ctrl + x Ctrl + e opens the current command in editor.
- Ctrl + a moves cursor to the beginning, Ctrl + e to the end.
- Alt + ◀ moves cursor backward one word.
- Ctrl + u deletes to the beginning of line.
- Ctrl + l clears the screen
- Ctrl + D =
exit
General tips
cd -- changes directory to the previous onesudo !!- runs previous command with sudo![command]- runs previous command that starts with [command][cmd1] ; [cmd2]- executes both commands ignoring result of [cmd1][cmd1] && [cmd2]executes [cmd2] if [cmd1] is executed without errors[cmd1] || [cmd2]executes [cmd2] if [cmd1] is executed with errors
Output manipulations
grep [keyword]- leaves only lines contain keyword,grep -E "[regex]"works with regular expressionsawk '{print $1}'- prints the first column of the outputawk '{[program]}'- executes Perl-like scriptsort -bf- sorts output line by line.b- ignore leading blanks,f- ignore case,h- human sort,r- reversetail [file]- shows the last part of a file.f- monitors a file for updates,-n [N],-[N]- prints last N lineshead- opposite totail
System information
lscpu- system specs.lsb_release -a- Ubuntu version informationdf -h- overall information about filesystemsdu -hs [path] | sort -hr | head- top 10 biggest directories/files within the path sorted by directory/file sizes- displays only total for each directoryfree -m- current memory usage.mshow megabytes,h- human readable.top- displays processes. Sort by: %MEM - Shift + m, %CPU - Shift + p. Shift + e changes units. c toggles between command line and name for processestop -p [PID]- displays information for the specific PID/PIDsps aux- static version of thetop
Archiving
tar -zxvf file.tar.gz- extracts tar.gz into current directory.z- gzip,x- extract,v- verbose output,f- work with file (not stdout/stdin)tar -cvzf test.tar.gz ./*- creates tar.gz.c- create a file
SSH
ssh-keygen -t rsa- generates key paircat ~/.ssh/id_rsa.pub | ssh [user@host] "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"- adds public key to a remote machine. A proper way for linux isssh-copy-id, but it doesn’t present on MacOSscp [name]@[domain]:[remote path] [local path]orscp [local path] [name]@[domain]:[remote path]- copies file from remote machine to local or vice versa
Network
dig [domain]- dns related informationnslookup [domain]- translates a domain name to an IP address,nslookup [ip]- vice versatelnet [host] [port]- connects to host:port by TCPiptables -L -n -v- displays status of the firewall.L- list rules,v- verbose,n- shows ip addresses in numeric formatnmap -O [host]- shows all open ports on the host
Netstat
Netstat is a command line utility that can show information about network connections in the system.
netstat -plunt- show processes that are using ports on the server.u- udp,t- tcp,p- PID and program name,l- only listening,n- numerical addressesnetstat -r- shows routing table. See this article to understand the output.netstat -s- statistics
Curl
curl -O [url]- downloads file to the current directorycurl [url] -F file=@[file path]- uploads file from [file path]curl [url] -X [method] -H [header] -d [data] | python -m json.tool- sends [method] (GET/POST/PUT…) reqest to [url] with headers and [data] body.python -m json.toolformats json (useful tip:alias prettyjson='python -m json.tool')
Misc
Mysql
mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [db.sql.gz]- dumps database to a gzip archivegunzip < db.sql.gz | mysql -u [uname] -p[pass] db- restores database dump from the archive
APT Package management
apt-cache search [keyword]- searches packageapt-cache show [package name]- retrieves a package description
Docker
docker stop $(docker ps -a -q) & docker rm $(docker ps -a -q)- stops and removes all containers, sometimes may require-fto force deletion.docker volume rm $(docker volume ls -qf dangling=true)- removes unused volumesdocker rmi $(docker images | grep "^<none>" | awk '{print $3}')- removes untagged images