Which shell I am using?

Starting with macOS Catalina (10.15), Apple set the default shell to the Z shell (zsh). In previous macOS versions, the default was Bash.

  • In zsh, the configuration file is ~/.zshrc.

  • In bash, it’s ~/.bash_profile.

Every time you make changes with the configuration file

  • run source ~/.bash_profile if you have bash. [✘]

  • run source ~/.zshrc if the shell is zsh. [✔]

Get help

man cmd get manual page for cmd. E.g., man ls will display detailed info about the ls cmd, including its usage, options, and examples.

Once you’ve accessed a manual page, you can navigate through it using various commands. The common navigation keys include:

  • Spacebar: Move forward one page.
  • Enter: Move forward one line.
  • B: Move backward one page.
  • Q: Quit the manual viewer.

Error: Cannot find man command.

Error Message looks like:

This system has been minimized by removing packages and content that are not required on a system that users do not log into.

To restore this content, including manpages, you can run the ‘unminimize’ command. You will still need to ensure the ‘man-db’ package is installed.

Cause: Typically man pages for Ubuntu commands not installed on a minimal cloud server or container-based images. To save hard disk space and to keep container image rebuild time smaller, man pages not installed.

Fix: install man pages.

Steps:

  1. Run the following commands as the root user using the sudo command or su command.

    sudo -i
    ## OR ##
    su -
    

    Now you become the root.

  2. Search for Ubuntu man pages

    # search
    apt search manpages
    # see what the page does
    apt info man-db
    
  3. Install man pages on server

    First update package index and then install manpages-posix package for all missing commands:

    sudo apt update
    sudo apt install man-db manpages-posix
    

    Now you have installed man-db package.

  4. Install man pages for software development

    sudo apt install manpages-dev manpages-posix-dev
    
  5. Then run unminimize. That may takes a while.

    Now you can run man ls to check the syntax for ls command.

Reference: https://www.cyberciti.biz/faq/how-to-add-install-man-pages-on-ubuntu-linux/


which [flags] program locate a program file executed by the shell.

  • -a List all instances of executables found (instead of just the first one of each).

Specify flags/options for cmds: use a single dash (-) or a double dash (--).

  • - can be followed by any number of single-character flags.

  • -- can be followed only by a single, multi-character option.

    Using - is more compact, but it gets even MORE compact when you want to specify multiple flags.

    Consider:

    $ ls -l --size --human-readable --group-directories-first
    $ ls -lshg
    

    While the two commands listed above are identical, the second one is clearly much quicker to type. If you use the wrong type of hyphen, for example specifying ls -size instead of ls --size, would be interpreted by the shell as equivalent to ls -s -i -z -e. As it happens, the -s options is the same as --size, but because there are no z or e options for the ls command, they are simply ignored.

echo $PATH print PATH environment variable.

  • When you type a command, the shell looks for executable files in the directories specified by your PATH.
    • The directories are recursive, meaning if the parent folder is included, every file/folder inside is also included.
    • The order stands for precedence.
  • If you try to run a file or command that isn’t in one of the directories in your path, you’ll receive an error that says the “Command not found”.
  • It’s a list of directory paths, separated by colons (:). For example, a default $PATH looks like /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin.

Add a new directory to the PATH.

export PATH=/Users/menghan/anaconda3:$PATH The new $PATH variable will be exported to the environment as a combination of the previous $PATH plus a new directory.

  • The new directory comes first; it will take precedence over any directories present in the previous $PATH.
  • A : colon character separates the new directory path from the existing directories in the previous $PATH.

Set the $PATH in script

Set the $PATH environment variable in the ~/.zprofile (Zsh Profile) file. The tilde ~/ is a Unix abbreviation for your home directory. That is, the .zprofile file belongs in your home directory.

These are the two zsh shell initialization files that are commonly used for configuration:

  • ` ~/.zshrc` (Zsh Run Control): This file is best for adjusting the appearance and behavior of the shell, such as setting command aliases and adjusting the shell prompt.
  • ~/.zprofile (Zsh Profile): This file is ideal for commands that should be executed only when the terminal window is opened, such as setting the $PATH environment variable.

The article .zshrc or .zprofile explains the differences.

By default, the ~/.zprofile configuration file does not exist for a user on a new Mac. You’ll need to manually create the file in your home directory to properly configure your development environment.

You can create a new file from the command line with the touch command:

$ touch ~/.zprofile

After you create the file, you can edit it.

You can use TextEdit, the default macOS graphical text editor, to edit the shell configuration files. You can open a file in TextEdit from the Mac Terminal:

$ open -e ~/.zprofile

You also can use the command line editors nano or vim to edit the shell configuration files. See Shell Configuration for more about editing shell configuration files.

For the changes in ~/.zprofile to take effect, you have to log out and log in again. Alternatively, you can use the source command to reset the shell environment:

$ source ~/.zprofile

The source command reads and executes a shell script file, in this case resetting the shell environment with your new $PATH setting.

Install tar.gz on macOS

  1. Finder

    https://stackoverflow.com/questions/62988753/install-tar-gz-on-mac

    1. Double click it the tar.gz file open it

    2. One of the new files might be a unix executable that looks like this:

      enter image description here

  2. Command line

    Download the desired .tar.gz or (.tar.bz2) file. Open Terminal, extract the .tar.gz or (.tar.bz2) file with the following commands:

    tar xvzf PACKAGENAME.tar.gz 
    tar xvjf PACKAGENAME.tar.bz2 
    

    Navigate to the extracted folder using cd command

    cd PACKAGENAME 
    

    Now run the following command to install the tarball

     ./configure 
     make 
     sudo make install
    

tar [options] [archive-file] [file or directory to be archived] options:

  • x extract the archive;
  • v print its action on console;
  • f tell tar which file to perform the action on;
  • z compress the tar file using gzip
  • j compress the tar file using bzip2

tar xvjf PACKAGENAME.tar.gz -C / -C means changing directory.

man tar to show help page for tar.

echo $PATH check $PATH variable.

  • stored in ~/.bash_profile.

  • in R

    Sys.getenv("PATH")
    

touch ~/.R/Makevars create a file ~/.R/Makevars

source $SHELL restart your session to enable new settings.

File Management

cp [option] <source> <destination> copy the <source> file to the <destination> file or directory. If multiple <source> is provided, then copy all files to the <destination> directory. Options:

  • -i interactive copying. Warn the user before overwriting the desitnation file.
  • -r or -R recursive copying the entire directory structure.

rm DeleteMe.odt DeleteMe2.odt delete multiple files.

  • If there are special characters in file names, simply add quotes to file names. E.g.,

    rm '4-way mini-roundabout.png' 'DTC.png' 'Heyford.png' 'image-20220824132439609.png' 'image-20220824133418559.png'

  • If the file doesn’t exit, there will be warning message No such file or directory.

# R codes to generate the shell cmd to delete files

# delete repetitive files
post_image <- list.files("/Users/menghan/Documents/PhD Courses/Econ-Study/images")
post_image

google_drive <- list.files("/Users/menghan/Library/CloudStorage/GoogleDrive-my1396@nyu.edu/My Drive/Typora_images")
google_drive

mask <- google_drive %in% post_image
google_drive[!mask]

file_delete <- google_drive[mask]
file_delete <- as_tibble_col(file_delete, column_name = "name")
# add quotes to file names to escape special characters
file_delete <- file_delete %>% 
  mutate(new_name = paste0("'", name, "'") 
  )
file_delete
nrow(file_delete)

# cmd to delete files
paste0("rm ", paste(file_delete$new_name[1:5], collapse = " "))

length(google_drive)
length(post_image)
length(list.files("/Users/menghan/Library/CloudStorage/GoogleDrive-my1396@nyu.edu/My Drive/Typora_images"))

Hardlink vs symbolic link

  • Once a hard link has been made the link is to the inode. Deleting, renaming, or moving the original file will not affect the hard link as it links to the underlying inode. Any changes to the data on the inode is reflected in all files that refer to that inode.

  • Hardlinks can only refer to files within the same volume otherwise symbolic links will be needed.

  • Symbolic links point to the original file, changing the symboliclink should change the original file.

    What will break a symbolic link is when the original file is moved to a different file or deleted.

    ln original_file link_file # hardlink
    ln -s original_file link_file # softlink (more felxible)
    ln –s /Users/admin/Documents /Users/admin/Desktop # create a symbolic link for my Documents folder on the Desktop
    rm /Users/admin/Desktop/Documents # remove symbolic link
    

And to check the current status of the service, you’d have to use the systemctl command in the following manner:

systemctl status <service-name>

Understanding systemctl states:

  • active (running): Service is actively running in the background.
  • active (exited): Indicates the service was meant to be executed one time or periodically. So the service did its job and then exit upon completion.
  • active (waiting): It indicates the service is running but it is waiting to be triggered by some condition like a specific event.

  • inactive: Service is not currently running.

  • enabled: Service will be enabled at system boot time.

  • disabled: Service is disabled and won’t be started at system boot.

  • static: It means the specific service can’t be managed using systemd (or the systemctl command) and you’d need to have another init service or manage it manually.

  • masked: This means the service is masked and can’t be stated directly using the systemctl command. This can be helpful when you want to prevent accidental starting of service.

  • alias: It indicates the service name is an alias and the service is a symlink pointing to another unit file.

  • linked: It indicates that the service or the unit file is symbolically linked to another unit file.