Creating files and directories in linux

How to Create Files and Directories in Linux using «touch» and «mkdir» Commands

You can create new files and directories within the directory hierarchy using the touch and mkdir commands. The touch command creates a new, empty file, and the mkdir command creates a new directory.

Creating Empty Files

You can use the touch command to create an empty file. You can create multiple files on the same command line. 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. The syntax for the touch command is:

You can use absolute or relative path names on the command line when creating new files. To create an empty file named space in the dir3 directory, perform the touch command.

$ pwd /export/home/user1 $ cd dir3 $ ls planets $ touch space $ ls planets space 

To create three empty files named moon, sun, and cosmos in the dir3 directory, perform the touch command.

$ touch moon sun cosmos $ ls cosmos moon planets space sun $ 

Creating Directories

You can use the mkdir command with a directory_name to create directories. If the directory_name includes a path name, use the mkdir command with the -p option. 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. The syntax for the mkdir command includes:

$ mkdir directory_name $ mkdir -p directory_names 

You can use absolute or relative path names on the command line when creating new directories. To createa new directory, named Reports, within the user1 directory, use the mkdir command.

$ cd $ pwd /export/home/user1 $ mkdir Reports $ ls -ld Reports drwxr-xr-x 2 user1 staff 512 Mar 14 16:24 Reports 

To create a new directory named empty located inside a directory named newdir, perform the mkdir command with the -p option. The newdir directory does not yet exist.

$ cd $ pwd /export/home/user1 $ ls brands dir10 dir5 file1 file3 letters dante dir2 feathers file.2 file4 Reports dante_1 dir3 feathers_6 file2 fruit tutor.vi dir1 dir4 file.1 file.3 fruit2 $ mkdir -p newdir/empty $ ls newdir empty 

To create a Weekly directory in the Reports directory, perform the mkdir command.

$ mkdir Reports/Weekly $ ls Reports Weekly 

To create the dir1, dir2, and dir3 directories in the Weekly directory, perform the mkdir command.

$ cd Reports/Weekly $ mkdir dir1 dir2 dir3 $ ls -F dir1/ dir2/ dir3/ 

Источник

Читайте также:  Установка wine simply linux

Linux essentials: How to create and delete files and directories

Learn how to use the mkdir, touch, and rm commands to create files and directories, then clean them up when you’re ready.

File system

Creating and deleting files and directories are standard operations for a sysadmin. Depending on your operating system and filesystem, there may be different ways to perform these tasks. The most efficient way is to use the shell (for instance, Bash). This article assumes you already understand how to enter commands into a Linux terminal. (Read Nathan Lager’s What sysadmins need to know about using Bash if you want a refresher.)

Connect to your Linux terminal and get ready to sling some files.

[ Boost your Bash skills. Download the Bash shell scripting cheat sheet. ]

Create a directory

Before creating a new directory, use the pwd command to understand where you are in the filesystem:

I’m in the localuser’s home folder (and you’re probably in whatever user’s home directory you’ve logged in as). Checking for files with the ls command, I see that I have none:

Create a new directory as the starting point for this article’s exercises. The command to create a new directory is mkdir . Check its options and available parameters:

$ mkdir --help Usage: mkdir [OPTION]. DIRECTORY. Create the DIRECTORY(ies), if they do not already exist. Mandatory arguments to long options are mandatory for short options too. -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask -p, --parents no error if existing, make parent directories as needed -v, --verbose print a message for each created directory -Z set SELinux security context of each created directory to the default type --context[=CTX] like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX --help display this help and exit --version output version information and exit

IT Automation ebook

The basic syntax for using this command is mkdir (replace with the desired name of your directory). Before creating any directory or file, remember that most Linux filesystems are case-sensitive. That means a resource named Dir or File is different from dir or file , which is also different from DIR or FILE . Bash interprets the name exactly as you spell it.

Create a new directory called mydir , change to it with the cd command, and list its contents:

$ mkdir mydir $ file mydir mydir: directory $ ls -l drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:47 mydir $ cd mydir/ $ pwd /home/localuser/mydir $ ls -l total 0

You’ve just created a new directory, confirmed that it is indeed a directory-type object with the file command, entered the directory, and verified that it is empty. To create more than one directory simultaneously, specify the names of the new directories after mkdir with a blank space between them:

$ mkdir dir1 dir2 dir3 $ ls -l total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 [mydir]$ ls -R .: dir1 dir2 dir3 ./dir1: ./dir2: ./dir3:

You created three new empty directories.

Читайте также:  Gnome theme kali linux

To create a directory with a directory inside of it, use the -p option:

$ mkdir -p dir4/subdir1 $ ls -l total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 $ ls -lR .: total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 ./dir1: total 0 ./dir2: total 0 ./dir3: total 0 ./dir4: total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 16:57 subdir1 ./dir4/subdir1: total 0 [mydir]$ ls -l dir4/ total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 16:57 subdir1

Create files

Now that you have some directories, create some files. There are multiple ways to create files. To create files using shell redirection, refer to How to manipulate files with shell redirection and pipelines in Linux. You can also create empty files with the touch command. Here are its options and parameters:

$ touch --help Usage: touch [OPTION]. FILE. Update the access and modification times of each FILE to the current time. A FILE argument that does not exist is created empty, unless -c or -h is supplied. A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output. Mandatory arguments to long options are mandatory for short options too. -a change only the access time -c, --no-create do not create any files -d, --date=STRING parse STRING and use it instead of current time -f (ignored) -h, --no-dereference affect each symbolic link instead of any referenced file (useful only on systems that can change the timestamps of a symlink) -m change only the modification time -r, --reference=FILE use this file's times instead of current time -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time --time=WORD change the specified time: WORD is access, atime, or use: equivalent to -a WORD is modify or mtime: equivalent to -m --help display this help and exit --version output version information and exit

The basic syntax to create an empty file is touch :

$ ls dir1 dir2 dir3 dir4 $ touch file1 $ ls -l total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1

To create multiple files, type each file’s name in front of another with blank spaces separating them:

$ touch file2 file3 $ ls -l total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file2 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file3

To create an empty file inside a subdirectory, specify the full path of the desired directory before the name of the new file:

$ touch dir4/subdir1/file4 $ ls -lR .: total 0 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir1 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir2 drwxrwxr-x. 2 localuser localuser 6 Jun 9 14:57 dir3 drwxrwxr-x. 3 localuser localuser 21 Jun 9 16:57 dir4 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:31 file1 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file2 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:33 file3 ./dir1: total 0 ./dir2: total 0 ./dir3: total 0 ./dir4: total 0 drwxrwxr-x. 2 localuser localuser 19 Jun 9 17:35 subdir1 ./dir4/subdir1: total 0 -rw-rw-r--. 1 localuser localuser 0 Jun 9 17:35 file4 [mydir]$ ls dir4/subdir1/ file4

Delete files and directories

Training & certification

Now that you have created some files and directories, you can delete everything you’ve created so far.

Читайте также:  Linux add ssh public key

It can be easy to get disoriented in the terminal, which can have disastrous consequences. Use the pwd command to display exactly which part of the filesystem you’re in.

The safest way to remove files and directories is to send them to a trash bin, just as you do on your desktop. Projects such as trashy and trash-cli help you remove files from a directory without actually deleting anything until you’re ready for the data to be erased. However, these tools are not often installed by default. The standard tool to remove resources is rm .

Use the rm command when you’re sure you’re ready to erase data permanently. Unlike trash commands, there is no unremove command, so use rm judiciously.

$ ls dir3/ dir2 file3 $ rm dir3/file3 $ ls dir3/ dir2

To delete a directory and its contents, use the -r or -R option with rm :

If you’re dealing with an empty directory (such as my example dir3 , which had its contents removed), use the -d parameter to delete it:

Advanced operations

The shell makes creating files and directories easy, scriptable, and efficient. You can use special shell operations to create multiple directories at a time. Try mkdir along with seq :

Wrap up

The Linux terminal is a powerful tool. Now you know how to use it for a few basic file-management tasks. Creating and deleting files and directories are essential tasks. Tasks such as copy and move are equally important, and I’ll write about them in my next article.

Источник

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