โŒจ๏ธLinux Commands

DescriptionComands

System Info

uname -acat /etc/*-release

Update software

  • -y assumes yes to all prompts

sudo apt-get update -y

Install software (Debian/Ubuntu)

  • -y assumes 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

  • -l display as a list

  • -a show hidden files

  • -F show all directories with a / at the end, making them easier to see

  • -h show directory sizes in human readable format

  • -R list directories recursively

  • -t sort by modification date in ascending order, means the higher will be come first

  • -S sort by size

  • -i displays Inode information

  • -o shows owner of the files and directories but not the groups

  • *<STRING_TO_MATCH>* used to only display files and directories matching the string

    • This 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

  • -s creates soft link

ln <FILE1> <FILE2>

ln -s <FILE1> <FILE2>

Tree view of directories

  • -d only show directories

tree

tree -d /

Create, update the access, change, and modify times of files

  • -t allows 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

  • -rf Forcefully 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

  • -f forcefully remove a file

  • -r interactively 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

  • -n shows line numbers

cat

tac

chmod builder

Restrict file so only owner can read

  • -R to 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?

  • -l Print the number of lines.

  • -w Print the number of words.

  • -m Print the number of characters.

  • -c Print the number of bytes.

  • -L Print 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".

  • -N shows 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 15 or just -15 if 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 which then try whereis as it looks at a broader range of locations

which <PROGRAM_NAME>

Show home directory path for current user

echo $HOME

Change directory

  • cd ~ or just cd changes to Home directory

  • cd .. 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

  • pushd moves you to the directory specified in the argument

  • popd returns you to the most recent directory from the last pushd command (so similar to cd - but saves it, even if you go to multiple cd commands between)

  • dirs displays the current list of all directories saved with pushd

pushd <NEW_DIRECTORY>

popd

dirs

Find a file that contains specific text

  • -r or -R is recursive

  • -n is line number

  • -w stands for match the whole word

  • -l (lower-case L) can be added to just give the file name of matching files

  • -e is 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

  • -name looks for files with the following name

  • -iname ignores case of the name

  • -type restrict the results to files of a certain specified type

    • d for directory

    • l for symbolic link

    • f for a regular file

  • * wildcard matches any string of characters

  • ? wildcard matches any single character

  • -ls shows additional information about files found

  • -maxdepth limits results to a max depth specified

  • -size looks 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 find but 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

  • -h shows sizes in human readable format

  • -s summarize at the current depth

    • Shows size of all folders (and everything they contain) and files at the selected location

  • -a the all flag lists the sizes of all files and directories in the given file path. This option is often combined with the -h flag for ease of use. Notice the individual file sizes are listed with the directories.

  • -c Shows the total of all disk usage for the given file path as a new line at the bottom

  • * use for current location

du -sh <LOCATION>

du -sh <LOCATION>/*

du -sh *

du -shc *

Shows size of directories

  • -T shows type of directory

  • -h shows 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

sudo kill -15 <PID>

Terminal command history

  • -c delete 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

  • -h apparently this isn't actually needed

  • -r restarts instead of shutting down

  • now shuts 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 ~/.bashrc reloads 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

  • -P show progress

  • -a archive (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

  • -v verbose

  • -e specify 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 ubuntu@172.31.31.109:~/

Find

  • all files changed in the last 5 minutes

  • starting at the / root directory

  • to 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

  • -l flag to specify new name

usermod -l <NEW_NAME> <OLD_NAME>

Change groupname

  • -n flag 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