How to create file and directory in linux

Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.

For users more familiar with a graphical user interface (GUI), the Linux command line interface can initially appear daunting. It is not always apparent how to perform common file and directory operations. Fortunately, Linux commands are very powerful and flexible, and with some practice, you can accomplish any file management operation you want. This guide explains how to navigate the Linux file structure and how to perform common management operations on files and directories.

An Introduction to the Linux File System

Linux has a directory-based structure. Files are stored within a particular directory, which is also referred to as a folder. Each directory can also contain other directories. These directories can in turn contain files and directories.

Conceptually, this can be thought of in terms of a tree structure. At the trunk of the tree is the / directory, also known as the root directory. All other directories branch off of the root directory. The root file system contains all the files and directories which are on the same disk partition as the root directory. All the files required to boot the system must be contained inside the root file system. Other file systems can be mounted as subdirectories within the root file system.

Every Linux prompt is actually a process that is associated with a location that maps to a directory within the file system. The directory a user is currently working in is known as the current working directory or present working directory (PWD). The name of this directory can be retrieved using the pwd command. It is possible to navigate from this point through the file system using either absolute or relative path names.

The root directory of most Linux file systems includes several standard, well-defined directories. The most important of these directories include the following:

  • /bin: contains user-executable programs and files.
  • /boot: contains the bootloader and the files required to boot the computer.
  • /etc: stores configuration files for the system.
  • /home: contains the home directories for the various user accounts. Each user has a subdirectory located at /home/userid . While a user is logged in, their home directory is aliased as the ~ directory.
  • /lib: shared system library files are located in this directory.
  • /root: this is the home directory for the root user. This is not the same as the root, or / directory.
  • /usr: contains shared binary, library, and documentation files for all users.
  • /var: data files are typically stored here.
Читайте также:  Best linux home pc

How to List Files in Linux

The ls command provides a way to list files within a directory. However, many options allow for output filtering or to change what information to display.

The ls Command

  1. To display all files and directories except hidden files, use the ls command. Specify the relative or absolute path of a directory to list files in that directory. If no directory is specified, the contents of the current directory are displayed.
cities.txt countries.txt payroll states.txt states2.txt 
accounts/ backup/ mysqlbackup/ phpcomposer/ wpbackup/ 
-rw-rw-r-- 1 userid userid 29 Aug 31 14:51 cities.txt -rw-rw-r-- 1 userid userid 42 Aug 31 14:51 countries.txt drwxrwxr-x 2 userid userid 4096 Sep 2 16:51 payroll -rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states.txt -rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states2.txt 

The . directory refers to the current working directory. The .. entry is a hard link to the parent directory. This is the directory containing the current directory.

drwxrwxr-x 3 userid userid 4096 Sep 3 10:56 . drwxr-xr-x 10 userid userid 4096 Sep 1 08:48 .. -rw-rw-r-- 1 userid userid 51 Sep 3 10:56 .states2.txt -rw-rw-r-- 1 userid userid 29 Aug 31 14:51 cities.txt -rw-rw-r-- 1 userid userid 42 Aug 31 14:51 countries.txt drwxrwxr-x 2 userid userid 4096 Sep 2 16:51 payroll -rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states.txt -rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states2.txt 
-rw-rw-r-- 1 userid userid 51 Sep 3 10:56 .states2.txt -rw-rw-r-- 1 userid userid 29 Aug 31 14:51 cities.txt -rw-rw-r-- 1 userid userid 42 Aug 31 14:51 countries.txt drwxrwxr-x 2 userid userid 4096 Sep 2 16:51 payroll -rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states.txt -rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states2.txt 
-rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states2.txt -rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states.txt drwxrwxr-x 2 userid userid 4096 Sep 2 16:51 payroll -rw-rw-r-- 1 userid userid 42 Aug 31 14:51 countries.txt -rw-rw-r-- 1 userid userid 29 Aug 31 14:51 cities.txt 
drwxrwxr-x 2 userid userid 4096 Sep 2 16:51 payroll -rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states.txt -rw-rw-r-- 1 userid userid 51 Aug 31 14:51 states2.txt -rw-rw-r-- 1 userid userid 42 Aug 31 14:51 countries.txt -rw-rw-r-- 1 userid userid 29 Aug 31 14:51 cities.txt 
drwxrwxr-x 3 userid userid 4096 Sep 2 16:51 accounts drwxrwxr-x 2 userid userid 4096 Sep 1 08:47 backup -rw-rw-r-- 1 userid userid 42 Aug 31 14:50 countries.txt -rw-rw-r-- 1 userid userid 51 Aug 31 14:43 states.txt drwxrwxr-x 3 userid userid 4096 Aug 24 17:08 phpcomposer drwxrwxr-x 2 userid userid 4096 Jul 21 13:24 mysqlbackup drwxrwxr-x 3 userid userid 4096 Jul 21 12:06 wpbackup -rw-r--r-- 1 userid userid 196 Jul 20 14:42 testconnection2.php 
drwxrwxr-x 2 userid userid 4096 Sep 1 08:47 backup drwxrwxr-x 3 userid userid 4096 Sep 3 10:56 accounts drwxrwxr-x 3 userid userid 4096 Jul 21 12:06 wpbackup drwxrwxr-x 2 userid userid 4096 Jul 21 13:24 mysqlbackup -rw-r--r-- 1 userid userid 196 Jul 20 14:42 testconnection2.php -rw-rw-r-- 1 userid userid 51 Aug 31 14:43 states.txt -rw-rw-r-- 1 userid userid 42 Aug 31 14:50 countries.txt drwxrwxr-x 3 userid userid 4096 Aug 24 17:08 phpcomposer 

The ls command has many more options. Consult the Linux manual page for ls, also known as the command man page, for complete information.

Читайте также:  Run cron in linux

How to Change Directories in Linux

It is possible to navigate the Linux file system in a relative or absolute manner. An absolute path indicates the location of a directory from the root directory, and always begins with the / symbol. A relative path is defined in relation to the current working directory.

The cd command is used to change directories. This command is very straightforward to use, but it does have a few useful shortcuts. More information about the cd command can be found on the Linux man page.

The cd Command

  1. While relatively navigating the Linux file system, it is often useful to know the path to the current directory. To display the full pathname of the present working directory, use the pwd command.

Most Linux distributions offer an auto-complete function. While typing the name of a file or directory, press the tab key. If the name is unambiguous, the system automatically completes the rest of the name. If there are multiple potential options, the system either lists all possible choices or auto-completes the characters common to all alternatives.

Источник

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:

Читайте также:  Linux single user mode password

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/ 

Источник

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