Linux servers exist to process data efficiently, and files store data, so file management is critical. Directories manage files, so directory management is more crucial.

Let’s begin with the first one.

Print Current Working Directory

Let’s take a look at the manual for the pwd command:

1
man pwd    # pwd's manual
img

Check pwd's definition and usage

With this command, we know which directory we are currently working on:

1
pwd
img

When I logged in with root, by default, I was in the /root directory

List Directory Contents

Check out the manual for the ls command:

1
man ls
img

Check ls command's definition and usage

Usage of ls:

1
ls
img

This command lists all the files and directories under the current working directory but doesn't include the hidden files or directories

Another command that is often used is a detailed version of ls, which lists file types, permissions, owners and groups, sizes, dates:

1
ll
img

This command shows files' and directories' only

ll is an alias of ls as I mentioned earlier in this page: 7. Types of Commands - Alias

If you want to display hidden files and directories, you append the -a argument to the ll command, which is one of the commands I use most often:

1
ll -a
img

This command shows files' and directories' only

The first directory (.) is parent directory, the one below (..) is current working directory. Hidden files or directories usually start with a dot. A line starts with a hyphen (-) means that it is a file if it starts with a letter (d) indicate that it is a directory.

Make Directory

Sometimes we need to create new directories to hold files, and with the help of the mkdir command, we can do it:

1
man mkdir
img

Check mkdir command's definition and usage

Create a testd directory:

1
2
mkdir testd
ll
img

List the created directory

Create a multilevel directory:

1
mkdir -p a/b/c/d/e/f/g
img

List the created directories as tree-like format

Remove Directory

We can use the rmdir command to delete directories, but it can only be used to remove empty directories:

1
man rmdir
img

Check the rmdir command's definition and usage

We can use the rmdir command to delete the testd directory we just created:

1
rmdir testd
img

The testd directory has gone

List Tree-like Directory Contents

Because CentOS 7 doesn’t have this install this tool by default, so we need to get it manually:

1
yum install tree -y

We can use the tree command to display the structure of the directory in a tree-like format.

1
man tree
img

Check tree command's definition and usage

Let’s try this command on the multilevel directory we created above:

1
tree a
img

List the directory contents recursively in a tree-like format

Change Working Directory

We can use the cd command to switch the working directory to another directory:

1
help cd
img

Check cd command's definition and usage

With this command, we can switch the current working directory to directory a we just created:

1
cd a
img

Change our working directory to directory a and list the contents

We can also change our working directory to a multilevel directory at once:

1
2
3
cd b/c/d/e/f
pwd
ll
img

Change our working directory to multilevel directory, prints the working directory path, and list the contents

Create Empty File

We can use the touch command to create empty files:

1
man touch
img

Check touch command's definition and usage

Let’s create an empty file:

1
2
touch a
ll
img

This creates an empty file

We can create multiple empty files at once:

1
2
touch b c d
ll
img

This creates multiple empty files at once

Move File or Directory

We can use the mv command to move files or directories to other directories:

1
man mv
img

Check mv command's definition and usage

With this command, we can move the file a to the directory g:

1
2
mv a g
ll g
img

Move file a to directory g

We can also use this command to change the name of a file or directory:

1
2
mv b two
ll
img

Rename file b to two

Copy File or Directory

We can copy files or directories using the cp command:

1
man cp
img

Check cp command's definition and usage

We can use this command to back up the file c as c_bak:

1
2
cp c c_bak
ll
img

This command backs up the file c as c_bak

We can also copy the file d intactly to the directory g:

1
2
cp d g
ll
img

This command the file d in directory g

System Status of File and Directory

You can use the stat command to view system status information for files or directories:

1
man stat
img

Check stat command's definition and usage

Let’s take a look at the system status of the file d and the directory g, respectively:

1
2
3
4
stat d
ll
stat g
ll
img

Check the system status for file d and directory g

Remove File or Directory

We can use the rm command to delete files or directories:

1
man rm
img

Check rm command's definition and usage

We can use the rm command to delete the file c:

1
rm c
img

File c has been removed with my confirmation, or I can just type y instead of yes

We can also use this command to delete the directory g:

1
rm -r g    # -r argument means to recursively remove the files and directories under directory g
img

All file under the directory g included itself have been removed with my confirmation

If this directory has a lot of files and we don’t want ignore the confirmation for every file or directory, then we have to add the -f parameter, which means that the deletion is enforced and no manual approval is required.

References Linux Command Line: Files and Directory

Buy me a coffeeBuy me a coffee