💡
Knowledge
  • Home
  • Technology
    • Browser
      • Chrome / Brave
        • Known Issues
    • Messaging
      • Discord
        • Known Issues
      • Microsoft Teams
      • Telegram
    • Office Production
      • Sheets
    • Music Production
    • Operational systems
      • Docker + OSX
      • Raspberry Pi OS
      • Linux
        • Terminal
        • Known Issues
        • Desktop Environment
          • Gnome
            • How to
            • Known Issues
            • Theming
          • Kde Plasma
        • How to
          • Audio
          • Proxy
          • SSH
          • ZSH
      • Windows
    • Programming
      • Code Quality
        • Stress Tests
      • Cascading Style Sheets(CSS)
      • Database
        • Postgres
        • SQLServer
      • Design Patterns
      • DevOps
        • Cloud Platforms
        • Continuous Integration
        • Docker
          • How to
          • IPSEC VPN Server
          • Docker Compose
          • Known issues
          • Proxy
        • Swarm
      • Git
        • How to
        • Known Issues
        • Github
        • Gitlab
          • GitlabCI
          • Gitlab Runner
      • IDE / Text Editor
        • Vim
          • Commands
        • PHPStorm
        • VSCode
      • Programming Languages
        • Typescript
        • Java
          • How to
          • Spring Boot
        • Javascript
          • Known issues
          • Backend
            • NestJS
            • NodeJS
          • Frontend
            • JQuery
            • React
            • Vue
          • How to
          • Package Manager
            • Yarn
          • Packages
          • Vanilla
        • PHP
          • About
          • Cache
          • Composer
          • Docker
          • How to
          • Known Issues
          • Laravel
            • Jet Stream
            • Know Issues
            • Sanctum
            • Sail
            • Valet
          • Tools
            • PHPUnit
          • Wordpress
            • Docker
            • WP CLI
            • Known Issues
            • WooComerce
        • Python
        • Shell Script
      • Server
        • Apache2
          • Known Issues
        • Nginx
          • How To
          • Known issues
      • Tools
        • Visual Studio Code
    • Stream
      • Game
      • Twitch
      • Tests
        • Unit Tests
    • Sites
    • Specs
    • Tools
  • Pessoal
    • About me
Powered by GitBook
On this page
  • File exists
  • With the if statement
  • With the "&&" operator
  • Folder exists
  • Or not exists
  • Find differences between foldes
  • Show ports in use
  • Create alias
  • Cheatsheet of multiple commands
  • Read every line from a file
  • Log
  • Basic view of records

Was this helpful?

  1. Technology
  2. Programming
  3. Programming Languages

Shell Script

File exists

With the if statement

FILE=/etc/resolv.conf
if [ -f "$FILE" ]; then
    echo "$FILE exists."
else 
    echo "$FILE does not exist."
fi

With the "&&" operator

[ -f /etc/resolv.conf ] && echo "File exists!"

Folder exists

if [ "$(ls -A /tmp)" ]; then
  echo "Exists and is not Empty"
fi

# Or using `-d`

if ! [ -d "/var/www/html" ] || ! [ -d "/var/www/x-html" ]; then
    echo "Folder not found here - ;("
fi

Or not exists

if ! [ "$(ls -A /tmp322)" ]; then
  echo "Empty"
else
  echo "Not Empty"
fi

Find differences between foldes

diff -q directory-1/ directory-2/

Show ports in use

sudo lsof -i -P -n | grep LISTEN

Create alias

Insert the line below to your ~/.zsh.rc or ~/.profile

alias ports="sudo lsof -i -P -n | grep LISTEN"

Cheatsheet of multiple commands

A; B    # Run A and then B, regardless of success of A
A && B  # Run B if and only if A succeeded
A || B  # Run B if and only if A failed
A &     # Run A in background.

Example

mv tmp.file tmp.file2 ; echo 'aa'
mv tmp.file tmp.file2 && echo 'aa'
mv tmp.file tmp.file2 || echo 'aa'
mv tmp.file tmp.file2 &

Read every line from a file

In the example below read every line from a file and do something.

while read line; do
  if [ ! -z $line ]; then
   echo "export $line" >> /etc/apache2/envvars
  fi
done <app.env

Log

Basic view of records

journalctl

To see the logs that the journald daemon has collected, use the journalctl command

PreviousPythonNextServer

Last updated 3 years ago

Was this helpful?