- Creating and Removing Files and Directories Under Linux
- Creating Files
- Creating Directories
- Removing Files
- Removing Directories
- Linux: How to Make a Directory Command
- mkdir command Syntax
- Examples
- How to create a new director
- How to create Directories in Linux
- How to create directories in Linux with verbose option
- Setting up permissions when creating a directory
- Setting up SELinux context with mkdir on RHEL or CentOS
- Sample mkdir demo command
- Summing up
- Getting help
- Unix — create path of folders and file
- 11 Answers 11
- How to create a file in Linux from terminal window? [closed]
- 17 Answers 17
- Create an empty file
- Create a file containing a newline and nothing else
- Write text into a file
- How to create a directory or folder
- Creating a folder in Microsoft Windows
- My Computer or Windows Explorer
- Windows desktop
- Create a new folder using a shortcut key
- Windows command line
- Creating a directory in MS-DOS and the Windows command line
- Related pages
- Create a directory with a batch file
- Creating a directory in Linux, Unix, and their variants
- Creating a folder and directory in Microsoft Windows 3.X
- File Manager
- MS-DOS
- Creating a folder in macOS X
- Desktop folder
- Create a folder in a directory
Creating and Removing Files and Directories Under Linux
Here is an absolute beginner post on creating and removing Files and Directories under Linux.
Creating Files
The touch command creates a new empty file.
You can create multiple files with the same command. If the file name or directory name already exists, the touch command updates the modification time and access time to the current date and time. You can use absolute or relative path names on the command line when creating new files.
To create an empty file named file1 in the /tmp directory, enter the following commands:
To create multiple empty files use the touch commands with the file names in one line as shown below.
Creating Directories
The mkdir command creates new directories.
Include the –p option if the directory name includes a path name. The command used with the -p option creates all of the non-existing parent directories that do not yet exist in the path to the new directory. You can use absolute or relative path names on the command line when creating new directories.
For example, create a new directory, named dir1, within the /tmp directory.
You can use the command ‘ls -ld’ to view the created directory.
To create a new directory named dir_in located inside a directory named dir_out, use the mkdir command with the -p option. The dir_out directory does not yet exist.
To create the dir1, dir2, and dir3 directories, enter the mkdir command with all the directory names in one line as shown below.
Removing Files
You can permanently remove files from the directory hierarchy with the rm command.
The rm command is a destructive command if not used with the correct option. The table describes the options that you can use with the rm command when removing files and directories.
Option | Description |
---|---|
-r | Includes the contents of a directory and the contents of all subdirectories when you remove a directory |
-i | Prevents the accidental removal of existing files or directories |
The –r option allows you to remove directories that contain files and subdirectories. The -i option prompts you for confirmation before removing any file.
– A yes response completes the removal of the file.
– A no response aborts the removal of the file.
For example, remove the file named file1 from the /tmp directory.
Lets see an example of using the -i option to delete the files.
Removing Directories
You can use the rm command with the -r option to remove directories that contain files and subdirectories.
For example, remove the dir1 directory and its content by using the rm –r command.
If you do not use the -r option with the rm command while removing directories, the following error message appears:
To interactively remove a directory and its contents, use the –i option along with the rm –r command. For example,
The rmdir command removes empty directories.
For example to remove the empty directory dir3, use the command below.
To remove a directory in which you are currently working in, you must first change to its parent directory.
Источник
Linux: How to Make a Directory Command
H ow do I make directory under Linux operating systems using the command prompt or bash shell?
You need to use the mkdir command to create new folders or directories under Linux operating systems. A directory (also known as folder in MS-Windows/macOS
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | mkdir on Linux |
Est. reading time | 3 mintues |
mkdir command Syntax
The mkdir command has the following syntax:
mkdir dirname
mkdir dirname1 dirname2
mkdir [option] dieNameHere
mkdir -p dir1/dir2/dir3
Examples
Let us see some commann useful examples.
How to create a new director
Open a terminal and then use the mkdir command to create empty directories. The following command would create a directory called foo:
$ mkdir foo
To list directories, enter:
$ ls
$ ls -l
The following command would create two directories within the current directory:
$ mkdir tom jerry
$ ls -l
How to create Directories in Linux
The -p option allows you to create parent directories as needed (if parent do not already exits). For example, you can create the following directory structure:
$ mkdir -p
/public_html/images/trip
Verify it:
ls -l
/public_html/
ls -l
/public_html/images/
ls -R -l
How to create directories in Linux with verbose option
Pass the -v as follows to display a message for each created directory:
mkdir -v dir1
ls -l
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Setting up permissions when creating a directory
To set directory mode (permission) pass the -m option as follows:
mkdir -m
The -m option is same as giving permissions using the chmod command. For examples:
mkdir data
chmod 0700 data
We can do the same with a single command and save typing time at the command-line:
mkdir -v -m 0700 data
ls -ld data
Setting up SELinux context with mkdir on RHEL or CentOS
The syntax is follows to set up system_u:object_r:httpd_sys_content_t:s0 as SELinux context for foo dir:
How to Create a Directory in Linux with mkdir Command with SELinux
Sample mkdir demo command
Animated gif 01: mkdir in action under Linux / Unix like operating systems
Summing up
The mkdir command in Linux is used to make new directories as per your needs. We create a new directory in current directory or given path:
mkdir my-dir-name-here
ls -l
Also make directories recursively which is useful for creating nested dirs on Linux. For instance:
mkdir -p path/to/dir1/dir2
Getting help
Make sure you read the following man pages:
man mkdir
man ls
mkdir —help
Option | Description | Example |
---|---|---|
-m ( —mode=MODE ) | Set file mode (as in chmod command), not a=rwx – umask. | mkdir -m 0644 sales |
-p ( —parents ) | No error if existing, make parent directories as needed. | mkdir -p one/two/three |
-v ( —verbose ) | Print a message for each created directory. | mkdir -v detla |
-Z | Set SELinux security context of each created directory to the default type. | mkdir -Z dir1 |
—context[=CTX] | Like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX. | See above |
—help | Display this help and exit. | mkdir —help |
—version | output version information and exit. | mkdir —version |
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Unix — create path of folders and file
I know you can do mkdir to create a directory and touch to create a file, but is there no way to do both operations in one go?
i.e. if I want to do the below when the folder other does not exist:
Has anyone come up with a function as a workaround for this?
11 Answers 11
Use && to combine two commands in one shell line:
Note: Previously I recommended usage of ; to separate the two commands but as pointed out by @trysis it’s probably better to use && in most situations because in case COMMAND1 fails COMMAND2 won’t be executed either. (Otherwise this might lead to issues you might not have been expecting.)
You need to make all of the parent directories first.
If you want to get creative, you can make a function:
And then use it like any other command:
Do it with /usr/bin/install:
when you don’t have a source file:
This is what I would do:
mkdir -p /my/other/path/here && touch $_/cpredthing.txt
Here, the $_ is a variable that represents the last argument to the previous command that we executed in line.
As always if you want to see what the output might be, you can test it by using the echo command, like so:
echo mkdir -p /code/temp/other/path/here && echo touch $_/cpredthing.txt
Which outputs as:
As a bonus, you could write multiple files at once using brace expansion, for example:
Источник
How to create a file in Linux from terminal window? [closed]
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year .
What’s the easiest way to create a file in Linux terminal?
17 Answers 17
Depending on what you want the file to contain:
- touch /path/to/file for an empty file
somecommand > /path/to/file for a file containing the output of some command.
nano /path/to/file or vi /path/to/file (or any other editor emacs,gedit etc )
It either opens the existing one for editing or creates & opens the empty file to enter, if it doesn’t exist
Create the file using cat
Now, just type whatever you want in the file:
CTRL-D to save and exit
There are several possible solutions:
Create an empty file
The echo version will work only if your version of echo supports the -n switch to suppress newlines. This is a non-standard addition. The other examples will all work in a POSIX shell.
Create a file containing a newline and nothing else
This is a valid «text file» because it ends in a newline.
Write text into a file
These are equivalent. The $EDITOR command assumes that you have an interactive text editor defined in the EDITOR environment variable and that you interactively enter equivalent text. The cat version presumes a literal newline after the \ and after each other line. Other than that these will all work in a POSIX shell.
Of course there are many other methods of writing and creating files, too.
Источник
How to create a directory or folder
There are multiple ways to create or add a folder, subfolder, directory, and subdirectory on a computer. The methods depend on the operating system or where the directory is being created. Select a link below for steps on how to create a directory and folder in each major operating system.
Creating a folder in Microsoft Windows
My Computer or Windows Explorer
- Open My Computer or Windows Explorer.
- Open the drive or folder where you’d like to create the new folder; for example, the C: drive. If you do not want to create a folder in the root directory, browse to the location of your choosing.
- In Windows 10 on the Home tab, click the New folder icon. In Windows 7 and earlier on the file menu bar, select File and then Folder.
In all versions of Windows, you can also right-click with your mouse on a blank portion of the folder, select New and then Folder.
If you’re using Windows 7 or earlier and do not see the file menu bar at the top of Windows Explorer, press the Alt key and it should become visible.
Windows desktop
- Navigate to the Windows desktop.
- Right-click any blank portion of the desktop.
- In the menu that appears (like that shown in the picture), click New and then Folder.
- A new folder appears. Type the name of the folder you want to use and then press Enter .
Create a new folder using a shortcut key
While in Windows Explorer, you can press Ctrl + Shift + N to create a new folder without using the mouse.
Windows command line
See the following MS-DOS and Windows command line user section for information about creating a directory in the Windows command line.
Creating a directory in MS-DOS and the Windows command line
It is more appropriate to use «directory» instead of «folder» when referring to the command line.
To create a directory in MS-DOS or the Windows command line (cmd), use the md or mkdir MS-DOS command. For example, below we are creating a new directory called «hope» in the current directory.
You can also create multiple new directories in the current directory with the md command. In the next example, we are creating three new directories, called «user1,» «user2,» and «user3,» in the current directory.
If you want to create a directory with spaces, you need to surround the directory name with quotes. In the example below, we are creating a directory called «my example directory» in the current directory.
To create a directory in the parent directory, without first moving into that directory, you can use the command below. This example moves back one directory to create the «example» directory.
To create a subdirectory in a different directory without moving it, use a command similar to the example below. This command creates a «test» directory in the hope directory.
Once a directory is created, you can use the cd command to change the directory and move into that directory.
To make a directory in another drive without moving into that drive, use a command similar to the example below. This command creates an «example» directory on the F: drive. The drive letter «f:» can be substituted from any drive letter.
Related pages
Create a directory with a batch file
A batch file is a series of commands that can be entered in the command line. Therefore, you can use any of the examples from the section above in the batch file to create a new folder.
Creating a directory in Linux, Unix, and their variants
It is more appropriate to use «directory» instead of «folder» when in a command line.
You must have the permissions to create a directory outside of your home directory.
To create a directory in Linux, Unix, or any variant, use the mkdir Linux and Unix command. For example, below we are creating a new directory called hope in the current directory.
Once the directory is created, you can use the cd command to change the directory and move into that directory.
If you want to change the permissions of a directory after its been created, use the chmod command.
Creating a folder and directory in Microsoft Windows 3.X
File Manager
- Open File Manager
- Open the folder where you’d like to place the new folder and on the top menu of File Manager, select File and then new folder.
MS-DOS
See the above MS-DOS user section for information about creating a directory in MS-DOS.
Creating a folder in macOS X
Desktop folder
- Navigate to the macOS desktop.
- Right-click (tap two fingers on the mouse) any blank space on the desktop.
- Select New Folder from the drop-down menu that appears.
- Name the folder, and then press Return .
Create a folder in a directory
- Open Finder and navigate to the directory where you’d like to create the folder.
- Click File in the upper-left corner of the screen.
- Select New Folder in the drop-down menu that appears.
- Name the folder, and then press Return .
Users may also press the shortcut key combination Command + Shift + N to create a new folder.
To create a directory in the Terminal command line, see the Linux steps.
Источник