Skip to content

Latest commit

 

History

History
389 lines (278 loc) · 15.9 KB

linux.org

File metadata and controls

389 lines (278 loc) · 15.9 KB

Linux

File System

  • etc: system-wide configuration folder (eg. pacman mirrorlist)
  • bin: common binary files for applications (eg. ls)
  • boot: stores my bootloader
  • lib: shared libraries that applications use
  • mnt: where external drives are mounted (eg. usbstick)
  • opt: manually installed softwares
  • proc: information about system processes and resources, each folder is a process’ PID.
  • root: volatile storage
  • srv: service directory, where service data is stored
  • sys: it’s a way to interact with the hardware
  • tmp: where files are temporary stored in a session (eg. writing in a libreoffice file)
  • usr: user application’s space, where applications will be installed
  • var: contains files that are supposed to grow (eg. logs)
  • home: where you store your own personal files

Sha Bang

ExpressionDescription
#!locate an executable for the specified script
!!executes last command
!*executes command with ALL the args passed to the last command
!^executes command with the FIRST arg passed to the last command
!$executes command with the LAST arg passed to the last command
!?keyword?execute a command from history for the first pattern match
!nexecute a command from the nth position in the history
!<command>executes a command with the same flags as before

Permissions

Group permissions

GroupDescription
uuser (owner)
ggroup
oothers
aall

Operators

OperatorDescription
+add permission
-remove permission
=override permission

Octal notation

OctalDescription
0nothing
1execute
2write
31 + 2 (wx) write and execute
4read
54 + 1 (rx) read and execute
64 + 3 (rw) read and write
74 + 2 + 1 (rwx) read, write and execute

chmod (Change Mode)

CommandDescription
chmod -R <specifications> <directory>changing permissions of the contents of a directory recursively
chmod og=+radd read permission for the owner and group
chmod a +rwx fileallow all user to read, write and execute

chown (Change Ownership)

chown user1 filename changing ownership of a file to user1

su (switch user)

CommandDescription
su -invokes a login shell, reseting most environment variables
sunormal shell
sudo suswitch to root

sudo (superuser do)

sudo will run a specific command with root level of permission.

sudo !! (last command) for instance

Other commands

CommandDescription
cd -go to the previous working directory
ls \*Dpathname expansion, returns all files starting with D
ls exe\*all files ending with ‘exe’
less <file>view the contents of a file with a pager
head <file>first several lines of a file (pass -n <numer-of-lines> to see N number of lines)
tail <file>last several lines of a file (pass -n <numer-of-lines> to see N number of lines)
unameshow informations about the system (-a flag shows ALL info)
dusummarizes the disk usage of files
du -sh *summarizes every file in the current directory
dfdisk space of filesystem
psprocess status
ps -C <name>find the PID of a specific program
killall <p>terminates a process
kill <PID>terminates a process given the PID
whichsearches for executables specified by the environment variable PATH
timetime the execution of any program (eg. time node script.js)
watch -n Nruns a command infinitely each N seconds
aproposfind a command that I’m unaware of (eg. apropos “list dir”)
use in conjuntion with (whatis) command
whereissearches for executables, source files and manual pages
diffcompares files, and lists their difference
findfind files in directory (eg. find -name README.md)
man giteverydayuseful minimul set of commands for everyday git
curl wttr.inweather forecast
curl parrot.liverainbow parrot
sha256sumverify file checksum. (or sha1sum, md5sum depending on the hash)
curl cheat.sh/<command>/get a cheatsheet about a particular command

Fix typos

Instead of retyping a long list of arguments of a mispelled command, we can correct whatever typo we made with ^typo^correct

$ dc /tmp

$ ^dc^cd

File Compression

FlagsDescription
-c –createcreate a new archive
-x –extractextract files from an archive
-t –listlist the contents of an archive
-f –file=ARCHIVEuse archive file
-v –verboselist files being processed
-zcompress the archive with gzip (faster, but compresses less)
-jcompress the archive with bzip2 (slower, but compresses more)
–exclude <path>ignores subfolder

Syntax

CommandDescription
tar -cf ./archive.tar ./foldercreates an archive from a folder
tar -lf ./archive.tarlists the contents of the archive without extracting it
tar -xvf ./archive.tarextracts the archive
tar -cf archive.tar ./folder –exclude “folder/subfolder”ignores subfolder inside my-folder
tar -czvf archive.tar.gz ./foldercreates a gzip archive
tar -cjvf archive.tar.bz2 ./foldercreates a bzip2 archive
gunzip <filename>un-compresses files compressed by gzip
gzcat <filename>look at a gzipped file without having to gunzip it
gzip <filename>compresses a file to gzip format
bzip2 <filename>compresses a file to bzip2 format

Encryption

GPG (GNU Privacy Guard)

FlagDescription
--list-keyslist all keys keyring
--import <key-path>Add a pubkey into keyring
--delete-key <fingerprint>Del key in keyring

Import keys from keyserver

$ gpg --keyserver <server> --recv-keys <hex>

Where <server> can be “pgp.mit.edu” for instance.

Encrypt message

Having the receiver’s pkey:

$ gpg --output doc.gpg --encrypt --recipient mail@recipient doc

Decrypt message

$ gpg --output doc --decrypt doc.gg

Symmetric encryption

-c is for symmetric encryption (single pkey for encrypting/decrypting).

$ gpg -c --no-sym-key-cache --cipher-algo AES256 <file>

Shell expansion

We can use shell expansion to:

  • rename and backup operations
  • pattern matching
  • match any string

The * Wildcard

  • The * is a greedy operator that matches any string, incuding the null string.

$ echo file* returns file.log file.tex file.txt file.exe $ echo *.tex returns file.tex

The ? Wildcard

  • The ? matches a single character.

$ echo file?.txt returns file1.txt file2.txt file3.txt ...

Inverting Sets

  • The ^ character represents not.
    • [abc] means either a, b or c
    • [^abc] any character that is not a, b or c

Brace Expansion

To backup settings.conf to settings.conf.bak:

cp settings.conf{,.bak}

To revert the file from settings.conf.bak to settings.conf:

mv settings.conf{.bak,}

Other uses:

echo foo{1,2,3}.txt outputs foo1.txt, foo2.txt, foo3.txt

echo file-{a..b}.txt outputs file-a.txt file-b.txt file-c.txt file-d.txt

mv program.{c,exe} bin/

IO Redirections

Output redirections

The output from a command normally intended for stdout can be easily diverted to a file.

$ who > users

$ cat users
nexi     tty01    Oct 22 08:58
ai       tty02    Oct 22 13:58

The > operator will always override the contents of the file.

We can use the >> operator to append the output in a file with existing info:

$ echo lorem ipsum >> users

$ cat users
nexi     tty01    Oct 22 08:58
ai       tty02    Oct 22 13:58
lorem ipsum

Input redirection

Commands that receive input from stdin can have their input redirected from a file.

$ wc -l < users
3

Here document

A “here document” redirects input into an interactive shell script or program.

command << EOF
    document
EOF

The >> command is an instruction to read input until it finds a line containing the delimeted (EOF in this case)

$ wc -l <<EOF
  lorem ipsum
  dolor sit
  amet
EOF
3

Chaining Operators

  • & (Ampersand Operator)

    run one or more jobs in the background,

    EXAMPLE: ping www.google.com & apt-get update & apt-get upgrade &

    • Ctrl-z pauses the job
    • jobs prints all the jobs
  • ; (Semi-colon operator)

    run several commands at once sequentially, disregarding the exit status of the preceding command

    EXAMPLE: apt-get update ; apt-get upgrade ; mkdir test

  • && (AND operator)

    executes a command IF the exit status of the preceding command is 0

    EXAMPLE: ping www.google.com && links www.google.com (checking the connection before using links command)

  • || (OR operator)

    much like an ‘else’ statement, allows to execute the second command only if the execution of the first fails (i.e., the exit status is 1)

    EXAMPLE: apt-get update || links www.google.com

  • ! (NOT operator)

    much like an ‘except’ statement, this command will execute all except the condition provided

    EXAMPLE: rm -r !(*.html) removes all files in a folder except .html files

  • | (PIPE operator)

    passes the output of the first command to the second one

    EXAMPLE: ls -l | less

  • {} (Command Combination operator)

    combine two or more commands

    [ -d Folder] || { echo creating Folder; mkdir Folder; } && echo Folder exists.

RegEx Tools

  • grep (Globally search for a Regular Expression and Print) for searching stuff in files, or any STDOUT (eg. ‘ls’ command)

    EXAMPLE: ls | grep "\.exe$"

  • sed (stream editor) for substitituing, deleting or filtering text on a stream

    EXAMPLE: sed 's/regexp/replacement/g' file > output

    -r will extend the Regex portability (POSIX)

  • xargs (command args) pass any command to it and it will execute it on a stream.

    EXAMPLE: find | grep "\.exe$" | xargs ls -lh

Bash

keybindingdescription
ctrl-udelete from cursor position to beginning of line
ctrl-kdelete from cursor position to end of line
ctrl-lclear screen
ctrl-wdelete last word
ctrl-rsearch through command history
alt-.cycles through last arguments
alt-*expands glob

Cleanup

Steps to free disk space on your Linux machine:

  1. Check directories disk space: cd / && sudo du -h --max-depth=1
  2. Delete pacman cache:
    • sudo paccache -r keep at least 3 caches
    • sudo paccache -rk 1 keep only 1 pkg cache