Directories in Linux

Introduction: file types in Linux

In Linux everything is files and directories are no exception.

Run

ls -l $(tty)

Or a similar command

ls -l /dev/pts/1

crw--w----. 1 andrei tty 136, Nov 17 22:15 /dev/pts/1

The first character is the file type.

Seven file types are available: regular, directories, character devices, block devices, sockets, named pipes, symbolic links

- : regular file
d : directory
c : character device file
b : block device file
s : local socket file
p : named pipe
l : symbolic link

If you run

ls -ld /etc

drwxr-xr-x. 118 root root 8192 Nov 17 22:15 /etc

It can be seen that the file type is d. That is, directory

How to create a directory in Linux

mkdir dir_name

Create several directories at once

mkdir one two three

How to delete a directory in Linux

If it is empty, this will do

rmdir dir_name

If there is something in the directory, then

rm -rf dir_name

How to create a directory with a subdirectory in Linux

To create a directory with a subdirectory inside it at once, use the command mkdir with the option p (parent)

mkdir -p sites/heiheiru
ls -l sites

total 0
drwxrwxr-x. 2 andrei andrei Nov 17 22:15 heiheiru

How to copy a directory in Linux

The syntax is as follows

cp -r source destination

To copy the files directory into the sites directory with all its contents, run

cp -R files sites
ls -l sites/files/

total 0 -rw-rw-r--. 1 andrei andrei 0 Nov 17 22:15 file1 -rw-rw-r--. 1 andrei andrei 0 Nov 17 22:15 file2 -rw-rw-r--. 1 andrei andrei 0 Nov 17 22:15 file3 -rw-rw-r--. 1 andrei andrei 0 Nov 17 22:15 file4 -rw-rw-r--. 1 andrei andrei 0 Nov 17 22:15 file5

The files directory was copied into the sites directory, that is, sites now has the subdirectory files.

If you have the tree module installed, you can study the nesting clearly. If not, run first

sudo yum install tree

And then

tree sites

sites └── files ├── file1 ├── file2 ├── file3 ├── file4 └── file5 1 directory, 5 files

A link can be created with the command ln , a symbolic link with ln -s

If you need to copy the contents of one directory into another, you can do the following

cp -r source/* target/

How to change directory

With the command cd you can go to another directory

cd Documents

To return to the home directory it is enough to run cd without parameters

To repeat the previous command it is enough to press the up arrow

To return to the previous directory (the analogue of the back button in a browser), run

cd -

Create a directory with specific access permissions

The option m allows you to create a directory by specifying the required access permissions

mkdir -m 777 dir1
mkdir -m 700 dir2
ls -ld dir1 dir2

drwxrwxrwx. 2 andrei andrei Nov 17 22:15 dir1
drwx------. 2 andrei andrei Nov 17 22:15 dir2

Article author: Andrei Olegovich