8. Linux Directory and File Management
Contents
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:
|
|

Check pwd's definition and usage
With this command, we know which directory we are currently working on:
|
|

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:
|
|

Check ls command's definition and usage
Usage of ls:
|
|

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:
|
|

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:
|
|

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:
|
|

Check mkdir command's definition and usage
Create a testd directory:
|
|

List the created directory
Create a multilevel directory:
|
|

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:
|
|

Check the rmdir command's definition and usage
We can use the rmdir command to delete the testd directory we just created:
|
|

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:
|
|
We can use the tree command to display the structure of the directory in a tree-like format.
|
|

Check tree command's definition and usage
Let’s try this command on the multilevel directory we created above:
|
|

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:
|
|

Check cd command's definition and usage
With this command, we can switch the current working directory to directory a we just created:
|
|

Change our working directory to directory a and list the contents
We can also change our working directory to a multilevel directory at once:
|
|

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:
|
|

Check touch command's definition and usage
Let’s create an empty file:
|
|

This creates an empty file
We can create multiple empty files at once:
|
|

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:
|
|

Check mv command's definition and usage
With this command, we can move the file a to the directory g:
|
|

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

Rename file b to two
Copy File or Directory
We can copy files or directories using the cp command:
|
|

Check cp command's definition and usage
We can use this command to back up the file c as c_bak:
|
|

This command backs up the file c as c_bak
We can also copy the file d intactly to the directory g:
|
|

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:
|
|

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:
|
|

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:
|
|

Check rm command's definition and usage
We can use the rm command to delete the file c:
|
|

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:
|
|

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
Author Dong Chen
LastMod Sat Feb 9 2019