mkdir create directory in current users directory - bash - linux

#!/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"

Related

mkdir: cannot create directory No such file or directory

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

Mkdir working for one folder but not the other in a bash script?

This is probably a simple fix but I wrote a bash script to create two directories with one of those being a sub-directory of the other. I will link the script below. It creates the "/usr/local/sites" just fine but it won't create the A-upgrade below that directory for some reason. Any thoughts?
#!/bin/bash
DIRECTORY=/usr/local/sites/
SITE=A
sudo mkdir -p "$DIRECTORY"
sudo mkdir -p "$DIRECTORY/$SITE-upgrade/"
cd "$DIRECTORY/$SITE-upgrade/"
After help from the others in the comments, I stupidly realized that I had a cleanup function in my script that was deleting my directory, which is what it was suppose to do. Thanks again for the help guys. Sometimes it helps to add a "-x". The cleanup directory did the following and was deleting the directory I was searching for.
log "cleaning up folder"
log "cd up a directory"
cd ..
log "remove folder $SITE-upgrade"
find "$SITE-upgrade" -type d | xargs rm -rf
You have $SITE in the sudo statement instead of $SITES, which is the variable you assigned to above the sudo statement.

Linux Copy a directory but with a different name?

I have a directory that I want to copy all of it, but to a directory with a different name.
Example:
/Home/user/DirA-Web
copy its contents to (but it needs to be created)
/Home/user/version1/DirB-Img
/Home/user/version2/DirB-Img
I could always copy it and the rename it, I suppose.
Edit: I currently rsync the directories to the desired location and them mv in a for loop to rename them. I am looking for something cleaner.
If the directory
/Home/user/version1/
exists, a simple cp will do:
cp -r /Home/user/DirA-Web /Home/user/version1/DirB-Img
If not, you need to use mkdir beforehand, because cp has no option to
create your target directory recursively:
mkdir -p /Home/user/version1/DirB-Img && cp -r /Home/user/DirA-Web /Home/user/version1/DirB-Img

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