Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

how to check a path is file or director in shell script

#!/bin/bash

########### to check for a directory ###########
path="./folder"
if [[ -d $path ]]; then
	echo "$1 Directory exists"
fi

# or

[ -d "folder" ] && echo Directory exists

########### to check for a file ###########
path="./file.txt"
if [[ -f $path ]]; then
	echo "$1 File exists"
fi

# or

[ -f "file.txt" ] && echo file exists
Comment

PREVIOUS NEXT
Code Example
Shell :: how to search for a package on arch repositories 
Shell :: yaml reuse block 
Shell :: composer command to install vendor in magento 2 
Shell :: Payload retrieve sam and system from a live file system 
Shell :: bash clear all environment variables 
Shell :: shell remove pdf pages 
Shell :: docker cleanup - Remove Docker Images, Containers, Networks & Volumes 
Shell :: eslint src multiple extensions 
Shell :: Create bash BACKUP 
Shell :: jq select where value starts with 
Shell :: gitattributes -diff 
Shell :: retrieve column from csv unix 
Shell :: crontab test run-parts 
Shell :: fish function to change php version devilbox .env 
Shell :: kali qcow2 image download 
Shell :: case in fish 
Shell :: ctrl+z equivalent in vim 
Shell :: barryvdh dompdf file_getcontents passing null 
Shell :: bash script help syntax option vs mandatory 
Shell :: add user to sudoer "zsh" customization 
Shell :: get character frequency in linux 
Shell :: sed beginning of file 
Shell :: output file contents linux 
Shell :: can you speak in linux commands 
Shell :: vscode is blacking out on wsl 
Shell :: . ~/ms_env.sh 
Shell :: docker compose share gpu 
Shell :: linux mount lvm 
Shell :: ubuntu command to make a soft link 
Shell :: weka explorer for ubuntu 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =