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