Linux commands using iTerm (Terminal for MacOS) - yoss123/test-automation-repo GitHub Wiki

  • Sudo vi {file_path} - edit a file with root permission

Creating file with list of commands

  1. (recommendation) Create directory named 'bin' under home directory to put there all those commands files
    1.1. cd - move to home directory
    1.2. mkdir bin - create directory named 'bin'
  2. Create the file you want to run the commands in
    2.1. Move to bin directory: cd bin
    2.2. vim [file_name] -> i (to move to insert mode) -> write each command in different line -> ESC -> :wq!
    2.3. Add permissions to the new file to be executed
    2.3.1. To view the permissions of the files under bin directory: ls -lah
    2.3.2. To add execution permission to the created file: chmod a+x [file_name]
    2.3.3. Good source for managin permissions in Linux: https://kb.iu.edu/d/abdb
    2.4. Test the file execution: [file_name]

~/.zshrc
The ~/.zshrc file is the configuration file for the Zsh shell (Z shell). Here's what ~/.zshrc does:
Purpose
Shell Configuration: Contains settings, customizations, and startup commands for your Zsh shell
Runs on Startup: Executed every time you open a new terminal session or start a new Zsh instance
Common Contents
The .zshrc file typically contains:
`

Environment variables

export PATH="/usr/local/bin:$PATH" export EDITOR="vim"

Aliases (shortcuts for commands)

alias ll="ls -la" alias grep="grep --color=auto"

Shell options

setopt AUTO_CD # Change directory without typing 'cd' setopt HIST_VERIFY # Show command before executing from history

Prompt customization

PS1="%n@%m:%~$ "

Functions

function mkcd() { mkdir -p "$1" && cd "$1" }

Plugin configurations (if using frameworks like Oh My Zsh)

Source other configuration files

` 3. (Example) create a alias for running the file
3.1. Edit file the next file: vim ~/.zshrc
3.2. Add the alias like the next line:
alias print_test_alias="cd ~/[dir_path_to_run_from]; /Users/yossidahan/bin/print_test"
3.3. Run the next command to load the changes: source .zshrc
3.4. Test the alias: print_test_alias

Shortcuts

  • control+d - open new window in the current terminal tab
  • control+l - clear the previous text of the current window

Machine resources

  • top - Check memory and CPU usage
  • htop - While top focuses on the processes that consume the most system resources, htop shows all running processes
  • df - displays the amount of disk space available on the filesystem with each file name's argument (-h present it in GB)
  • netstat - (Network Statistics) generates displays that show network status and protocol statistics ; An example for the command with parameters: netstat -noa | grep | grep <process_id>
  • You can find here more info about resources consumption in Linux

  • To search in the command history: history
  • To search a command in the history: history | grep command_to_search_in_hostory

  • To open new tab in the terminal: command+t or right-click and select 'new tab'

  • pwd - prints the current working directory path, starting from the root ( / )
  • whoami - displays the username of the current user
  • rm {file-name} - deletes the specified file
  • mkdir dir_name - creating new directory under the current one
  • echo $HOME - print the value of the environment variable HOME
  • appName --version - print the version of the application on the machine

  • source FILENAME [arguments]
  • source is a shell built-in command which is used to read and execute the content of a file(generally set of commands), passed as an argument in the current shell script. The command after taking the content of the specified files passes it to the TCL interpreter as a text script which then gets executed. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise, the positional parameters remain unchanged. The entries in $PATH are used to find the directory containing FILENAME, however if the file is not present in $PATH it will search the file in the current directory. The source command has no option and the argument is the file only.

  • chmod +x *.sh - add execution privilege for all the files with .sh extension
  • chmod +x * - add execution privilege for all the files in the current directory

  • curl - cURL is a computer software project providing a library and command-line tool for transferring data using various network protocols. The name stands for "Client URL"
  • For example, to Install AWS CLI v2 run the next 2 lines:
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /

  • vim {file-name} - edit the file
  • click on 'i' to edit the file
  • {esc} + :q! - exit editing the file without saving
  • {esc} + :wq! - save editing and exit the file

  • sudo su - changing user to admin
  • ssh {host-name} - connecting (opening terminal) to the host
  • ./file_to_execute.sh - executing the file_to_execute.sh script file
  • exit - logout from the terminal opened by the ssh command

  • ls - list down the files and sub-directories within your current directory
  • ls -lh {file-name} - show info about the specified file
  • ls -a - show all files including the hidden files (those that starts with .fileName)

  • cat {file-name} - view the content of the file
  • cat -n {file-name} - view the content of the file and number each line in the output
  • cat >test_file.txt - create a file named test_file.txt and let you edit it (to quit editing click ctrl+c)

  • more file_name - displays the contents of the file one screen at a time for large files (space - move to the next page ; b - go back to the previous page ; q - go back to the command prompt)
  • less file_name - similar to more with few differences: it's show in the prompt the file name instead of the percentage of the part of the file we're currently viewing ; It let us search specific text pattern in the file

  • tail {file-name} - shows the end of the file (usually .log file). 'cat' command shows all the file
  • tail -f {file-name} - show the current content that being written to the specified file

  • cd ~/.ssh/ - an example for moving to .ssh directory
  • cd - going back to the home directory (you can verify it by running pwd command)

  • find . -name thisfile.txt - search thisfile.txt in current and sub-directories
  • find /home -name *.jpg - Look for all .jpg files in the /home and directories below it
  • To see more 'find' options look in the next link: https://www.plesk.com/blog/various/find-files-in-linux-via-command-line/
  • which file_name - used to locate the executable file associated with the given command by searching it in the path environment variable

  • alias - shows a list of defined aliases on your profile
  • alias shortName="your custom command here" - create a new alias
  • example, to alias my dev machine to dev in my $HOME/.ssh/config you should add the next 3 lines to the config file (while the 3rd line contain my RSA key):
Host dev
     HostName dev-yossid.own-backup-dev.com
     IdentityFile ~/.ssh/id_rsa

  • grep 'word' filename - Search any line that contains the word in filename
  • grep -n 'word' filename - show the matching line and its number
  • grep --color 'word' filename - Search any line that contains the word in filename and display the line with the search-pattern in red
  • grep -i 'bar' file1 - Perform a case-insensitive search for the word ‘bar’
  • grep -R 'httpd' . - Look for all files in the current directory and in all of its subdirectories in Linux for the word ‘httpd’
  • grep -c 'nixcraft' frontpage.md - Search and display the total number of times that the string ‘nixcraft’ appears in a file named frontpage.md
  • for more grep options review the next link: https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/

  • cp file_to_copy directory_path_to_copy_the_file_to - copy the file from one directory to another on the same local machine
  • scp [email protected]:/remote/directory/file_name_to_copy - copy a file from remote machine to local machine
  • for example: scp yossid@dev:./ob_deploy/ansible_dev_deploy_4.log
  • To see the path of the file copied to the local machine: pwd
  • To open the directory of the copied file: open .
  • for example: yossidahan@C02F83SDMD6P-YD dev_machine % pwd
  • will result: /Users/yossidahan/src/ownbackup/scripts/dev_machine
⚠️ **GitHub.com Fallback** ⚠️