# Linux Commands

<table data-full-width="true"><thead><tr><th width="499">Description</th><th>Comands</th></tr></thead><tbody><tr><td>System Info</td><td><code>uname -acat /etc/*-release</code></td></tr><tr><td><p>Update software</p><ul><li><code>-y</code> assumes yes to all prompts</li></ul></td><td><code>sudo apt-get update -y</code></td></tr><tr><td><p>Install software (Debian/Ubuntu)</p><ul><li><code>-y</code> assumes yes to all prompts</li></ul></td><td><code>sudo apt-get install -y &#x3C;PACKAGE_NAME></code></td></tr><tr><td>Uninstall software</td><td><code>sudo apt-get --purge remove &#x3C;PACKAGE_NAME></code></td></tr><tr><td>Show current file path</td><td><code>pwd</code></td></tr><tr><td><p>Show all files in current directory</p><ul><li><code>-l</code> display as a list</li><li><code>-a</code> show hidden files</li><li><code>-F</code> show all directories with a / at the end, making them easier to see</li><li><code>-h</code> show directory sizes in human readable format</li><li><code>-R</code> list directories recursively</li><li><code>-t</code> sort by modification date in ascending order, means the higher will be come first</li><li><code>-S</code> sort by size</li><li><code>-i</code> displays Inode information</li><li><code>-o</code> shows owner of the files and directories but not the groups</li><li><p><code>*&#x3C;STRING_TO_MATCH>*</code> used to only display files and directories matching the string</p><ul><li>This doesn't see to work correctly when used with <code>-a</code></li></ul></li></ul></td><td><p><code>ls -l</code></p><p><code>ls -l *&#x3C;STRING_TO_MATCH>*</code></p></td></tr><tr><td>Show hidden files</td><td><code>ls -la</code></td></tr><tr><td><p>Create hard and soft links to files</p><ul><li><code>-s</code> creates soft link</li></ul></td><td><p><code>ln &#x3C;FILE1> &#x3C;FILE2></code></p><p><code>ln -s &#x3C;FILE1> &#x3C;FILE2></code></p></td></tr><tr><td><p>Tree view of directories</p><ul><li><code>-d</code> only show directories</li></ul></td><td><p><code>tree</code></p><p><code>tree -d /</code></p></td></tr><tr><td><p>Create, update the access, change, and modify times of files</p><ul><li><code>-t</code> allows you to set the date and timestamp of the file to a specific value e.g. 12091600</li></ul></td><td><p><code>touch</code></p><p><code>touch -t &#x3C;DATE> &#x3C;FILENAME></code></p></td></tr><tr><td><p>Create and remove directories</p><ul><li><code>-rf</code> Forcefully remove a directory that contains files recursively</li></ul></td><td><p><code>mkdir &#x3C;NEW_DIRECTORY></code></p><p><code>rmdir &#x3C;DIRECTORY_TO_DELETE></code></p></td></tr><tr><td><p>Moving or Renaming a file</p><ul><li>Simply rename a file</li><li>Move a file to another location, while possibly changing its name at the same time.</li></ul></td><td><code>mv</code></td></tr><tr><td><p>Removing a file</p><ul><li><code>-f</code> forcefully remove a file</li><li><code>-r</code> interactively remove a file (ask before removing)</li></ul></td><td><code>rm</code></td></tr><tr><td><p>Viewing files (or combine files) that are not very long</p><ul><li>It does not provide any scroll-back.</li><li>View backwards, starting at the last line with <code>tac</code></li><li><code>-n</code> shows line numbers</li></ul></td><td><p><code>cat</code></p><p><code>tac</code></p></td></tr><tr><td>chmod builder</td><td><a href="https://chmodcommand.com/">https://chmodcommand.com/</a></td></tr><tr><td><p>Restrict file so only owner can read</p><ul><li><code>-R</code> to recursively change permissions for all folders and files below</li></ul></td><td><p><code>chmod 440 &#x3C;FILE/FOLDER></code></p><p><code>chmod -R 400 &#x3C;FILE/FOLDER></code></p></td></tr><tr><td>Make file executable</td><td><code>chmod u+x &#x3C;FILE></code></td></tr><tr><td><p>Count words in a file</p><ul><li>Default: lines, words, characters/bytes?</li><li><code>-l</code> Print the number of lines.</li><li><code>-w</code> Print the number of words.</li><li><code>-m</code> Print the number of characters.</li><li><code>-c</code> Print the number of bytes.</li><li><code>-L</code> Print the length of the longest line</li></ul></td><td><code>wc</code></td></tr><tr><td><p>View larger files with a paging program</p><ul><li>It pauses at each screen full of text, provides scroll-back capabilities, and lets you search and navigate within the file.</li><li>Use spacebar to paginate</li><li>Use <code>/</code> to search for a pattern in the forward direction and <code>?</code> for a pattern in the backward direction. An older program named more is still used, but has fewer capabilities: "less is more".</li><li><code>-N</code> shows line numbers</li></ul></td><td><code>less</code></td></tr><tr><td><p>Show the first few lines of a file</p><ul><li>Same modifiers as tails</li></ul></td><td><code>head</code></td></tr><tr><td><p>Shows the last few lines of a file</p><ul><li>Print the last 10 lines of a file by default</li><li>Change the number of lines by doing <code>-n 15</code> or just <code>-15</code> if you wanted to look at the last 15 lines instead of the default.</li></ul></td><td><p><code>tail</code></p><p><code>tail 15</code></p><p><code>tail -n 15</code></p></td></tr><tr><td>Used to view documentation</td><td><code>man</code></td></tr><tr><td><p>Find where a program resides on the filesystem</p><ul><li>If a program isn't found with <code>which</code> then try <code>whereis</code> as it looks at a broader range of locations</li></ul></td><td><code>which &#x3C;PROGRAM_NAME></code></td></tr><tr><td>Show home directory path for current user</td><td><code>echo $HOME</code></td></tr><tr><td><p>Change directory</p><ul><li><code>cd ~</code> or just <code>cd</code> changes to Home directory</li><li><code>cd ..</code> changes to parent directory (..)</li><li><code>cd -</code> changes to previous directory (- (minus))</li></ul></td><td><p><code>cd</code></p><p><code>cd ~</code></p><p><code>cd ..</code></p><p><code>cd -</code></p></td></tr><tr><td><p>Move to a new directory but save the current directory for quick return</p><ul><li><code>pushd</code> moves you to the directory specified in the argument</li><li><code>popd</code> returns you to the most recent directory from the last <code>pushd</code> command (so similar to <code>cd -</code> but saves it, even if you go to multiple <code>cd</code> commands between)</li><li><code>dirs</code> displays the current list of all directories saved with <code>pushd</code></li></ul></td><td><p><code>pushd &#x3C;NEW_DIRECTORY></code></p><p><code>popd</code></p><p><code>dirs</code></p></td></tr><tr><td><p>Find a file that contains specific text</p><ul><li><code>-r</code> or <code>-R</code> is recursive</li><li><code>-n</code> is line number</li><li><code>-w</code> stands for match the whole word</li><li><code>-l</code> (lower-case L) can be added to just give the file name of matching files</li><li><code>-e</code> is the pattern used during the search</li></ul></td><td><code>grep -rnw '&#x3C;PATH>/&#x3C;TO>/&#x3C;SOMEWHERE>' -e '&#x3C;TEXT>'</code></td></tr><tr><td><p>Count number of files in this directory and all sub-directories</p><ul><li><code>.</code> look in current directory</li><li><code>-name</code> looks for files with the following name</li><li><code>-iname</code> ignores case of the name</li><li><p><code>-type</code> restrict the results to files of a certain specified type</p><ul><li><code>d</code> for directory</li><li><code>l</code> for symbolic link</li><li><code>f</code> for a regular file</li></ul></li><li><code>*</code> wildcard matches any string of characters</li><li><code>?</code> wildcard matches any single character</li><li><code>-ls</code> shows additional information about files found</li><li><code>-maxdepth</code> limits results to a max depth specified</li><li><code>-size</code> looks for files with specified size</li></ul></td><td><p><code>find</code></p><p><code>find . -name "&#x3C;FILE/DIRECTORY_NAME>"</code></p><p><code>find . -name "test*" -ls</code></p><p><code>find . -type f -name "test*" -ls</code></p><p><code>find . -type d -maxdepth 1</code></p><p><code>find . -size 0</code></p></td></tr><tr><td><p>Search all files and directories on the sys</p><ul><li>Uses a local DB to index all files, so it can be faster than <code>find</code> but also out of date (updates once per day)</li><li>DB can be manually updated using <code>sudo updatedb</code></li></ul></td><td><code>locate</code></td></tr><tr><td>Search for a specific package</td><td><code>sudo dpkg --list | grep &#x3C;PACKAGE_NAME></code></td></tr><tr><td><p>Total disk space used (disk usage) by current directory and all sub-directories</p><ul><li><code>-h</code> shows sizes in human readable format</li><li><p><code>-s</code> summarize at the current depth</p><ul><li>Shows size of all folders (and everything they contain) and files at the selected location</li></ul></li><li><code>-a</code> the all flag lists the sizes of all files and directories in the given file path. This option is often combined with the <code>-h</code> flag for ease of use. Notice the individual file sizes are listed with the directories.</li><li><code>-c</code> Shows the total of all disk usage for the given file path as a new line at the bottom</li><li><code>*</code> use for current location</li><li>Useful notes on arguments: <a href="https://www.redhat.com/sysadmin/du-command-options">https://www.redhat.com/sysadmin/du-command-options</a></li></ul></td><td><p><code>du -sh &#x3C;LOCATION></code></p><p><code>du -sh &#x3C;LOCATION>/*</code></p><p><code>du -sh *</code></p><p><code>du -shc *</code></p></td></tr><tr><td><p>Shows size of directories</p><ul><li><code>-T</code> shows type of directory</li><li><code>-h</code> shows sizes in human readable format</li></ul></td><td><code>df -Th</code></td></tr><tr><td>See if a service is running</td><td><p><code>service &#x3C;service-name> status</code></p><p><code>service --status-all</code></p></td></tr><tr><td><p>Search terminal history</p><ul><li>Keep pressing to search through previous commands that match</li></ul></td><td><code>ctrl-r</code></td></tr><tr><td><p>Shows running processes and Env Args</p><ul><li>I think the ww does line wrapping</li></ul></td><td><code>ps -auxww</code></td></tr><tr><td><p>Unrar</p><ul><li>unrars to the directory you are currently in</li></ul></td><td><code>sudo unrar x &#x3C;FILENAME>.rar</code></td></tr><tr><td><p>Run last command</p><ul><li>So to run the last command as sudo if you forget to add it to the start: sudo !!</li></ul></td><td><code>!!</code></td></tr><tr><td>Open a file using sudo in a GUI application on Mac</td><td><code>sudo open -a &#x3C;APPLICATION_NAME> /file/to/open</code></td></tr><tr><td>Show IP</td><td><code>ifconfig</code></td></tr><tr><td><p>Run command as a different user:</p><ul><li>sudo = be root so I can do anything.  </li><li>su means "now be another user”</li><li>When you run su as root, you don't need to provide the users' password</li><li>So "sudo su" means "run command as other user, and only have to enter root password"</li><li>Command to run = -c</li></ul></td><td><code>sudo su postgres -c "psql"</code></td></tr><tr><td><p>How to Kill Processes</p><ul><li><a href="https://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/">https://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/</a></li><li>-15: the default way to kill a process</li><li>-9: Last resort way to kill a process </li></ul></td><td><code>sudo kill -15 &#x3C;PID></code></td></tr><tr><td><p>Terminal command history</p><ul><li><code>-c</code> delete current session history</li></ul></td><td><p><code>history</code></p><p><code>history -c</code></p></td></tr><tr><td>Delete all terminal command history</td><td><code>sudo rm ~/.bash_history</code></td></tr><tr><td>Diff</td><td><code>diff -arq /Diretory1 /Directory2</code></td></tr><tr><td>Find a file by name</td><td><code>sudo find / -type f -name “pg_hba.conf"</code></td></tr><tr><td>Find a directory by name</td><td><code>find / -type d -name "post*"</code></td></tr><tr><td><p>Change user</p><ul><li>Leave empty to change to root user</li></ul></td><td><code>su</code></td></tr><tr><td>On a clean install, grant current user sudo powers</td><td><p><code>su</code></p><p><code>&#x3C;ENTER ROOT PASSWORD></code></p><p><code>echo "&#x3C;USERNAME> ALL=(ALL) ALL" ></code></p><p><code>/etc/sudoers.d/&#x3C;USERNAME></code></p><p><code>chmod 440 /etc/sudoers.d/&#x3C;USERNAME></code></p></td></tr><tr><td><p>Shutdown / restart</p><ul><li>Requires sudo</li><li><code>-h</code> apparently this isn't actually needed</li><li><code>-r</code> restarts instead of shutting down</li><li><code>now</code> shuts it down immediately otherwise it will default to a 60 second delay</li><li><a href="https://www.ionos.co.uk/digitalguide/server/configuration/linux-shutdown-command/">All arguments</a></li></ul></td><td><p><code>sudo shutdown -r now</code></p><p><code>sudo shutdown -h 10:00 "Shutting down for scheduled maintenance."</code><br></p></td></tr><tr><td>Use a shell command in-line</td><td><code>ls -l $(dpkg --listfiles ftp)</code></td></tr><tr><td>Open an image from the command line</td><td><code>eog &#x3C;IMAGE_FILE></code></td></tr><tr><td><p>Alias a command persistently directly from the command line and reload terminal</p><ul><li>This is faster than opening the ~/.bashrc file manually and adding the line directly</li><li><code>source ~/.bashrc</code> reloads the new parameters into the current terminal session</li></ul></td><td><p><code>echo "alias ls='ls -la'" >> ~/.bashrc</code></p><p><code>source ~/.bashrc</code></p></td></tr><tr><td>Check public facing ip</td><td><code>curl https://ipinfo.io/ip</code></td></tr><tr><td>Show interface configuration</td><td><code>ifconfig</code></td></tr><tr><td>Kernel IP routing table</td><td><code>netstat -rn</code></td></tr><tr><td>List all ip routing tables</td><td><code>ip route show table all | grep -Po 'table \K[^\s]+' | sort -u</code></td></tr><tr><td>Show content of ip routing table</td><td><code>ip route list table &#x3C;TABLE_NAME></code></td></tr><tr><td>Delete ip routing table</td><td><code>sudo ip route flush table &#x3C;TABLE_NAME></code></td></tr><tr><td>Disable and uninstall SSH</td><td><p><code>sudo systemctl stop ssh</code></p><p><code>sudo apt-get --purge remove ssh</code></p></td></tr><tr><td><p>rsync large files and directories</p><ul><li><code>-P</code> show progress</li><li><p><code>-a</code> archive (good for large files or unstable connections)</p><ul><li>Keeps a record of both sides of transfer to make sure it is successful even if it drops out temporarily in the middle</li></ul></li><li><code>-v</code> verbose</li><li><code>-e</code> specify remote shell to use</li></ul></td><td><p><code>sudo rsync -Pav -e "ssh -i $HOME/Keys/&#x3C;SSH_KEYS>.pem" &#x3C;SOURCE_LOCATION> &#x3C;DEST_USER>@&#x3C;DEST_IP>:&#x3C;DEST_LOCATION></code></p><p><br><code>sudo rsync -Pav -e "ssh -i $HOME/keys/Dev-Validator-Keys.pem" ~/TestDir ubuntu@172.31.31.109:~/</code></p></td></tr><tr><td><p>Find</p><ul><li>all files changed in the last 5 minutes</li><li>starting at the <code>/</code> root directory</li><li>to a maxdepth of 10</li><li>not including the paths <code>/pro/*</code> and <code>/sys*/</code></li><li>Show updated time next to each file and folder</li></ul></td><td><code>sudo find / -maxdepth 10 -not -path "/proc/*" -and -not -path "/sys/*" -mmin -5 -printf "%p %TY-%Tm-%Td %TH:%TM \n"</code></td></tr><tr><td><p>Change username</p><ul><li><code>-l</code> flag to specify new name</li></ul></td><td><code>usermod -l &#x3C;NEW_NAME> &#x3C;OLD_NAME></code></td></tr><tr><td><p>Change groupname</p><ul><li><code>-n</code> flag to specify new name</li></ul></td><td><code>groupmod -n &#x3C;NEW_NAME> &#x3C;OLD_NAME></code></td></tr><tr><td>UFW - Show all rules even when UFW is disabled</td><td><code>sudo ufw show added</code></td></tr><tr><td>Compress <code>tar.gz</code></td><td><code>tar -czvf archive.tar.gz stuff</code></td></tr><tr><td>Extract <code>tar.gz</code></td><td><code>tar -xzvf archive.tar.gz</code></td></tr><tr><td>Compress <code>tar.xz</code></td><td><code>tar -cJf archive.tar.xz stuff</code></td></tr><tr><td>Extract <code>tar.xz</code></td><td><code>tar -xvf archive.tar.xz</code></td></tr><tr><td>Show all available kernels</td><td><code>dpkg --list | grep linux-image</code></td></tr><tr><td>Show current kernel</td><td><code>uname -r</code></td></tr><tr><td>Remove kernel version</td><td><code>sudo apt remove -y linux-image-</code></td></tr></tbody></table>
