Easy Tutorial
❮ Linux Comm Swapon Linux Comm Tftp ❯

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

head

Extract the first few lines of a file

Syntax:

head [-n number] file

Options and arguments:

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:

❮ Linux Comm Swapon Linux Comm Tftp ❯