I need to run something like
mkdir /var/log/apache2/www/custom-name/
The problem is if some of directories in path are missing. Shell does not create missing directories but throws me an error.
Is it possible to make missing directories in the path without testing if it exists?
If I understood the question, yes, it is.
In your case, instead of specifying
mkdir /var/log/apache2/www/custom-name/
write
mkdir -p /var/log/apache2/www/custom-name/
The -p flag enables the creation of parent directories. It should run without any error. (Reference 1 and 2)
Related
This might appear a noob question.
While working in bash, if we run cd ., it stays in the current folder.
I understand the functionality, however, I am not able to understand the rationale of this functionality?
What would be some practical ways to use this?
The primary use case I've seen for cd . is to test whether your file handle on the current directory is still valid.
If you're on a directory from a network share -- NFS, or the like -- it can be possible for directories to be remotely deleted, but for the local client to still believe they're accessible and in use.
cd . is a way to trigger an error if your handle on the current working directory is no longer valid.
This is the only "practical" case that came to my mind
$ cd .
cd: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
when your process has a current working directory referencing a directory that has been removed by another process.
That command has no functionality. But in a POSIX-compliant environment, if you add a -P option, then it has functionality: it resolves symlinks. So for example on a Mac, if you cd to a path with a symlink:
cd /System/Library/Frameworks/AppKit.framework/Versions/Current
...then do cd -P . ... you will point to:
/System/Library/Frameworks/AppKit.framework/Versions/C
. is a special file that represents the current directory.
There are plenty of things which make use of directories and it is sometimes useful to refer to the current directory.
Changing the directory to the current directory is not one of those.
A simple example where cd . fails:
mkdir my_error
cd my_error
rm -rf ../my_error
cd .
When the rm is embedded in a difficult script or can be done by some other cleanup process, is can be an useful check.
I use a build script which removes and recreates a directory.
The old directory disappears and new appears with new inode.
If, in one of my shells my $PWD is that reappeared directory and I notice
it became unusable (and I know it was recreated), I just
$ cd .
to get the (new) directory useable again and can continue my work there.
I am following a tutorial where I have to create a directory but also pass -p flag. I tried running it and I got a syntax failure. So I wanted to figure out what the -p did and found that this abbreviation is short for privileged. And found
Script runs as "suid" (caution!)
Started looking what that meant and found it meant Set User Identification and read that
– When a command or script with SUID bit set is run, its effective UID becomes that of the owner of the file, rather than of the user who is running it. Source
However, I still do not quite understand it. What is the purpose of me setting a directory to have that privilege and why should I be careful? Also, I tried looking here but I couldn't find any clarification(with the different search keywords I used). Also, not necessary.. but , why would me doing mkdir -p src/entities give me a syntax failure? I am using Windows(but I also have a bash package for Anaconda).
It looks like you're following a Unix-ish tutorial but running the commands on Windows in cmd.exe.
As the usage instructions say:
C:\>mkdir /?
Creates a directory.
MKDIR [drive:]path
MD [drive:]path
If Command Extensions are enabled MKDIR changes as follows:
MKDIR creates any intermediate directories in the path, if needed.
For example, assume \a does not exist then:
mkdir \a\b\c\d
is the same as:
mkdir \a
chdir \a
mkdir b
chdir b
mkdir c
chdir c
mkdir d
which is what you would have to type if extensions were disabled.
Windows commands don't use - for options (and in particular, the mkdir command built into cmd doesn't understand -p).
The part about "privileged" is for the shell option -p, as in bash -p. It has nothing to do with mkdir -p, which is explained in man mkdir:
-p, --parents
no error if existing, make parent directories as needed
But again, that only applies to the Unix mkdir, not Windows / cmd.
"-p" creates parent directories if they don't exist.
For example:
With "-p" if "first" directory doesn't exist.
mkdir -p first/second # "first" parent directory is created
Without "-p" if "first" directory doesn't exist.
mkdir first/second # "first" parent directory is not created
mkdir: cannot create directory ‘first/second’: No such file or directory
I have been reading the description of the OSX Man page. It has description like following regarding mkdir -p:
-p
Create intermediate directories as required. If this option is not specified, the full path prefix of each operand must already exist. On the other hand, with this option specified, no error will be reported if a directory given as an operand already exists. Intermediate directories are created with permission bits of rwxrwxrwx (0777) as modified by the current umask, plus write and search permission for the owner.
I am not quite following this description. especially "If this option is not specified, the full path prefix of each operand must already exist. On the other hand, with this option specified, no error will be reported if a directory given as an operand already exists."
Does someone has an example regarding this explanation?
Given this directory structure:
/
foo/
bar/
baz/
This will obviously work:
mkdir /foo/x
This will not work:
mkdir /foo/x/y
Because /foo/x does not exist, the directory /foo/x/y cannot be created underneath it. The prefix /foo/x/ needs to exist in order to create /foo/x/y.
This is where -p comes in. This works:
mkdir -p /foo/x/y
/foo/x will implicitly be created together with /foo/x/y.
If you try:
mkdir /bar/baz
You'll get an error that the directory already exists. However, if you do:
mkdir -p /bar/baz
you will not get an error, it'll just silently ignore all already existing directories and be content with the result without doing anything.
Imagine you have an empty folder, and you want to create a subdirectory called "d1" and a subirectory inside "d1" called "d2". Normally you must do this:
mkdir d1
mkdir d1/d2
With the "-p" option you can have mkdir create the in-between directory (d1) for you:
mkdir -p d1/d2
The bit you are asking about says that if "d1" already exists and you use "mkdir -p" it won't matter that it is already there and there won't be any error messages.
This work on version 10.10.4
mkdir -pv d1/d/d3
Not sure if it is mac only, but on mac os x you can do
mkdir -p src/{main,test}/{java,resources,scala}
which will not only give you a nested, but also cartesian product of your directories:
src/test/java
src/test/resources
src/test/scala
src/main/java
src/main/resources
src/main/scala
How to delete the recursive directory in Solaris.
Directory Structure : SourceCode/unit_test_cases.
The command
cp -rf SourceCode/ SourceCode/unit_test_cases/ : created a recursive directory.
Directory Structure looks like below
SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/SourceCode/unit_test_cases/
I know "rm -rf SourceCode" is to delete recursively. But this command gives the error File Name too long. This is due to unlimited child directories recursively created.
So am not able delete the directories.
I tried this in Ubuntu, It is smart that it determines the cyclic recursion and breaks # some depth, But Solaris seems to be dumb in this case.
Can anybody help me
It is simply
rm -rf PATHS
where PATHS is one or more PATH to be removed (it's enough SourceCode if you want to remove that dir completely).
Try this
rm -r <your directory>
and be careful of what you are deleting.
First off I am very very new to shell scripting. I am trying to write a script that takes in one parameter and then copies a folder in a different directory naming it using the parameter. This is the current code that I have:
#!/bin/sh
cd /var/www/html/fbplugin/chrome
sudo mkdir temp/$1
sudo cp -rf "/var/www/html/fbplugin/chrome/fbplugin" "/var/www/html/fbplugin/chrome/temp/$1"
When I run this code it says can't cd to /var/www/html/fbplugin/chrome. I'm not sure why it is saying this because I know the directory exists. I have copied the line directly and it works in terminal. If anyone could help me out that would be great.
If it matters in order to run the script I am typing "sh build.sh"
If that directory really exists, then you must have executed that script with a different user (cron, webserver, etc).
Check the rights for that directory.
I don't know why you're getting the error about cd, but it looks like you could just use absolute paths throughout. That would solve the larger problem of the script working correctly.