Basic file system operations

Source: Internet
Author: User
Tags disk usage
Reprinted from http://linux.chinaunix.net/bbs/thread-895440-1-1.html
This document is very practical. It is not about the specific meaning of a command parameter, but about what commands are required to complete the task from the perspective of actual work.

File System operations are the most basic operations. Without a file system, the operating system cannot run at all.

Below are some of the things we often do. The specific parameter meanings below are not explained. For more information, see the help documentation for this command.

1. Create a directory

Mkdir

No1. create a level-1 directory in the current path

[Root @ rehat root] # mkdir Test

NO2. create a multi-level directory in the current path

[Root @ rehat root] # mkdir-P mytest/test1/test1_1

NO3. grant permissions to the newly created directory while creating the Directory

[Root @ rehat root] # mkdir-M 777 testmod

In this way, anyone has any permission for this directory.

2. Copy files and folders

CP

No1. copy the files in the specified directory to the current directory and rename

[Root @ rehat root] # CP ~ /. Bashrc bashrc_bak

NO2. force copy the file from the specified directory to the current directory, regardless of whether the current directory contains the file

[Root @ rehat root] # cp-f ~ /. Bashrc

NO2. copy the specified directory to the current directory

[Root @ rehat root] # cp-r/root/test.

[Root @ rehat root] # cp-r/root/test /.

The two have the same effect. When copying a directory, all the last-level directories in the source path will be copied, including the directory itself.

NO3. copy files from the specified directory to the specified directory

[Root @ rehat root] # CP ~ /. Bashrc/bak/. bashrc

No4. during replication, all the attributes of the source file are also copied. If no parameter is specified, the properties of the target file and the source file may be inconsistent.

[Root @ rehat root] # cp-~ /. Bashrc/bak/. bashrc

No5. if two folders need to be synchronized, one file is changed and the other file is changed, but ensure that the files of both files are up-to-date.

[Root @ rehat root] # cp-U/src/. bashrc/bak_src/bashrc

3. Create a link file, including hard links and soft links.

Ln

No1. create a shortcut similar to Windows

[Root @ rehat root] # ln-s test.txt _ slnk

NO2. if you want to back up a file with insufficient space, you can create a hard connection for the file. In this way, even if the original file is deleted

If the linked file is not deleted, it is still not deleted in the bucket.

[Root @ rehat root] # ln-l test.txt _ hlnk

4. delete an object

Rm

No1. delete files in the current directory

[Root @ rehat root] # rm test.txt

NO2. force delete the file in the current directory. No prompt is displayed.

[Root @ rehat root] # rm-F test.txt

NO3. force Delete the entire directory, including deleting all directories and files. administrator permissions are required.

[Root @ rehat root] # rm-r-F test

5. delete folders

Rmdir

No1. delete an empty directory

[Root @ rehat root] # rmdir emptydir

NO2. delete a multi-level empty directory

[Root @ rehat root] # rmdir-P emptydir/D1/D11

6. Mount and detach a File System

Mount/umount

No1. mount the optical drive

[Root @ rehat root] # Mount-T iso9660/dev/CDROM/mnt/CDROM

NO2. mount the optical drive, supporting Chinese Characters

[Root @ rehat root] # Mount-T iso9660-O codePage = 936, iocharset = cp936/dev/CDROM/mnt/CDROM

NO3. mount Windows partition and FAT file system

[Root @ rehat root] # Mount-T vfat/dev/hda3/mnt/CDROM

No4. mount Windows partition, NTFS file system

[Root @ rehat root] # Mount-t ntfs-O iocharset = cp936/dev/hda7/mnt/had7

No5. mount the ISO file

[Root @ rehat root] # Mount-o loop/ABC. ISO/mnt/CDROM

No6. attach a soft drive

[Root @ rehat root] # Mount/dev/fd0/mnt/floppy

No7. mount a flash disk

[Root @ rehat root] # Mount/dev/sda1/mnt/CDROM

No8. mount a folder shared by the Windows operating system

[Root @ rehat root] # Mount-T smbfs-O username = guest, password = guest // machine/path/mnt/CDROM

No9. display mounted file systems

[Root @ rehat root] # Mount

[Root @ rehat root] # Cat/etc/fstab: displays the file system automatically loaded when the system starts.

[Root @ rehat root] # Cat/etc/mtab displays the currently Loaded File System

7. Check disk space

DF

No1. displays the space usage of all storage systems and the file system type s of the storage system.

[Root @ rehat root] # DF-

NO2. display space usage of the specified file system

[Root @ rehat root] # DF-T ext3

NO3. display the size of each storage space in a user-friendly manner

[Root @ rehat root] # DF-ah

No4. sometimes a Network File System is mounted. If you only want to view the local file system, use the following command:

[Root @ rehat root] # DF-ahlt

No5. view disk usage of a file system

[Root @ rehat root] # DF-H/dev/CDROM

8. Check the directory space size.

Du

No1. view the current folder size

[Root @ rehat root] # Du-SH

NO2. view the size of the subfolders in the current file and file

[Root @ rehat root] # Du-CH

NO3. view the file size

[Root @ rehat root] # Du-H test1.txt

No4. view the size of multiple files at the same time

[Root @ rehat root] # Du-H test1.txt test2.txt

9. disk fragmentation

In Linux, there is basically no need for fragment. It will be automatically organized at intervals.

10. Create/change a File System

No1. create a file system type

[Root @ rehat root] # umount/dev/sdb1

[Root @ rehat root] # mkfs-T ext3/dev/db1

[Root @ rehat root] # Mount/dev/sdb1/practice

11. Change file or folder Permissions

Chmod

No1. set your notes to be read only by yourself.

[Root @ rehat root] # chmod go-rwx test.txt

Or

[Root @ rehat root] # chmod 700 test.txt

NO2. Modify permissions for multiple files at the same time

[Root @ rehat root] # chmod 700 test1.txt test2.txt

NO3. modify the permissions of a directory, including its subdirectories and files

[Root @ rehat root] # chmod 700-r Test

12. Change the owner of a file or folder

Chown this command can only be used by the root user

No1. change the owner of a file

[Root @ rehat root] # chown JIM: usergroup test.txt

NO2. change the owner of a directory and contain sub-Directories

[Root @ rehat root] # chown JIM: usergroup-r Test

13. View Text File Content

Cat

No1. view the file content and add a line number before each line

[Root @ rehat root] # Cat-N test.txt

NO2. view the file content and add the row number before the non-empty row

[Root @ rehat root] # Cat-B test.txt

NO3. merge the content of two files

[Root @ rehat root] # Cat test1.txt test2.txt> test_new.txt

No4. complete the content of the two files and trace back to a file

[Root @ rehat root] # Cat test1.txt test2.txt> test_total.txt

No5. clear the content of a file

[Root @ rehat root] # Cat/dev/null> test.txt

No6. create a new file

[Root @ rehat root] # Cat> new.txt press Ctrl + C to end the input

14. edit a file

VI

No1. create a file

[Root @ rehat root] # vi newfile.txt

NO2. modify an archive file

[Root @ rehat root] # vi test.txt already exists

Two working modes of NO3. VI: Command mode and edit mode

No4. after entering VI, it is in command mode. Press the insrt key to enter edit mode.

Press ESC to enter the command mode. You cannot edit the command mode. You can only enter the command

No5. Common commands in command mode

: W Save the current document

: Q: Exit VI directly.

: WQ is saved first and then exited

15. Path operations

CD pwd

No1. display current path

[Root @ rehat root] # pwd

NO2. return the user's home directory

[Root @ rehat root] # cd

NO3. change to another path

[Root @ rehat root] # cd/etc

No4. return to the upper-level directory

[Root @ rehat root] # CD ..

No5. return to the root directory

[Root @ rehat root] # cd/

16. Query files or folders

Find

No1. search for all files in the current user's home directory

[Root @ rehat root] # Find ~

NO2. grant the file owner in the current directory read and write permissions, and the users in the file group and other users have read permissions;

[Root @ rehat root] # Find.-Perm 644-exec LS-l {}/;

NO3. in order to find all common files with a length of 0 in the system and list their full paths;

[Root @ rehat root] # Find/size 0-type F-exec LS-l {}/;

No4. search for common files that were modified seven days ago in the/var/logs directory and ask them before deletion;

[Root @ rehat root] # Find/var/logs-mtime + 7-type F-OK Rm-I {}/;

No5. find all files belonging to the root group in the system;

[Root @ rehat root] # Find/-group root-exec LS-l {}/;

The no6. find command will delete the admin. log file that contains the numeric suffix since the last seven days.

[Root @ rehat root] # Find. -Name "Admin. log [0-9] [0-9] [0-9] "-atime-7-OK RM {}/;

No7. to find and sort all directories in the current File System

[Root @ rehat root] # Find.-type D | sort

No8. to find all RMT tape devices in the system

[Root @ rehat root] # Find/dev/RMT

17. display the file/folder list

Ls/Dir

No1. display all files, including hidden files starting.

[Root @ rehat root] # ls-

NO2. display file details

[Root @ rehat root] # ls-l

NO3. display information about the current directory and all subdirectories

[Root @ rehat root] # ls-rl

No4. display directories in chronological order, which is useful for finding the latest file

[Root @ rehat root] # ls-Tl

No5. sort by file size

[Root @ rehat root] # ls-SL

No6. display the file size and sort by size

[Root @ rehat root] # ls-S-l-S

18. Move or change the file/folder name

The usage of MV and CP commands is similar.

No1. if the target file already exists, back up the original directory file before moving it.

[Root @ rehat root] # mv-B test.txt Test2/

In this way, there will be two test.txt and text.txt ~ files under Test2 ~

Test.txt ~ Is the hosts file. test.txt is the new file.

NO2. if the target object already exists but you do not want to overwrite it

[Root @ rehat root] # mv-F test.txt Test2/

NO3. when both the source and target have the same file, if the source file matches the new object, the object will be moved; otherwise, the object will not be moved.

[Root @ rehat root] # mv-u test.txt Test2/

No4. change file name

[Root @ rehat root] # mv test.txt test2.txt

No5. change the directory name

[Root @ rehat root] # mv/Test2/test2_2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.