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:
|
|
With this command, we know which directory we are currently working on:
|
|
List Directory Contents
Check out the manual for the ls command:
|
|
Usage of ls:
|
|
Another command that is often used is a detailed version of ls, which lists file types, permissions, owners and groups, sizes, dates:
|
|
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:
|
|
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:
|
|
Create a testd directory:
|
|
Create a multilevel directory:
|
|
Remove Directory
We can use the rmdir command to delete directories, but it can only be used to remove empty directories:
|
|
We can use the rmdir command to delete the testd directory we just created:
|
|
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.
|
|
Let’s try this command on the multilevel directory we created above:
|
|
Change Working Directory
We can use the cd command to switch the working directory to another directory:
|
|
With this command, we can switch the current working directory to directory a we just created:
|
|
We can also change our working directory to a multilevel directory at once:
|
|
Create Empty File
We can use the touch command to create empty files:
|
|
Let’s create an empty file:
|
|
We can create multiple empty files at once:
|
|
Move File or Directory
We can use the mv command to move files or directories to other directories:
|
|
With this command, we can move the file a to the directory g:
|
|
We can also use this command to change the name of a file or directory:
|
|
Copy File or Directory
We can copy files or directories using the cp command:
|
|
We can use this command to back up the file c as c_bak:
|
|
We can also copy the file d intactly to the directory g:
|
|
System Status of File and Directory
You can use the stat command to view system status information for files or directories:
|
|
Let’s take a look at the system status of the file d and the directory g, respectively:
|
|
Remove File or Directory
We can use the rm command to delete files or directories:
|
|
We can use the rm command to delete the file c:
|
|
We can also use this command to delete the directory g:
|
|
If this directory has a lot of files and we don’t want make 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