Linux File and Directory Management
We know that Linux's directory structure is tree-like, with the top-level directory being the root directory /
.
Other directories can be added to the tree by mounting them and removed by unmounting them.
Before starting this tutorial, we need to understand what absolute and relative paths are.
-
Absolute Path: Starts with /
, for example: /usr/share/doc
directory.
-
Relative Path: Does not start with /
, for example, to go from /usr/share/doc
to /usr/share/man
, you can write: cd ../man
. This is the relative path notation.
Common Commands for Directory Handling
Let's look at a few common commands for handling directories:
- ls (list files): Lists directory and file names.
- cd (change directory): Changes the directory.
- pwd (print work directory): Displays the current directory.
- mkdir (make directory): Creates a new directory.
- rmdir (remove directory): Deletes an empty directory.
- cp (copy file): Copies files or directories.
- rm (remove): Deletes files or directories.
- mv (move file): Moves files and directories, or renames files and directories.
You can use man [command]
to view the documentation for each command, such as man cp
.
ls (List Directory)
In the Linux system, the ls
command is probably the most commonly used.
Syntax:
[root@www ~]# ls [-aAdfFhilnrRSt] directory_name
[root@www ~]# ls [--color={never,auto,always}] directory_name
[root@www ~]# ls [--full-time] directory_name
Options and Arguments:
-a
: Lists all files, including hidden files (those starting with.
) (commonly used).-d
: Lists only the directory itself, not the files within it (commonly used).-l
: Lists detailed information, including file attributes and permissions (commonly used).
To list all files in the directory, including attributes and hidden files:
[root@www ~]# ls -al ~
cd (Change Directory)
cd
stands for Change Directory and is used to change the working directory.
Syntax:
cd [relative or absolute path]
Examples:
# Use mkdir to create the tutorialpro directory
[root@www ~]# mkdir tutorialpro
# Use an absolute path to switch to the tutorialpro directory
[root@www ~]# cd /root/tutorialpro/
# Use a relative path to switch to the tutorialpro directory
[root@www ~]# cd ./tutorialpro/
# Return to the home directory, which is /root
[root@www tutorialpro]# cd ~
# Go to the parent directory of the current directory, which is the parent of /root
[root@www ~]# cd ..
Practicing these commands several times should help you understand cd
well.
pwd (Display Current Directory)
pwd
stands for Print Working Directory and displays the current directory.
Syntax:
[root@www ~]# pwd [-P]
Options and Arguments:
-P
: Displays the actual path, not the link path.
Example: Simply display the current working directory:
[root@www ~]# pwd
/root # Displays the directory
Example: Display the actual working directory, not just the link file's directory name:
[root@www ~]# cd /var/mail # Note, /var/mail is a link file
[root@www mail]# pwd
/var/mail # Displays the current working directory
[root@www mail]# pwd -P
/var/spool/mail # What's going on? The -P option makes a big difference
[root@www mail]# ls -ld /var/mail
lrwxrwxrwx 1 root root 10 Sep 4 17:54 /var/mail -> spool/mail
# Now you know why. Because /var/mail is a link file pointing to /var/spool/mail,
# using pwd -P shows the actual full path instead of the link file's data.
mkdir (Create New Directory)
To create a new directory, use mkdir
(make directory).
Syntax:
mkdir [-mp] directory_name
Options and Arguments: This is a Chinese to English translation. Here is the English translation for the text:
-m: Permissions for the configuration file! Directly configure without having to look at the default permissions (umask).
-p: Helps you directly create the required directories (including the parent directories) recursively!
Example: Try creating several new directories under /tmp:
[root@www ~]# cd /tmp
[root@www tmp]# mkdir test <== Creates a new directory named test
[root@www tmp]# mkdir test1/test2/test3/test4
mkdir: cannot create directory `test1/test2/test3/test4':
No such file or directory <== Cannot directly create this directory!
[root@www tmp]# mkdir -p test1/test2/test3/test4
With the -p option, it can automatically create multiple levels of directories for you!
Example: Create a directory with permissions rwx--x--x.
[root@www tmp]# mkdir -m 711 test2
[root@www tmp]# ls -l
drwxr-xr-x 3 root root 4096 Jul 18 12:50 test
drwxr-xr-x 3 root root 4096 Jul 18 12:53 test1
drwx--x--x 2 root root 4096 Jul 18 12:54 test2
In the permissions section above, if the -m option is not used to force the configuration of attributes, the system will use default attributes.
If we use -m, as in the example above where we give -m 711 to set the new directory to have drwx--x--x permissions.
rmdir (Remove empty directories)
Syntax:
rmdir [-p] directory name
Options and parameters:
--p: Delete multiple levels of empty directories starting from this directory.
Remove the tutorialpro directory:
[root@www tmp]# rmdir tutorialpro/
Delete the directories created in the mkdir example (/tmp):
[root@www tmp]# ls -l <== See how many directories exist?
drwxr-xr-x 3 root root 4096 Jul 18 12:50 test
drwxr-xr-x 3 root root 4096 Jul 18 12:53 test1
drwx--x--x 2 root root 4096 Jul 18 12:54 test2
[root@www tmp]# rmdir test <== Can be deleted directly, no problem
[root@www tmp]# rmdir test1 <== Cannot be deleted because it still contains content!
rmdir: `test1': Directory not empty
[root@www tmp]# rmdir -p test1/test2/test3/test4
[root@www tmp]# ls -l <== Look, test and test1 are gone!
drwx--x--x 2 root root 4096 Jul 18 12:54 test2
Using the -p option, you can immediately delete test1/test2/test3/test4 all at once.
Note that rmdir can only delete empty directories. You can use the rm command to delete non-empty directories.
cp (Copy files or directories)
cp stands for copying files and directories.
Syntax:
[root@www ~]# cp [-adfilprsu] source file(source) destination file(destination)
[root@www ~]# cp [options] source1 source2 source3 .... directory
Options and parameters:
-
-a: Equivalent to -pdr; refer to the following explanation for pdr; (commonly used)
-
-d: If the source file is a link file attribute, copy the link file attribute instead of the file itself;
-
-f: Force, if the destination file already exists and cannot be opened, remove it and try again;
-
-i: If the destination file already exists, ask for action before overwriting; (commonly used)
-
-l: Create a hard link (hard link) instead of copying the file itself;
-
-p: Copy along with the file attributes, not using the default attributes; (commonly used for backups)
-
-r: Recursively copy, used for directory copying; (commonly used)
-
-s: Copy as a symbolic link file, i.e., a 'shortcut' file;
-
-u: Upgrade destination only if it is older than source! Copy the .bashrc file from the root directory to /tmp and rename it to bashrc using the root user.
[root@www ~]# cp ~/.bashrc /tmp/bashrc
[root@www ~]# cp -i ~/.bashrc /tmp/bashrc
cp: overwrite `/tmp/bashrc'? n <==n for no overwrite, y for overwrite
rm (Remove files or directories)
Syntax:
rm [-fir] file or directory
Options and arguments:
-f : means force, ignores non-existent files, and does not show a warning message;
-i : interactive mode, asks the user before deleting;
-r : recursive deletion, commonly used for directory deletion! This is a very dangerous option!!!
Delete the bashrc file created in the cp example:
[root@www tmp]# rm -i bashrc
rm: remove regular file `bashrc'? y
If the -i option is added, it will actively ask, preventing you from deleting the wrong filename!
mv (Move files and directories, or rename them)
Syntax:
[root@www ~]# mv [-fiu] source destination
[root@www ~]# mv [options] source1 source2 source3 .... directory
Options and arguments:
-f : force, if the target file already exists, it will not ask and will directly overwrite;
-i : if the target file (destination) already exists, it will ask whether to overwrite!
-u : if the target file already exists and the source is newer, it will update (update)
Copy a file, create a directory, and move the file into the directory:
[root@www ~]# cd /tmp
[root@www tmp]# cp ~/.bashrc bashrc
[root@www tmp]# mkdir mvtest
[root@www tmp]# mv bashrc mvtest
Move a file to a directory like this!
Rename the directory created earlier to mvtest2:
[root@www tmp]# mv mvtest mvtest2
Viewing File Contents in Linux
Use the following commands in Linux to view file contents:
cat Display file content from the first line
tac Display file content from the last line, tac is the reverse of cat!
nl Display content with line numbers!
more Display file content one page at a time
less Similar to more, but better because it allows forward paging!
head Display only the first few lines
tail Display only the last few lines
You can use man [command] to view the documentation for each command, such as: man cp.
cat
Display file content from the first line
Syntax:
cat [-AbEnTv]
Options and arguments:
-A : Equivalent to -vET, can list some special characters instead of just spaces;
-b : List line numbers, only for non-blank lines, blank lines do not have line numbers!
-E : Display the end-of-line character $;
-n : Print line numbers, including blank lines, different from the -b option;
-T : Display the [tab] key as ^I;
-v : List some invisible special characters
View the content of the /etc/issue file:
[root@www ~]# cat /etc/issue
CentOS release 6.4 (Final)
Kernel \r on an \m
tac
tac is the opposite of the cat command, displaying file content from the last line, tac is the reverse of cat! For example:
[root@www ~]# tac /etc/issue
Kernel \r on an \m
CentOS release 6.4 (Final)
nl
Display line numbers
Syntax:
nl [-bnw] file
Options and arguments:
-b : Specify the way line numbers are assigned, mainly two ways:
-n : Specify the method of line number representation, mainly three ways:
-w : The number of digits occupied by the line number column.
Example: Use nl to list the content of /etc/issue
[root@www ~]# nl /etc/issue
1 CentOS release 6.4 (Final)
2 Kernel \r on an \m
more
Page by page scrolling
[root@www ~]# more /etc/man_db.config
#
# Generated automatically from man.conf.in by the
# configure script.
#
# man.conf from man-1.6d
....(content omitted)....
--More--(28%) <== Your cursor will also be here waiting for your command
During the execution of the more
program, you have several keys you can press:
Spacebar: Scroll down one page;
Enter: Scroll down one line;
/string: Search for the keyword 'string' downwards in the displayed content;
:f: Immediately show the filename and the current line number being displayed;
q: Exit
more
immediately, no longer displaying the file content.b or [ctrl]-b: Scroll back one page, but this action only works for files, not for pipes.
less
Scroll through content one page at a time. The following example outputs the content of the /etc/man.config file:
[root@www ~]# less /etc/man.config
#
# Generated automatically from man.conf.in by the
# configure script.
#
# man.conf from man-1.6d
....(content omitted)....
: <== Here you can wait for your input command!
Commands you can input during less
execution:
Spacebar: Scroll down one page;
[pagedown]: Scroll down one page;
[pageup]: Scroll up one page;
/string: Search for the keyword 'string' downwards;
?string: Search for the keyword 'string' upwards;
n: Repeat the previous search (related to / or ?);
N: Repeat the previous search in reverse (related to / or ?);
q: Exit the
less
program;
head
Extract the first few lines of a file
Syntax:
head [-n number] file
Options and arguments:
- -n: Followed by a number, representing the number of lines to display
[root@www ~]# head /etc/man.config
By default, the first 10 lines are displayed! To display the first 20 lines, you would do this:
[root@www ~]# head -n 20 /etc/man.config
tail
Extract the last few lines of a file
Syntax:
tail [-n number] file
Options and arguments:
-n: Followed by a number, representing the number of lines to display
-f: Indicates continuous monitoring of the file specified, until [ctrl]-c is pressed to end the
tail
monitoring[root@www ~]# tail /etc/man.config # By default, the last ten lines are displayed! To display the last 20 lines, you would do this: [root@www ~]# tail -n 20 /etc/man.config