Linux red hat создать файл

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.

Читайте также:  Узнать свой днс 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.

Читайте также:  Alt linux установка драйверов nvidia

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.

Источник

RootUsers

Guides, tutorials, reviews and news for System Administrators.

Create and edit text files – RHEL 8 RHCSA

Being able to create and edit text files in Red Hat Enterprise Linux (RHEL) 8 is a simple yet important task.

In these examples we will cover the Red Hat Certified System Administrator (RHCSA) objective “Create and edit text files”, both through the graphical user interface and command line.

Graphical User Interface

If your installation of Red Hat Enterprise Linux (RHEL) 8 comes with the graphical user interface (GUI) installed, the default, you can open the text editor by following these steps:

  1. Click “Activities” towards the top left hand corner of the screen.
  2. Select the icon towards the bottom from the bar on the left, allowing you to see more applications.
  3. Click on “Text Editor”

Note that this process may differ depending on what display manager your installation is using, this example was done with a fresh default installation of RHEL 8.

Читайте также:  Hp m203dn driver linux

Once opened, you’ll have access to a standard text editor where you can write content and then choose where to save the file.

Command Line

The GUI may not always be available, for instance if you’re editing a text file through SSH, so it is important to know how to create and edit text files through the command line interface (CLI).

In this example we’ll be using the ‘vi’ text editor, as it is always available by default within RHEL – perfect for the RHCSA exam. It’s not very beginner friendly unfortunately, so we’ll only be covering the basics of creating and editing text files. Here’s a great game which will help you learn and get better with Vim (vi-improved, basically a newer more modern version of vi).

To edit an existing file, simply enter ‘vi ’ which will open up the file in the vi text editor. If ‘filename’ does not exist, once you save your changes the new file will be created.

In the example below we are creating a new file called ‘testing’ in the current working directory (the home directory of ‘user’, as identified by the ‘~’ symbol at the end of the bash shell).

This is what the vi text editor looks like by default.

At this point vi is in command mode, you cannot yet start typing. To enter text, press ‘i’ to enter insert mode. Insert mode is identified by the – – INSERT – – text down the bottom left corner.

With insert mode activated, we can now edit the text file by typing normally.

Once complete, press the escape key to leave insert mode and go back to command mode. Next we want to save the file and exit vi. To do this, enter ‘:wq’ as shown down the bottom left corner of the screen. The ‘w’ means write the file, so save it, and the ‘q’ means quit vi.

You can of course just use ‘w’ only to save the file without exiting vi. You could also use ‘:q!’ to quit without saving the changes. Combining these into a ‘:wq!’ will save the changes, overwrite the files even if it’s read only if the user account has the privileges and exit vi.

Finally we can check that our new file exists and contains the contents expected.

That’s it, you should now have a basic understanding of how to create and edit text files in RHEL 8 for the RHCSA exam. Make sure you practice these examples yourself to get a full understanding on how the commands work, new Linux users often have trouble escaping vi!

This post is part of our Red Hat Certified System Administrator (RHCSA) exam study guide series for Red Hat Enterprise Linux (RHEL) 8. For more RHCSA related posts and information, see our full RHCSA 8 study guide.

Share this:

Источник

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