Linux Commands

Action Command
COPY FILE // Copy a file or directory.
cp /path/to/source /path/to/destination

// Copy a directory recursively.
cp -R /path/to/source /path/to/destination
MOVE FILE // Move single item.
mv /path/to/source /path/to/destination
// Move multiple items at once.
mv /path/to/source1 /path/to/source2 /path/to/destination

// Move to current directory.
mv /path/to/source .
RENAME FILE mv oldFileName newFileName
DELETE FILE rm my_file
DELETE DIRECTORY
// Prompts y/n.
rm -r mydir

// Move forcefully.
rm -rf mydir
DIRECTORY PERMISSION RECURSIVELY chmod 777 /path/to/directory

// Change permission recursively.
chmod -R 777 /path/to/directory
ZIP DIRECTORY
zip my_zip_file_name.zip /path/to/source -r
GET ZIP FILE INFORMATION
unzip -l my_file.zip

// Directories only.
unzip -l my_file.zip "*/"
UNZIP ZIP FILE
unzip my_file.zip

// Forcefully override files.
unzip -o my_file.zip

// Unzip to some directory.
unzip my_file.zip -d some_directory

// Unzip to some directory (Forcefully override).
unzip -o my_file.zip -d some_directory


// With other options.
-o overwrite files without prompting
-f freshen existing files, create none
-n never overwrite existing files
-q quiet mode (-qq => quieter)

EXTRACT TAR BALL
// Unzip .TAR.GZ file.
tar -xvzf my_file.tar.gz
VI EDITOR WRITE/QUIT
Hit the Esc key; that goes into command mode.
Then you can type..
:q To quit (short for :quit)
:q! To quit without saving (short for :quit!)
:w To write and not quit
:wq To write and quit
:wq! To write and quit even if file has only read permission (if file does not have write permission: force write)
:x To write and quit (shorter than :wq)
:qa To quit all (short for :quitall)
:234 To go to specific line number 234
:set number To show line numbers
:set no number To hide line numbers
:U To undo your current changes
:Ctrl+R To redo your current changes
LINUX VERSION
uname -a && cat /etc/*release
SHELL
// OhMyZsh SHELL
// To bring your terminal alive more developer oriented.

Github: Oh My Zsh (Shell)
// Or, Install directly
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Comments

  1. #TAR BALL
    .TAR
    Compress:
    tar -cvf filename.tar file1 dir1
    tar -cvf path/to/filename.tar path/to/file1 path/to/dir1

    tar -cvf filename.tar path/to/dir1/subdir1 // Archive will contain path/to/dir1/subdir1 path structure.
    tar -cvf filename.tar . // For compressing current directory, archive will contain ./ path structure.

    tar -C path/to/dir1 -cvf filename.tar subdir1 // Using the specified directory, here archive will contain subdir1 path structure.
    // Above is equivalent to- cd path/to/dir1 && tar -cvf filename.tar subdir1

    Extract:
    tar -C path/to/dir1 -xvf path/to/filename.tar // Extract to folder path/to/dir1
    Above is equivalent to- cd path/to/dir1 && tar -xvf path/to/filename.tar

    .TGZ/.TAR.GZ
    Compress:
    tar -cvzf filename.tgz file1 dir1
    Extract:
    tar -xvzf filename.tgz

    .TAR.BZ2
    Compress:
    tar -cvjf filename.tar.bz2 file1 dir1
    Extract:
    tar -xvjf filename.tar.bz2

    Options
    - c create archive
    - x extract archive
    - t list contents of archive
    - v verbose output
    - f archive file
    - z use gzip algorithm
    - j use bzip2 algorithm
    - C using the specified directory

    ReplyDelete
  2. CHMOD EXAMPLES:
    chmod [references][operators][modes] file...

    chmod u=rw example.txt
    chmod u-w example.txt

    • REFERENCE u owner | g group | o others | a all (same as ugo)
    • OPERATOR + adds | - removes | = exact modes.
    • MODE r read | w write/delete | x excute-file/search-directory


    ReplyDelete
    Replies
    1. Print environement variables
      printenv
      printenv | grep myvar

      Delete
  3. LINUX

    #Identify yourself
    whoami

    # List files with permissions, links, owner, group, size, time, name;
    ls -l

    # ID of the user
    # each user belongs to one or more groups but is only in one group at a time.
    # the gid entry shows the user's current group.
    id
    id

    # Group of the user
    groups
    groups

    # List of users
    Cat /etc/passwd

    #Change permission
    * chmod a+r foo - let all users read foo
    * chmod go-r foo - stop people in the file's group, and other users from reading the file
    * chmod u+x foo - make the file executable for the owner
    * chmod -R a+r foodir - let all users read foodir and all the files and directories under foodir.


    #change file group
    * chgrp partIa1 foo - puts the foo file into the partIa1 group
    * chgrp -R partIa1 foodir - puts the foodir directory and everything under it into the partIa1 group


    # See latest 5 commands executed
    history 5
    history 5 | awk '{$1="";print substr($0,2)}' //without line number
    history 5 | awk '{$1="";$2="# "$2;print substr($0,2)}' //without line number with prefix ‘# ’


    # run a command recursively
    watch -n 10 script.sh
    -d – highlights the changes in the command output.
    -n option, that specifies the interval with which the command will be executed
    If you wish to hide this header, you can use the -t option


    #
    tail -n 15 my.log

    ReplyDelete
  4. Add sudo users here-

    sudo visudo

    ReplyDelete
  5. TIPS
    - Only ‘cd’ will take to to home directory
    - Create new command alias
    - Create .bash_profile file, add “alias ll=‘ls -la’”, and at last refresh shell environment
    - nano .bash_profile
    - source ~/.bash_profile [OR] source ~/.zsh

    ReplyDelete
  6. HARD LINKS
    ln /path/to/source/file /path/to/destination/link
    ln /myfile.txt mylink

    Now, `ls -l` will look somewhat like below, where the link column value 2 shows number of links/copies of myfile.txt
    -rw-r--r--@ 2 drupal staff 363 Oct 21 22:07 mylink

    Hard links have actual file contents as they share the same inode value.
    Updating file content via hard link will update the source file contents as well.
    Removing hard link just reduce the link count by 1, without effecting original file or other hard links of the same file.
    If original file is removed, hard links retain the file content.
    Creating hard link for a directory is not allowed to avoid recursive loops.

    ln -h /Users/drupal/Drupal/drupal.org/modules/D8/track_progress/track_progress.info.yml yoyohl
    ln -h /Users/drupal/Drupal/drupal.org/modules/D8/track_progress/track_progress.info.yml yoyohl2
    -rw-r--r--@ 3 drupal staff 363 Oct 21 22:07 yoyohl
    -rw-r--r--@ 3 drupal staff 363 Oct 21 22:07 yoyohl2


    SOFT LINKS
    ln -s /path/to/source/file /path/to/destination/link
    ln -s /path/to/source/directory /path/to/destination
    ln -s /myfile.txt mylink

    Now, `ls -l` will look somewhat like below, with source path information
    lrwxr-xr-x 1 drupal staff 79 Oct 22 00:31 mylink -> /myfile.txt

    Soft or symbolic link is just a pointer to path of source file, similar to shortcut feature in Windows.
    Updating file content via soft link will update the source file contents as well.
    Removing soft link does not effect original file or other soft links of the same file.
    But if original file is removed, the soft link become dangling link or useless.

    Creating soft link for a directory is allowed.
    ln -s /path/to/source/directory /path/to/destination
    ln -s -f /Users/drupal/mydir /Users/beta
    Now, within/inside destination 'beta' directory you can access 'mydir' directory.

    Note: Relative path also works, in case it does not work- mention absolute path from root directory.


    REMOVE LINKS
    The rm command deletes files on Linux or Unix including a hard link.
    rm link_name

    ReplyDelete
  7. Understanding Shell Script's idiom: 2>&1
    https://www.brianstorti.com/understanding-shell-script-idiom-redirect/

    ReplyDelete
  8. VI Cheat Sheet https://vim.rtorr.com/

    ReplyDelete

Post a Comment

Drupal Contribution
Git Commands
RESTful Services
Lando Commands
Docker Commands
MySQL
Database Quick Code
Drush Commands
Drupal Console
PHP Quick Code
Drupal Quick Code
Composer Commands
Linux Commands
Linux Shell Scripting
Drupal Hooks
Twig Tricks
PHPUnit Test
PhpMyAdmin
Drupal Constants
CSS Clues
BLT Commands
Vagrant Commands
Localhost
127.0.0.1
Drupal Interview
Drupal Certifications
Concept & Definitions
Mac Tips
Windows Tips
Browser Tips

Best Practice

Use 'elseif' instead of 'else if'
#CodingTips

As of PHP 5.4 you can also use the short array syntax, which replaces array() with []
#CodingTips

Functions in general shall be named using snake_case(say, my_function()), and using camelCase(say, myFunction()) when declared within a plugin class
#CodingTips

Variables in general shall be named using snake_case(say, $my_variable), and using camelCase(say, $myVariable) when declared within a plugin class
#CodingTips

Manage automatically assigning of new permissions whenever a module is enabled here- admin/config/people/accounts
#ConfigurationTips

Manage source of Main-menu and User-menu links here- admin/structure/menu/settings
#ConfigurationTips

Helper function(s) shall be named prefixing an underscore(say, _my_helper_function()), which can prevent hooks from being called
#CodingTips

Ideally, configuring of 'Private file system path' at admin/config/media/file-system should be located outside of your Drupal root folder(say, ../my_private_files)
#ConfigurationTips

You should be aware that uploading files as 'Private file' will slow down the process of loading the files as Drupal has to be bootstrapped for every file that needs to be downloaded
#ConfigurationTips #BeAware

Code should always be pushed up(dev -> staging -> production) and databases should only be pushed down(production -> staging -> dev)
#DevelopmentTips

Get Raw SQL Query of drupal dynamic queries before executing it using $query->__toString();
#DebugTips

In VI-Editor, Press ESC key to come in command mode and for undo type :U and for redo type :Ctrl+R
#LinuxTips

Insert queries must always use a query builder object(layer of abstraction), allowing individual database drivers special handling for column values (if applicable), example case for LOB and BLOB fields.
#DatabaseQueryTips

Drupal uses the .inc extension to prevent files from being executed directly.
#DevelopmentTips

Popular Posts