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

This question already has answers here:
How do I check if a directory exists or not in a Bash shell script?
(35 answers)
Closed 2 years ago.
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??

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.

Check for directory exists
if [ -d "$DIRPATH" ]; then
# Add code logic here
fi
Check for directory does not exist
if [ ! -d "$DIRPATH" ]; then
# Add code logic here
fi

mkdir -p creates the directory without giving an error if it already exists.

Well, if you only check for the directory to create it if it does not exist, you might as well just use:
mkdir -p /src/com/mot
mkdir -p will create the directory if it does not exist, otherwise does nothing.

test -d /src/com/mot || mkdir /src/com/mot

This is baisc, but I think it works. You'll have to set a few variables if you're looking to have a dynamic list to cycle through and check.
if [ -d /src/com/mot ];
then
echo Directory found
else
mkdir /src/com/mot
fi
Hope that's what you were looking for...

Related

Bash scripting and comparing 2 directories - Beginner

My problem is that i have to check what files are included in the $directory, then i have to make a new file with name specified by me and compare if the name doesn't already exist in this directory (compare 2 directories).
Below is my code:
directory=$(pwd -L "/$nameProject")
read -p "Enter repo name: " nameRepo
# Check if repo name exists in $directory
if [$(find "$directory/$nameProject" -path "$directory/$nameProject/*")==$("$directory/$nameProject/$nameRepo")]; then
instruction..
fi
Thank you for any help!
Did you try
if [[ ! -d "$directory/$nameProject/$nameRepo" && ! -f "$directory/$nameProject/$nameRepo"]] ; then
mkdir "$directory/$nameProject/$nameRepo"
fi
You can split the above if loop into two and print appropriate error by removing !
Spaces are important to the shell. So is quoting. You need spaces around [ and == and ] (though ]; is ok). (Also technically the comparison operator is = and not ==.).
Also find can return more than one result which will cause problems for your test.
If you want to test whether a directory (or file) with a given name exists then you don't need (or want) to use find. You can just use the -d and -f (and -e) tests of the [ test binary/built-in directly(spec, bashref).
if [ -d path/to/directory ]; then
echo "Was a directory"
else
echo "Was not a directory"
fi

Checking wether a directory exist in the home directory

Trying to check wether a directory exist in the the home directory
if [ ! -d "$HOME/Smart_Cycle" ]; then
mkdir Smart_Cycle $DIRPATH
echo "Creating DIrecroty""
fi
Trying to check wether the Smart_Cycle directory exist in the home directory, and if it does not exist it will create the directory. Not sure what is going on or if I am on the right track.
When running the script I have these two error that I have never seen before
./smartcycle: line 4: unexpected EOF while looking for matching `"'
./smartcycle: line 6: syntax error: unexpected end of file
EOF is because you have double "" in
"Creating DIrecroty""
As Etan suggested, a better way is to use
mkdir -p "$HOME/Smart_Cycle"
-p will make sure that all directories in the specified path exist and if not, they will be created.
You had an extra quotation mark on
echo "Creating DIrecroty""
Try this
if [ ! -d "$HOME/Smart_Cycle" ]; then
mkdir "$HOME/Smart_Cycle"
echo "Creating DIrecroty"
fi
You can create more than one directory at once
mkdir A B C
In your case, $DIRPATH will be evaluated and a second directory created pointing to the value contained in $DIRPATH

How do you refer to an error in Bash/Shell Script?

Is it possible to refer to an error? Here is my code:
read dir
mkdir /Users/Dillon/$dir
And if the directory is already there, it tells me mkdir: /Users/Dillon/(dir): File exists
. Is there a way to state that if it already exists to not not show error?
You can test for directory existence before running the command:
[ -d /Users/Dillon/$dir ] || mkdir /Users/Dillon/$dir
Alternately, you can use the -p flag:
mkdir -p /Users/Dillon/$dir
This will make the directory if it does not yet exist, as well as any missing directories in the path. It does not complain if the directory already exists. It will complain if any segment of the path exists, but isn't a directory or a symlink to a directory.
To suppress error output for any command, redirect the stderr stream to /dev/null
mkdir /Users/Dillion/$dir 2> /dev/null
Or for this one specific case, you could first check for the existence of the directory and bypass the mkdir call if the directory exists:
if [ ! -d /Users/Dillion/$dir ]; then
mkdir /Users/Dillion/$dir
fi

Linux, how to create file using directory name?

How to create folders like: wdw/1/11, wdw/2/22, ... wdw/6/66, ..., wdw/9/99, and file using directory name in the deepest directory like directoryname_file.txt
In bash:
mkdir wdw # Creates the top dir.
mkdir {1..9} # Creates the subdirs using brace expansion.
for dir in {1..9} ; do
mkdir $dir/$dir$dir # Creates the subsubdirs.
touch $dir/$dir$dir/$dirdir"_file".txt # Creates the file.
done
In Bash, you can use the mkdir command:
your_dir="wdw/1/11"
if [ ! -d $your_dir ]; then
mkdir $your_dir
fi
The IF clause is to check if the directory already exists.
You can loop to change the value of "your_dir" with /2/22, 6/66, etc...
You'll have use the -p flag with mkdir to create nested directories, for example:
dir_name="wdw/1/11"
mkdir -p $dir_name
Then use touch or echo to create the files:
touch $dir_name/directoryname_file.txt
or
echo "some text" > $dir_name/directoryname_file.txt

Check if file exist Linux bash [duplicate]

This question already has answers here:
How do I tell if a file does not exist in Bash?
(20 answers)
Difference between ./ and ~/
(6 answers)
Closed 2 years ago.
So I'm trying to check if a file exists or not and then the script is supposed to do something if it does. The problem I'm having is actually getting it to recognize that something is actually there.
if [ -e /temp/file.txt ]; then
echo "file found!"
sudo cp -r temp/* extra
else
echo "file not found! Creating new one..."
./create.sh
fi
below is an example of the files in the directory I'm testing. they are clearly there, but for some reason I can't get the script to see that. what am I doing wrong?
nima#mkt:/docs/text$ ls -a temp
. .. more file.txt file2.txt
You are using absolute paths in your test while you should be using relative paths:
if [ -e ./temp/file.txt ]; then
/temp/file.txt vs /docs/text/temp/file.txt?
You script looks in /temp while you are looking in /docs/text/temp

Resources