Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check directory exist or not in linux.? [duplicate]

Tags:

Given a file path (e.g. /src/com/mot), how can I check whether mot exists, and create it if it doesn't using Linux or shell scripting??

like image 982
Vivek Avatar asked Jan 09 '12 15:01

Vivek


People also ask

How do you check if a directory exists or not in bash?

Checking If a Directory Exists In a Bash Shell Script-h "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir exists but point to $(readlink -f /path/to/dir)." The cmd2 is executed if, and only if, cmd1 returns a non-zero exit status.

How do I check directory?

To see them in the terminal, you use the "ls" command, which is used to list files and directories. So, when I type "ls" and press "Enter" we see the same folders that we do in the Finder window.

How do I find a missing directory in Linux?

The fsck utility makes it easy for us to recover the lost data. To recover lost data, we need to have run fsck before. Most Linux distros would run the fsck command on boot if the machine didn't properly shut down. Otherwise, we'll have to run it manually.

How do I list directories in Linux?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.


1 Answers

With bash/sh/ksh, you can do:

if [ ! -d /directory/to/check ]; then     mkdir -p /directory/toc/check fi 

For files, replace -d with -f, then you can do whatever operations you need on the non-existant file.

like image 65
Chris J Avatar answered Nov 17 '22 01:11

Chris J