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

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.

Related

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"

Need assistance for creating a simple bash script

I've created this bash file putting on it a secuence of commands i often run for synching files from my digital camera. the point is it doesn't to ANYTHING! What am i missing?
thank you!
code:
#!/bin/bash
#temporal
mkdir /tmp/canon
#copy files from camera
rsync -r /run/user/mango/gvfs/g*/DCIM /tmp/canon
cd /tmp/canon
#get files from subdirs
find ./ -name '*.JPG' -exec mv '{}' ./ \;
#remove dirs
ls -l | awk -F'[0-9][0-9]:[0-9][0-9]' '/^d/{print $NF}'| xargs -i rm -rf '{}' \;
#recreate folder structure with year|month pattern
jhead -n%Y/%m/%f *.JPG
#Sync with external HD
rsync -r --ignore-existing . /media/mango/WD/FOTOS/
If it does not even do the mkdir, then it sound most likely that the version of the script you want is not the one running. Try using an qualified path, such as ./myscript or an absolute path, like /home/joe/bin/myscript. The command type myscript will tell from where the shell is running it.
Also, try running the script after adding set -x to the top of the script or using bash -x myscript; that will show every line as it is executed.
If this still doesn't help, there could be bash startup code, such as in .bashrc getting in the way. That's much harder to diagnose, although the same set -x can be used, although with great caution unless a second user can login and edit this user's startup scripts since mistakes in startup scripts can make it impossible to login to the system.
Try this
chmod +x yourscriptname
./yourscriptname
Make usre you are running the same script you made.

CentOS: Copy directory to another directory

I'm working with a CentOS server. I have a folder named test located in /home/server/folder/test. I need to copy the directory test to /home/server/. How can I do it?
cp -r /home/server/folder/test /home/server/
To copy all files, including hidden files use:
cp -r /home/server/folder/test/. /home/server/
As I understand, you want to recursively copy test directory into /home/server/ path...
This can be done as:
-cp -rf /home/server/folder/test/* /home/server/
Hope this helps
This works for me.
cp -r /home/server/folder/test/. /home/server
For copy directory use following command
cp -r source Destination
For example
cp -r /home/hasan /opt
For copy file use command without -r
cp /home/file /home/hasan/

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

How to have the cp command create any necessary folders for copying a file to a destination [duplicate]

This question already has answers here:
Linux: copy and create destination dir if it does not exist
(27 answers)
Closed 7 years ago.
When copying a file using cp to a folder that may or may not exist, how do I get cp to create the folder if necessary? Here is what I have tried:
[root#file nutch-0.9]# cp -f urls-resume /nosuchdirectory/hi.txt
cp: cannot create regular file `/nosuchdirectory/hi.txt': No such file or directory
To expand upon Christian's answer, the only reliable way to do this would be to combine mkdir and cp:
mkdir -p /foo/bar && cp myfile "$_"
As an aside, when you only need to create a single directory in an existing hierarchy, rsync can do it in one operation. I'm quite a fan of rsync as a much more versatile cp replacement, in fact:
rsync -a myfile /foo/bar/ # works if /foo exists but /foo/bar doesn't. bar is created.
I didn't know you could do that with cp.
You can do it with mkdir ..
mkdir -p /var/path/to/your/dir
EDIT
See lhunath's answer for incorporating cp.
One can also use the command find:
find ./ -depth -print | cpio -pvd newdirpathname
mkdir -p `dirname /nosuchdirectory/hi.txt` && cp -r urls-resume /nosuchdirectory/hi.txt
There is no such option. What you can do is to run mkdir -p before copying the file
I made a very cool script you can use to copy files in locations that doesn't exist
#!/bin/bash
if [ ! -d "$2" ]; then
mkdir -p "$2"
fi
cp -R "$1" "$2"
Now just save it, give it permissions and run it using
./cp-improved SOURCE DEST
I put -R option but it's just a draft, I know it can be and you will improve it in many ways. Hope it helps you
rsync is work!
#file:
rsync -aqz _vimrc ~/.vimrc
#directory:
rsync -aqz _vim/ ~/.vim
cp -Rvn /source/path/* /destination/path/
cp: /destination/path/any.zip: No such file or directory
It will create no existing paths in destination, if path have a source file inside.
This dont create empty directories.
A moment ago i've seen xxxxxxxx: No such file or directory, because i run out of free space. without error message.
with ditto:
ditto -V /source/path/* /destination/path
ditto: /destination/path/any.zip: No space left on device
once freed space cp -Rvn /source/path/* /destination/path/ works as expected

Resources