Skip to content

Instantly share code, notes, and snippets.

@MaxR522
Last active November 21, 2021 08:12
Show Gist options
  • Save MaxR522/7640f35206fd6b4d0a0b2ea8adf47bcc to your computer and use it in GitHub Desktop.
Save MaxR522/7640f35206fd6b4d0a0b2ea8adf47bcc to your computer and use it in GitHub Desktop.
My notes when I learnt linux, I transformed it into cheat sheet

LINUX NOTES (using UBUNTU)

CLI for exploring file system

  • pwd Print Working Directory
  • cd Change Directory
  • ls list all the content of a directory

Administrative privileges in terminal

  • sudo super user do

  • using sudo: having super user privilege for this one command

  • sudo !! mean run the previous command as sudo

  • su switch user

  • sudo su change into the root account

Using package manager

  • apt-get program to install application

  • sudo apt-get install package-name to install a package available inside ubuntu repository

  • sudo apt-get remove package-name uninstall installed package

  • apt-cache search name for searching apt packages from the internet repositories on a Ubuntu or Debian based systems.

  • apt-cache policy name it will show whether the package is installed, which version is available from which repository and its priority.

  • sudo dpkg -i package-name install a local .deb package

package update

  • sudo apt-get update only updates the package list with the latest available versions, however, it does not install or upgrade the package.

  • sudo apt-get upgrade install the latest versions of all the previously installed packages on your system. This command only upgrades the packages which have a new release available as stated in the sources.list file in the “/etc/apt” folder.

  • sudo apt-get dist-upgrade also upgrades the packages. In addition to this, it also handles changing dependencies with the latest versions of the package. It intelligently resolves the conflict among package dependencies and tries to upgrade the most significant packages at the expense of less significant ones, if required. Unlike apt-get upgrade command, the apt-get dist-upgrade is proactive and it installs new packages or removes existing ones on its own in order to complete the upgrade.

Cleanup operation

  • sudo apt-get clean helps to clean out the cache once you have installed the packages using apt-get install command in your system. It removes the files that are no longer required but are still residing on your system and keeping the system space. It removes the retrieved .deb installer files and clears out the /var/cache/apt/archives leaving only the files in the lock and the partial directory.

  • sudo apt-get autoclean is similar to apt-get clean, but only for the packages that you have uninstalled or those with no newer versions available.

  • sudo apt-get autoremove is used to remove packages that were automatically installed to satisfy dependencies for some package and that are no longer needed.

Shortcuts:

  • sudo apt-get update && sudo apt-get dist-upgrade to upgrade all the packages in ubuntu
  • sudo apt-get autoremove && sudo apt-get clean to remove caches and obsolete packages & dependencies

File permission & ownership explained

ls -l output:

-rw-r--r-- 12 root users 12.0K Apr  8 20:51 filename.txt
|[-][-][-]-   [--] [---]
| |  |  | |     |   |
| |  |  | |     |   +----------------> 7. Group
| |  |  | |     +--------------------> 6. Owner
| |  |  | +--------------------------> 5. Alternate Access Method
| |  |  +----------------------------> 4. Others Permissions (3 chars)
| |  +-------------------------------> 3. Group Permissions (3 chars)
| +----------------------------------> 2. Owner Permissions (3 chars)
+------------------------------------> 1. File Type "-" for a regular file, "d" for directory, "I" for symbolic link

CHOWN

chown command allows you to change the user and/or group ownership of a given file, directory, or symbolic link

How to use chown

chown [OPTIONS] OWNER:GROUP FILE(s):

  • [OPTIONS] example of useful options: -R, --recursive: operate on files and directories recursively
  • OWNER the new owner of the file(s)
  • :GROUP the group ownership is changed to the given group.

example:

  • sudo chown -R mario:mario DIRECTORY-NAME change recursively the owner and group of this directory by mario

CHMOD

chmod command allows you to change the permissions on a file using either a symbolic or numeric mode or a reference file.

How to use chmod

chmod [OPTIONS] MODE FILE(s)

  • [OPTIONS] : -R: recursive, mean all file inside directory

  • MODE: different way to set permissions:

    • u: user
    • g: group
    • o: other
    • =: set the permission
    • r: read
    • w: write
    • x: execute

OR

  • 4 stands for "read",
  • 2 stands for "write",
  • 1 stands for "execute", and
  • 0 stands for "no permission."

example: rwx=4+2+1=7

example:

  • chmod u=rwx,g=rx,o=r myfile change the permission of myfile for owner, group and other
  • chmod 754 myfile same example using numeric permission

copy

cp [OPTIONS] SOURCE DESTINATION

  • [OPTIONS]:
    -i To get a confirmation prompt before overwriting the files
    -p to preserve the file mode, ownership , and timestamps
    -v prints what is being done
    -R or -r to copy folder and all file inside the folder recursively

  • SOURCE The file or the folder name that we want to copy. It can be one or multiple

  • DESTINATION destination path

Example:

  • cp file.txt /backup copy file.txt to /backup directory
  • cp -R Pictures Pictures_backup copying the directory Pictures to Pictures_backup

move

mv cp [OPTIONS] SOURCE DESTINATION

  • [OPTIONS]:
    -i To get a confirmation prompt before overwriting the files
    -p to preserve the file mode, ownership , and timestamps
    -v prints what is being done
    -R or -r to copy folder and all file inside the folder recursively
    -f Force overwriting
    -n never to overwrite any existing file
    -v Verbose output

  • SOURCE The file or the folder name that we want to copy. It can be one or multiple

  • DESTINATION destination path

example:

  • mv *.pdf ~/Documents move all pdf files from the current directory to the ~/Documents directory

remove

rm [OPTIONS] FILE_OR_DIRECTORY

  • [OPTIONS]:

-f force delete by ignoring prompt
-v verbose output
-d remove empty directory
-r remove directory and all file inside the directory recursively
-i prompt (y/n) the user for each given file before removing it

create directory

mkdir dirname make directory

Create file

touch filename.ext

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment