Checking wether a directory exist in the home directory - linux

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

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

Bash script, redirect output to another directory

I have an environment variable containing the name of a directory. I am trying to redirect output from an echo command to a text file in a different directory.
For example
DIR="NewDirectory"
mkdir $DIR
echo "Testing" >> "$DIR\file.txt"
Results in a file named NewDirectory\file.txt in the working directory of the script...what exactly am I missing here? The directory is created without issue, so I am not sure what is going on here.
You have to change \ into /:
DIR="NewDirectory"
mkdir -p $DIR
echo "Testing" >> "$DIR/file.txt"
Changed mkdir -p as suggested by #Jord, because -p means: no error if existing, make parent directories as needed
In linux (or unix for that matter), the directory separator is a slash (/), not a backslash (\):
DIR="NewDirectory"
mkdir $DIR
echo "Testing" >> "$DIR/file.txt"
Your line
echo "Testing" >> "$DIR\file.txt"
should read
echo "Testing" >> "$DIR/file.txt"
as / is the separator in paths in Linux.

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

Issue creating a folder then moving files into it within the same script

I'm having problems moving files into a folder after I create it in a shell script.
My script looks like:
#!/bin/bash
echo -e "Processing\033[36m" $1 "\033[0mwith the German script";
if [ ! -d ${1%.dat} ]; then
echo -e "making directory\033[33m" ${1%.dat} "\033[0msince it didn't exist...";
mkdir ${1%.dat};
fi
...processing occurs here... (irrelevant to issue)
if [ -d ${1%.dat} ]; then
mv useragents_$1 /${1%.dat}/useragents_$1;
mv summary_$1 /${1%.dat}/summary_$1;
more /${1%.dat}/useragents_$1;
else
echo -e "\033[31mERROR: cannot move files to folder.\033[0m";
fi
As you can see I create the folder if it doesn't exist in the top section and then if it exists I move the files into that folder in the bottom section, the problem is that it doesn't create the folder in time to move the files in (I'm assuming) so when it reaches the lower code, I only get the ERROR.
I tried using, sleep 5, but it only slows down the script and has no effect on the ERROR.
I would really appreciate some advice.
Errors below:
mv: cannot move `useragents_100_stns2_stns6.dat' to `/100_stns2_stns6/useragents_100_stns2_stns6.dat': No such file or directory
mv: cannot move `summary_100_stns2_stns6.dat' to `/100_stns2_stns6/summary_100_stns2_stns6.dat': No such file or directory
/100_stns2_stns6/useragents_100_stns2_stns6.dat: No such file or directory
Pass 1
Your check:
if [ ! -d ${1%.dat} ]; then
should be:
if [ -d ${1%.dat} ]; then
You created the directory; if it is a directory, move stuff into it.
Typo in question
Pass 2
You create:
mkdir ${1%.dat}
You try to move files:
mv useragents_$1 /${1%.dat}/useragents_$1;
Note the leading slash in the move compared to the create. Make those consistent.
Are you sure of this part ? It uses a root directory.
/${1%.dat}/summary_$1;
You probably want to do this instead:
${1%.dat}/summary_$1;
It allows you to move the file into the directory IN your current directory.

Bash: move file/directory and create a link of it

I am trying to make a bash script that moves a file or directory from source directory to destination directory and puts a symlink to it into source directory.
So, <source_path> can be a file or directory, <destination_dir_path> is the directory where I want the original moved to.
Sample usage:
$ mvln /source_dir/file.txt /destination_dir/
OR
$ mvln /source_dir/dir_I_want_to_move/ /destination_dir/
This is what I have managed to put together, but it does not work properly.
It works only if source is a directory, otherwise mv returns an error:
mv: unable to rename `/source_dir/some_file.txt': Not a directory
And the directory is not moved into destination_directory but only its contents are moved.
#!/bin/bash
SCRIPT_NAME='mvln'
USAGE_STRING='usage: '$SCRIPT_NAME' <source_path> <destination_dir_path>'
# Show usage and exit with status
show_usage_and_exit () {
echo $USAGE_STRING
exit 1
}
# ERROR file does not exist
no_file () {
echo $SCRIPT_NAME': '$1': No such file or directory'
exit 2
}
# Check syntax
if [ $# -ne 2 ]; then
show_usage_and_exit
fi
# Check file existence
if [ ! -e "$1" ]; then
no_file $1
fi
# Get paths
source_path=$1
destination_path=$2
# Check that destination ends with a slash
[[ $destination_path != */ ]] && destination_path="$destination_path"/
# Move source
mv "$source_path" "$destination_path"
# Get original path
original_path=$destination_path$(basename $source_path)
# Create symlink in source dir
ln -s "$original_path" "${source_path%/}"
Can some one please help?
The problem is that $destination_path refers to a directory that doesn't exist. Something like this:
mv /path/to/file.txt /path/to/non/existent/directory/
returns an error, and
mv /path/to/directory/ /path/to/non/existent/directory/
will rename /path/to/directory/ to /path/to/non/existent/directory/ (provided that /path/to/non/existent/ is an existent directory, just without a subfolder named directory).
If you are expecting that $destination_path doesn't already exist, then you can add a mkdir command:
mkdir "$destination_path"
mv "$source_path" "$destination_path"
if you're expecting that it might not exist, then you can add it conditionally:
[[ -d "$destination_path" ]] || mkdir "$destination_path"
mv "$source_path" "$destination_path"
and if you're expecting that it does exist, then you have some debugging to do!
(By the way, depending on your exact situation, you might find mkdir -p to be helpful. It recursively creates a directory and all necessary parent directories, and it doesn't mind if the directory already exists.)

Resources