linux mkdir directory in directory - linux

I need to make a hierarchie of directories but I can only use 1 command, how can I do the following?
I'm currently in the directory called 'linux', now I have to create here a directory called 'a' and in this directorie I have to create a directory called 'b'.
thnx

You can try:
mkdir --parents a/b

mkdir -p a/b/c/d
-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.

Related

chmod cannot access to 'uploads/' :file or folder doesn't exist

i did this
sudo chmod -R 775 uploads/
and got this
chmod cannot access to 'uploads/' :file or folder doesn't exist
Your command is syntactically correct. The reason why it is reporting file or folder doesn't exist will be that the file or folder doesn't exist.
As you are using a relative path, you may wish to ensure that the current directory when you type the command is the parent directory of where you expect the uploads directory to reside. Note in particular that sudo will run the chmod command from the existing current directory (for example, it would not change to the home directory of the root user merely because it is running the command as root).
If you wish to check whether uploads is a valid path relative to the current directory, then use the ls command (without command-line arguments) to list the contents of the current directory, and see whether uploads (case-sensitive) is mentioned in the output.

Cannot create directory in tmp(overthewire bandit24)

I'm trying to solve bandit24 on overthe wire on ubuntu virtual machine.
I have already seen the solution.
But i have a problem,when i try to create a directory on tmp as bandit24#bandit i get this message:
Cannot create directory "name_of_directory": file exists.
If I try with find command there is only the "." directory and with ls I get the message:
Cannot open directory '.' : permission denied.
I also have tried with ls -l on tmp and I get the message:
Cannot open directory 'tmp': Permission denied
What else could I do?
What could be the problem?
Try to prepend sudo at your command. Seems you don't have permissions to read the /tmp directory, what is pretty weird.
Example that might works:
To list the /tmp contents:
sudo ls -l /tmp
To create the 'my_new_dir' inside /tmp:
sudo mkdir /tmp/my_new_dir
It means that there is a directory under /tmp/ with the same name that you specified. But since you did not create it (in this case, someone created with a different bandit user), you cannot view it. There is not read permission for bandit24 to access it.
Since /tmp/ is directory accessible for all user accounts, you cannot list the files/directories under it without the root permission. (Which means the root of the bandit machine has configured like that)
What you need to do
Try a random name. Create anything random under /tmp/. It will work.

How to copy files and give them permission of destination directory

I am copying files from source to location. The source is not owned by me and the permission for files at source is ----rwx---. The permission of files coped to destination directory which is owned by me is ----r-x---. The permission of destination directory is drwxrwsrwx. How do I have the files with same permission of destination directory. I tried "cp --no-preserve=all" but it did not work (still the same permission).
Try this:
cp --no-preserve=mode,ownership $backupfile $destination
Let me rephrase that to "How to preserve permissions of destination directory on copy?"
I can't take credit for the answer since I just combined a couple of answers I found on the wild. So here it comes.
Firstly
Permissions are generally not propagated by the directory that files are being copied into, rather new permissions are controlled by the user's umask. However when you copy a file from one location to another it's a bit of a special case where the user's umask is essentially ignored and the existing permissions on the file are preserved.
Which explains why you can't directly propagate the permissions of the src to the dst directory.
However, there is two-step workaround to this.
cp-metadata: Copy the attributes and only the attributes you want to preserve back to the source directory. Here is a quick script that can do this:
#!/bin/bash
# Filename: cp-metadata
myecho=echo
src_path="$1"
dst_path="$2"
find "$src_path" |
while read src_file; do
dst_file="$dst_path${src_file#$src_path}"
$myecho chmod --reference="$src_file" "$dst_file"
$myecho chown --reference="$src_file" "$dst_file"
$myecho touch --reference="$src_file" "$dst_file"
done
You can leave out the touch command if you don't want keep the timestamp.
Replace myecho=echo with myecho= to actually perform the commands.
Mind that this script should be run in sudo mode in order to be able
to run chown and chmod effectively
cp --preserve: After you have successfully run the first command
now it's time to copy the contents along with the attributes to the dst
directory.
--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps), if possible additional attributes: context, links, xattr, all
\cp -rfp $src_dir $dst_dir should do what you want.

Why can the `ls` command of Ubuntu list the files of a directory with no execution permission set?

Why can the ls command of Ubuntu list the files of a directory with no execution permission set ?
The Test directory has read and write permissions set but no execution permission set. I understand that the x attribute of the directory specify whether the directory can be accessed, and if it is not set then it doesn't matter whether r or w is set (please correct me if I'm wrong).
The cd and cat commands works as expected, i.e. that cannot do their job, since they cannot access the directory.
Having +r but not +x on a directory allows reading its contents, but not making it the current directory. Conversely, having +x on a directory but not +r will allow you to make it your current directory but not list it.
In other words, on a directory:
r: The read bit allows you to read the contents of that directory
w: The write bit allows you to create, rename and delete files
x: The execute bit allows you to chdir into that directory
Edit:
Apologies, after re-reading the original post, I have a better understanding of the question. The files can be listed even though there is no execute permission because you have read permission on the directory. The x bit controls access to the inode, which contains the file metadata such as permissions info. This is why the files can be listed, but no permission data is available.
https://askubuntu.com/questions/83788/accessing-files-in-a-directory-without-x-permission
See also:
https://unix.stackexchange.com/questions/21251/how-do-directory-permissions-in-linux-work

What does the mkdir -p mean in a script file?

I have found a part of script like this In xx.sh:
BUILD_BOOT=.
mkdir -p $BUILD_BOOT
Can anybody help to explain what's the script above for as the directory parameter is .?
-p is short for --parents - it creates the entire directory tree up to the given directory.
E.g., suppose there are no directories in your current directory.
If you execute:
mkdir a/b/c
It will fail, since you do not have an a subdirectory.
On the other hand
mkdir -p a/b/c
Will create the entire structure - a/b/c
mkdir -p means: create the directory and, if required, all parent directories. The fact that this makes little sense when the path is specified as ., so the current working directory, does not change this. Most likely the line where the path is defined is meant to be adapted as required.
In general: consult the linux manual pages for questions about commands and their options like this: man mkdir. A great source of information!
See mkdir.
It creates all the intermediate directories on the path to the final directory that do not already exist (as well as the final directory), and doesn't fail if the target directory already exists.
In context, it is pointless; if the BUILD_ROOT is the current directory, it already exists. At some time, the BUILD_ROOT must have been a longer path.

Resources