Sub directory in linux

Creating multiple nested directories with one command

@user3142695 End of options (e.g. -s / —some-thing ) and only (positional) arguments from now on. See also unix.stackexchange.com/q/11376/117599 It’s not strictly necessary here, I just added it to signify further that those are multiple positional arguments.

To add to the above answers you can also do (in csh , tcsh , ksh , bash , zsh , fish , yash -o brace-expand ):

On bash and similar shells supporting that particular feature (brace expansion) that is. For more information, see wiki.bash-hackers.org/syntax/expansion/brace

@phk, you mean in csh and similar shells supporting that particular feature (that comes from csh (late 70s)).

@StéphaneChazelas Oops. BTW, does something like caniuse.com for shells exist? (BTW, I should have added to my previous comment «For more information on its implementation in bash ,»)

Reading the man page is always a good place to start.

The -p flag will create the required intermediate directories on the path.

You know even though it doesn’t ‘technically answer’ the particular it’s a very good point; you have to read the man pages as well as do — and type it out yourself helps you memorise it — if you really want to learn. Otherwise it’s just a custom can of scripts for you (and in that case it may very well be a can of worms).

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.12.43529

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to create a new subdirectory with a single command on Linux

What command will create a new subdirectory? For example I would like to create a new subdirectory called TEMP of a parent directory /tmp/.

Answer:

Creating directories on a linux system is done by use of mkdir command. Please note that Linux shell is case sensitive, therefore, temp and TEMP are two distinct directories. Below you can find a basic usage of the mkdir command. Visit the following link for more advanced mkdir command usage.

Читайте также:  Завершение работы линукс через терминал

Let’s start be creating a single directory within a current working directory:

The command above will create a directory called TEMP within your current working directory. The following linux command will create test directory inside /tmp/ . Here the command assumes that /tmp/ directory already exists:

The last command example will create a new TEMP directory as a subdirectory of /tmp/test even if test does not exists.

The above -p will instruct mkdir directory to create any parent directory if it does not exist. Executing mkdir command without -p option will result in error message:

$ mkdir: cannot create directory ‘/tmp/test/TEMP’: No such file or directory

Comments and Discussions

NEWSLETTER

Subscribe to Linux Career Newsletter to receive latest news, jobs, career advice and featured configuration tutorials.

WRITE FOR US

LinuxConfig is looking for a technical writer(s) geared towards GNU/Linux and FLOSS technologies. Your articles will feature various GNU/Linux configuration tutorials and FLOSS technologies used in combination with GNU/Linux operating system.

When writing your articles you will be expected to be able to keep up with a technological advancement regarding the above mentioned technical area of expertise. You will work independently and be able to produce at minimum 2 technical articles a month.

TAGS

  • VIM tutorial for beginners
  • How to install the NVIDIA drivers on Ubuntu 20.04 Focal Fossa Linux
  • Bash Scripting Tutorial for Beginners
  • How to check CentOS version
  • How to find my IP address on Ubuntu 20.04 Focal Fossa Linux
  • Ubuntu 20.04 Remote Desktop Access from Windows 10
  • Howto mount USB drive in Linux
  • How to install missing ifconfig command on Debian Linux
  • AMD Radeon Ubuntu 20.04 Driver Installation
  • Ubuntu Static IP configuration
  • How to use bash array in a shell script
  • Linux IP forwarding – How to Disable/Enable
  • How to install Tweak Tool on Ubuntu 20.04 LTS Focal Fossa Linux
  • How to enable/disable firewall on Ubuntu 18.04 Bionic Beaver Linux
  • Netplan static IP on Ubuntu configuration
  • How to change from default to alternative Python version on Debian Linux
  • Set Kali root password and enable root login
  • How to Install Adobe Acrobat Reader on Ubuntu 20.04 Focal Fossa Linux
  • How to install the NVIDIA drivers on Ubuntu 18.04 Bionic Beaver Linux
  • How to check NVIDIA driver version on your Linux system
  • Nvidia RTX 3080 Ethereum Hashrate and Mining Overclock settings on HiveOS Linux

LATEST TUTORIALS

  • Easy Steps to Update Your Raspberry Pi
  • Connecting Your Raspberry Pi to Wi-Fi: A How-To
  • How to install RealVNC viewer on Linux
  • How to check Raspberry Pi RAM size and usage
  • How to check Raspberry Pi model
  • Understanding UEFI and BIOS in Relation to Linux Nvidia Driver Installation
  • How to orchestrate Borg backups with Borgmatic
  • How to monitor filesystem events on files and directories on Linux
  • Debian USB Firmware Loader Script
  • How to install and self host an Ntfy server on Linux
  • How to backup your git repositories with gickup
  • How to bind an SSH public key to a specific command
  • Creating a Bootable USB for Windows 10 and 11 on Linux
  • How to list all displays on Linux
  • List of QR code generators on Linux
  • How to extract text from image
  • Linux EOF explained
  • How to use xclip on Linux
  • List of window managers on Linux
  • What is zombie process on Linux
Читайте также:  Linux distributed file system

Источник

Making Nested Directories in Linux

Learn to make nested directories so that if a directory in the given path doesn’t exist, it is created automatically.

Creating nested directories when the parent directory does not exist is. impossible. But that also does not mean that you need to create the parent directory first and invoke mkdir again.

There is a faster way to achieve this; you can use the -p flag with mkdir command.

mkdir -p parent_dir/child_dir/nested_child_dir

Using the -p flag will let mkdir know that it is okay to make a parent directory if it does not exist yet, and then create a nested child directory.

mkdir syntax

The method for creating directories in UNIX-like systems is to use the mkdir command.

Below is the syntax of mkdir:

Create nested directories with the ‘-p’ flag

Just as an example, I want to create a directory structure where the parent directory’s name is ‘songs’ and the child directory’s name is ‘artists.’

The typical way to achieve this is by the following set of commands:

$ mkdir -v songs mkdir: created directory 'songs' $ mkdir -v songs/artists mkdir: created directory 'songs/artists' # I only included the '-v' flag for verbosity, you need not use it

This can be achieved in a single line like so:

$ mkdir -p -v songs/artists mkdir: created directory 'songs' mkdir: created directory 'songs/artists'

But. you said, «it is impossible.» Technically, you can not create a child directory if the parent does not exist. Yes, that still holds true.

So, let us understand what happened here.

The mkdir command has a ‘-p’ flag, and its descriptor is «no error if existing, make parent directories as needed». The second part of that statement says that mkdir will create a parent directory if the need be.

If you look at the output of mkdir in the second example, you will notice that mkdir created the ‘songs’ directory first and then created ‘artists’ under the ‘songs’ directory.

This indicates that mkdir will first make the necessary parent directories and then the child directory if you use the ‘-p’ flag.

Create multiple child directories under a not-yet-existing parent directory

Let’s try to create multiple child directories when the parent directory does not exist.

There are two ways to achieve this. Have a look:

$ mkdir -vp songs/artists songs/albums songs/genres songs/genres/classical songs/genres/pop mkdir: created directory 'songs' mkdir: created directory 'songs/artists' mkdir: created directory 'songs/albums' mkdir: created directory 'songs/genres' mkdir: created directory 'songs/genres/classical' mkdir: created directory 'songs/genres/pop'

Or, you can use the following command:

$ mkdir -vp songs/> mkdir: created directory 'songs' mkdir: created directory 'songs/artists' mkdir: created directory 'songs/albums' mkdir: created directory 'songs/genres' mkdir: created directory 'songs/genres/classical' mkdir: created directory 'songs/genres/pop'

As you can see, both commands have the same output, but the latter one is faster to type.

Читайте также:  Linux create directory in root

The ‘<>‘ curly brackets are used to specify a list, in this case, a list of directory names.

In this example, the parent directory ‘songs’ does not exist, so it was created. Then, the child directories ‘artists’, ‘albums’, and ‘genres’ are created. The directories ‘classical’ and ‘pop’ are listed under genres, so these two are the child directory of ‘genres,’ and they are created as I expected.

Conclusion

You can learn more about the mkdir command in the article below.

So, now you know how you can create a child directory when a parent directory does not exist — without creating each directory manually.

Источник

Subdirectory

Illustration of a directory with several subdirectories.

Computer Hope

In a computer file system, a subdirectory is a directory that is contained another directory, called a parent directory. A parent directory may have multiple subdirectories.

In operating systems with a GUI (graphical user interface) such as Microsoft Windows, a directory is called a folder, and a subdirectory is called a subfolder.

Subdirectory

Every directory except for the top-level root directory is a subdirectory of at least one directory. The list of parent directories of a directory or file is called its path.

In Windows

The following is an example of a file’s path, including its parent directories, in Microsoft Windows.

In Linux

In the following example path in a Linux operating system, public_html is a subdirectory of computerhope, a subdirectory of c, etc. The special root folder / is the only directory that is not a subdirectory, because it has no parent directory.

/home/users/c/computerhope/public_html/

Subfolder

Like a subdirectory, a subfolder is any folder that is a child of another folder. In the example below, Fonts and System32 are subfolders of the Windows folder.

How do I create a subdirectory or subfolder?

You can create a subfolder from Microsoft Windows or another GUI by opening the folder containing the subfolder and creating a new folder. For example, if you had a folder called «Games» and you wanted to create a subfolder named «Doom» in the Games folder, move into the Games folder and then create a new folder called «Doom.»

To create a subdirectory from a command line, change to the directory that will be holding the subdirectory and create a new one (similar to above example). Alternatively, you could create a subdirectory in another directory using the full path. For example, using the Windows command line mkdir command, you could create the subdirectory «doom» in the «games» directory by typing «mkdir games\doom» at the command line.

Источник

Оцените статью
Adblock
detector