mkdir: cannot create directory No such file or directory - linux

I can't find a solution for this problem. I am trying to write a script that creates a folder, which name is the current date, with all content of the current directory so I can backup a bunch of files. I tried following things already that doesn't work:
use "mkdir -p" to create parent directories
not using relative path with "pwd"
But when I try and create a directory out of an script in the terminal(also bash) I can create perfectly fine directories with the date inside. (following command)
mkdir backup$(date +%d-%m-%Y_%H:%M)
My Code
#!/bin/bash
DATE=$(date +%d-%m-%Y_%H:%M)
PWD=$(pwd)
FILENAME=backup$DATE
if [ -d "backup/" ]; then
mkdir -p backup/$FILENAME
cp -r * backup/$FILENAME
else
mkdir -p backup/
mkdir -p backup/$FILENAME
cp -r * backup/$FILENAME
fi
Error thrown
mkdir: cannot create directory ‘backup/backup18-01-2022_12:43’: No such file or directory

Related

Prevent mkdir -p from overwriting directories and cp from overwriting files

I have a shell script and I want to use it for making directories and files in those direcctories. It is extremely important that this script does not overwrite or delete any files or directories. So I want to make a directory which is one directory above from the directory where the script is in. In this directory I want to make two subdirectories and in one of the subdirectories I want to copy 2 pre-existing files that have some text in them.
Files file1 and file2 are always going to be in same directory as this script and they always have same contents in them and it is very important that the contents do not change. I have multiple structures like this, they just have different names.
So I tried this:
#! /bin/bash
echo "Enter directory name"
read dirname
mkdir -p ../$dirname/{dir1,dir2}
cp file1 file2 ../$dirname/dir2
But if dirname already exists, this script overwrites it and also overwrites all the contents in it. Then I tried this:
#! /bin/bash
echo "Enter directory name"
read dirname
if [ -d $dirname ]
then
echo "directory already exists"
else
mkdir -p ../$dirname/{dir1,dir2}
cp file1 file2 ../$dirname/dir2
fi
But also this script overwrites everything. How can I make this script so that if dirname already exists, the script does not create new directories and it does not copy any files in any directory, i.e. it does not do anything?

mkdir create directory in current users directory - bash

#!/bin/bash
mkdir -p user_records
I want to create a directory user_records in the current user's directory. How do I achieve this? I tried adding sudo in front of mkdir but that does not create the directory in the desired location.
You should be able to access the user directory with ~, so you could try:
mkdir -p ~/user_records
Using the -p option is not necessary here. It is only useful if you want to create a hierarchy of directories. Example: if you want to create a "baz" directory inside a "bar" directory, inside a "foo" directory, inside the current directory, you will use the following command: mkdir -p foo/bar/baz.
In your Bash script, you have 3 simple solutions:
cd
#!/bin/bash
cd
mkdir user_records
~
#!/bin/bash
mkdir ~/user_records
$HOME
#!/bin/bash
mkdir "$HOME/user_records"

changing folder permissions to admin

I have a bash script to create and move files
This script is making folder bassed on file name and then create another folder downloads
example movie.mkv
movie/downloads/movie.mkv
for file in *.mkv; do
folder=$(basename "$file" ".mkv")"/downloads"
mkdir -p "$folder" && mv "$file" "$folder"
done
but folders created by this script have root permissions and I need admin permissions
Is there any way to edit this script?
Here is a picture what I want to change:
http://s5.postimg.org/5cmxxf8on/Bez_tytu_u.jpg
Best Regards and THank You
Since you're running script as root you can add chown command as the last command in script to change owner to admin:
for file in *.mkv; do
folder=$(basename "$file" ".mkv")"/downloads"
mkdir -p "$folder" && mv "$file" "$folder"
chown -R admin "$folder"
done

Copying files with wildcard (*) to a folder in a bash script - why isn't it working?

I am writing a bash script that creates a folder, and copies files to that folder. It works from the command line, but not from my script. What is wrong here?
#! /bin/sh
DIR_NAME=files
ROOT=..
FOOD_DIR=food
FRUITS_DIR=fruits
rm -rf $DIR_NAME
mkdir $DIR_NAME
chmod 755 $DIR_NAME
cp $ROOT/$FOOD_DIR/"*" $DIR_NAME/
I get:
cp: cannot stat `../food/fruits/*': No such file or directory
You got that exactly backwards -- everything except the * character should be double-quoted:
#!/bin/sh
dir_name=files
root=..
food_dir=food
fruits_dir=fruits
rm -rf "$dir_name"
mkdir "$dir_name"
chmod 755 "$dir_name"
cp "$root/$food_dir/"* "$dir_name/"
Also, as a matter of best-practice / convention, non-environment variable names should be lower case to avoid name conflicts with environment variables and builtins.

Copy and overwrite a file in shell script

I want to copy a certain file to a location, irrespective of that file already exists in the destination or not. I'm trying to copy through shell script.But the file is not getting copied. I'm using the following command
/bin/cp -rf /source/file /destination
but that doesn't work.
Use
cp -fr /source/file /destination
this should probably solve the problem.
This question has been already discussed, however you can write a little script like this:
#!/bin/bash
if [ ! -d "$2" ]; then
mkdir -p "$2"
fi
cp -R "$1" "$2"
Explaining this script a little bit
#!/bin/bash: tells your computer to use the bash interpreter.
if [ ! -d "$2" ]; then: If the second variable you supplied does not already exist...
mkdir -p "$2": make that directory, including any parent directories supplied in the path.
Running mkdir -p one/two/three will make:
$ mkdir -p one/two/three
$ tree one
one/
└── two
└── three
If you don't supply the -p tag then you'll get an error if directories one and two don't exist:
$ mkdir one/two/three
mkdir: cannot create directory ‘one/two/three’: No such file or directory
fi: Closes the if statement.
cp -R "$1" "$2": copies files from the first variable you supplied to the directory of the second variable you supplied.
So if you ran script.sh mars pluto, mars would be the first variable ($1) and pluto would be the second variable ($2).
The -R flag means it does this recursively, so the cp command will go through all the files and folders from your first variable, and copy them to the directory of your second variable.
Your problem might be caused by an alias for cp command created in your system by default (you can see al your aliases by typing "alias").
For example, my system has the following alis by default: alias cp='cp -i', where -i overrides -f option, i.e. cp will always prompt for overwriting confirmation.
What you need in such case (that'll actually work even if you don't have an alias) is to feed "yes" to that confirmation. To do that simply modify your cp command to look like this:
yes | cp /source/file /destination
/bin/cp -rf src dst
or
/usr/bin/env cp -rf

Resources