You have to work really hard to look effortless!

Wechat search public number [long Coding road], together From Zero To Hero!

preface

Linux series, mainly learning in the work of the necessary Linux command and shell programming, only involving common commands and parameters. If you’re not familiar with Linux, or if you want to learn more about Linux, you can click here. This article begins by learning about commands related to file manipulation.

cd

The command format

CD catalogueCopy the code

Command description

  • Switch the current working directory of the user.

The sample

CD # go to user home directory; CD ~ # Go to the user's home directory. cd .. # return to the parent directory (if the current directory is "/", then the parent directory is" /"; .." Upper level directory); cd .. /.. # return two levels of directory;Copy the code

ls

The command format

Ls option directoryCopy the code

Command description

  • lsList, short for list, is used to display a list of targets and is a popular command in Linux.

Commonly used options

  • -a: Displays all files, including hidden files
  • -l: Displays detailed information
  • -h: displays the file size in the format of B, K, and M
  • -i: Displays the Linux ID for each file
  • -t: sorts by time
  • -d: displays directory properties

The sample

#Use multiple commands in combination
[root@VM-0-5-centos redis-6.2.4]# ls -alhit
总用量 348K
131900 drwxrwxr-x   3 root root  12K 9月  20 11:14 src
131082 drwxrwxr-x   7 root root 4.0K 6月  19 14:16 .
132726 -rw-r--r--   1 root root  92K 6月  19 14:15 my.conf
131140 drwxrwxr-x   7 root root 4.0K 6月  19 14:04 deps
131117 -rw-rw-r--   1 root root  28K 6月   1 22:03 00-RELEASENOTES
131120 -rw-rw-r--   1 root root   51 6月   1 22:03 BUGS
131123 -rw-rw-r--   1 root root 5.0K 6月   1 22:03 CONDUCT
131126 -rw-rw-r--   1 root root 3.4K 6月   1 22:03 CONTRIBUTING
131129 -rw-rw-r--   1 root root 1.5K 6月   1 22:03 COPYING

#Use -d to print directory properties instead of listing the files in them[root@VM-0-5-centos redis-6.2.4]# ls-lhd /opt drwxr-xr-x. 4 root root 4.0k 6月 19 14:03 /optCopy the code

The second column of the command output contains the string “-rw-r–r–“, which describes the permissions on the file, as shown in the following figure:

The first character indicates the file type: ‘-‘ indicates the binary file, ‘d’ indicates the directory, and ‘L’ indicates the soft link file

The following three groups indicate the permissions of the owner, owning group, and other users: R indicates that the file can be read, W indicates that the file can be written, and x indicates that the file can be executed

mkdir

The command format

Mkdir Option directoryCopy the code

Command description

  • To create a directory

Commonly used options

  • -m: sets the directory permission when creating a directory
  • -p: if the upper-layer directory to be created has not been created, the system will create the upper-layer directory together.

The sample

[root@VM-0-5-centos /]# cd home
[root@VM-0-5-centos home]# ls

#-p to create a parent directory
[root@VM-0-5-centos home]# mkdir -p hello/world
[root@VM-0-5-centos home]# ls
hello

#-m: specifies the permission when creating a vm[root@VM-0-5-centos home]# mkdir -m 700 test [root@VM-0-5-centos home]# ls -l total amount 8 drwxr-xr-x 3 root root 4096 9月 20 12:06 Hello DRWX ------ 2 root root 4096 September 20 12:07 testCopy the code

rmdir

The command format

Rmdir option directoryCopy the code

Command description

  • Deleting an Empty Directory

Commonly used options

  • -p: Deletes a specified directory and deletes the upper – layer directory of the directory if it becomes empty.

The sample

[root@VM-0-5-centos home]# ls
hello  test

#Use the -p argument to delete an empty directory
[root@VM-0-5-centos home]# rmdir -p hello/world/
[root@VM-0-5-centos home]# ls
test
Copy the code

touch

The command format

Touch options fileCopy the code

Command description

Create a new empty file

The sample

[root@VM-0-5-centos home]# touch a.txt
[root@VM-0-5-centos home]# ls
a.txt  hello  test
Copy the code

rm

The command format

Rm Options fileCopy the code

Command description

Delete a file or directory

Commonly used options

  • -f: Deletes the vm forcibly without prompting whether to confirm the deletion
  • -r: indicates recursive deletion, used when deleting a directory
  • -i: Ask the user before deleting an existing file or directory

Use the rm command with caution because it cannot be retrieved after being deleted

The sample

[root@VM-0-5-centos home]# rm test2.txt rm: Check whether to delete the plain empty file "test2.txt". y [root@VM-0-5-centos home]#Copy the code

cp

The command format

Cp Option Source file or directory Destination file or directoryCopy the code

Command description

  • Copy a source file or directory to a destination file or directory

Commonly used options

  • -r Is used to replicate directories for recursive replication
  • -p Retain the original file attributes

The sample

#1. If the -p parameter is not used, you can see that the creation time of files in the two folders is different[root@VM-0-5-centos home]# touch test1.txt [root@VM-0-5-centos home]# touch test2.txt [root@VM-0-5-centos home]# mkdir Hello [root@VM-0-5-centos home]# ls -l total 4 drwxr-xr-x 2 root root 4096 9月 20 12:31 hello -rw-r--r-- 1 root root 09 月 TXT -rw-r--r-- 1 root root 0 9月 20 12:29 test2.txt
#Wait a minute before copying[root@VM-0-5-centos home]# cp test1.txt test2.txt hello [root@VM-0-5-centos home]# ls -l hello/ total amount 0 -rw-r--r-- 1 root TXT -rw-r--r-- 1 root root 0 September 20 12:31 test2.txt
#2. If -p is used, the file attributes are retained[root@VM-0-5-centos home]# mkdir test [root@VM-0-5-centos home]# cp -p test1.txt test2.txt test [root@VM-0-5-centos -rw-r--r-- 1 root root 0 9月 20 12:29 test1.txt -rw-r--r-- 1 root root 0 9月 20 12:29 test2.txt -rw-r--r-- 1 root root 0 9月 20 12:29 test2.txt
#3. Run -r to copy the directory
[root@VM-0-5-centos home]# cp -r test hello/
[root@VM-0-5-centos home]# ls hello/
test  test1.txt  test2.txt
Copy the code

mv

The command format

Mv Source file or directory Target file or directoryCopy the code

Command description

Move the file or rename it

The sample

#rename
[root@VM-0-5-centos home]# mv test1.txt test3.txt

#Move the file to another folder
[root@VM-0-5-centos home]# mv test3.txt hello/
Copy the code

cat

The command format

Cat option fileCopy the code

Command description

Display file contents

Commonly used options

  • -n Displays the line number

The sample

[root@VM-0-5-centos home]# cat -n /etc/issue
     1	\S
     2	Kernel \r on an \m
     3
Copy the code

tac

The command format

Tac Options fileCopy the code

Command description

In contrast to CAT, file contents are displayed in reverse

The sample

[root@VM-0-5-centos home]# tac /etc/issue

Kernel \r on an \m
\S
Copy the code

more

The command format

More filenameCopy the code

Command description

Display file contents in pages

operation

You can perform the following operations on the view page:

  • Space or F: Scroll down
  • B: Turn the page up
  • Enter: a newline
  • Q or Q: Exit

The sample

[root@VM-0-5-centos home]# more /etc/services
Copy the code

less

The command format

Less filenameCopy the code

Command description

Similar to more, less can search for keywords and is more powerful than MORE

operation

You can perform the following operations on the view page:

  • Space or F: Scroll down
  • B: Turn the page up
  • Up and down keys: newline
  • Enter: a newline
  • Q or Q: Exit
  • Enter “/ content “to search for content and pressnFind the next one (n for next)

The sample

[root@VM-0-5-centos home]# less /etc/services
Copy the code

head

The command format

Head option file nameCopy the code

Command description

The first few lines of the file are displayed. By default, the first 10 lines are displayed

Commonly used options

-n: Specifies the number of lines to be displayed

The sample

[root@VM-0-5-centos home]# head -n 5 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
# Network services, Internet style
# IANA services version: last updated 2013-04-10
Copy the code

tail

The command format

Tail Option file nameCopy the code

Command description

Displays the last few lines of the file. The last 10 lines are displayed by default

Commonly used options

  • -n: Specifies the number of lines to be displayed
  • -f: dynamically displays the file content (does not exit the command and listens for new data. When new data is added to the file, it will be automatically printed out and press Control + C to exit).

The sample

[root@VM-0-5-centos home]# tail -f /var/log/messages
Copy the code

conclusion

This article introduces Linux file manipulation commands, including:

  • Create or delete files
  • Create and view files
  • Copy and cut files

More and more

Personal blog: lifelmy.github. IO /

Wechat official account: Long Coding road