【Linux】 - File Operations in Linux

File Locations for Linux

pwd (view the current directory)

insert image description here

cd (change directory)

Order explain
cd + path Jump to the directory of the specified path
cd Jump to main directory
cd - Jump to the last directory
cd …/ go to the parent directory
cd ~ username Change the working directory to the home directory of the user username

ls (list directory contents)

1. List the contents of the current directory

ls

insert image description here
2. List the contents of the specified directory

ls  路径

insert image description here3. List the contents of multiple directories

ls  路径1  路径2

insert image description here
4. List file attributes

ls  -l

insert image description here

  1. access to files
  2. Number of file hard links
  3. Username of the file owner
  4. The name of the user group to which the file belongs
  5. file size in bytes
  6. Date and time the file was last modified
  7. file name

Common options for the ls command

Options meaning
-a List all files (including hidden files)
-d Used in conjunction with the -l option to view directory details
-F Add a type designator (or a slash if it is a directory) after each listed name
-h Display file size in human readable form instead of bytes
-l List file details, one file per line
-r Display the results in reverse order. Normally, the ls command displays results in ascending alphabetical order
-S Sort results by file size
-t Sort by modification time

View file contents

file (view file type)

file 文件名

insert image description here

cat (outputs everything in a file at once)

cat 文件名

insert image description here

head (the contents of the first ten lines in the output file)

head 文件名

insert image description here

tail (the last ten lines of the output file)

tail 文件名

insert image description here

less (page up and down to view content)

less 文件名

less common keyboard commands

Order explain
page up back page
page down turn one page forward
up arrow one line up
down arrow next line
G Jump to the end of the text file
g Jump to the beginning of a text file
/string Look forward to the specified string
n Look forward to the next occurrence of the string that was previously specified
h Show help screen
q quit less

Manipulate files or directories

cp (copy files and directories)

1. Copy a single file or directory f1 into file or directory f2.

cp f1 f2

2. Copy multiple files or directories f1 into directory d.

cp f1 f2 f2 d

Common options for cp

Options meaning
-a Copy files and directories and their attributes, including ownership and permissions.
-i Prompt the user for confirmation before overwriting an existing file. If this option is not specified, cp will overwrite the file by default
-r Recursively copies directories and their contents. This option (or the -a option) is required when copying directories
-in When copying files from one directory to another, only those files that do not exist in the target directory or are updated files of the corresponding files in the target directory are copied
-in When copying files, informative message is displayed

mv (move or rename files and directories)

1. Move a single file or directory f1 into file or directory f2.

mv f1 f2

2. Move multiple files or directories f1 into directory d.

mv f1 f2 f2 d

mv common options

Options meaning
-i Prompt the user for confirmation before overwriting an existing file. If this option is not specified, mv will overwrite the file by default
-in Move files from one directory to another, moving only those files that do not exist in the target directory or are updated files of the corresponding files in the target directory
-in Show informative message when moving files

mkdir (create directory)

1. Create a single directory.

mkdir dir

2. Create multiple directories.

mkdir dir1 dir2 dir3

rm (remove files and directories)

rm item...

item is the name of one or more files (or directories).

rm common options

Options meaning
-i Prompt the user for confirmation before deleting an existing file. If this option is not specified, the rm command will delete the file by default
-r Recursively delete directories. That is, if the deleted directory has subdirectories, delete them as well. To delete a directory, this option must be specified
-f Force delete
-in When deleting a file, an informative message is displayed

ln (create hard and symbolic links)

  1. Hard Links
    When a hard link is created, an additional directory entry is also created for the file. Hard links cannot refer to files outside their own file system. That is, a link cannot refer to a file that is not on the same disk partition as the link. Hard links cannot refer to directories.
ln f link

f means file

  1. Symbolic Links
    Symbolic links work by creating a special type of file that contains a text pointer to a referenced file or directory. (similar to Windows shortcuts).
ln -s item link

item represents a file or directory

wildcard

wildcard match
* Match any number of characters (including 0 and 1)
? matches any single character (excluding 0)
[characters] matches any character in the character set
[!characters] matches any character that is not in the character set
[[:class:]] matches any character belonging to the specified character class
[:alpha:] matches any letter
[:alnum:] matches any letter or number
[:digit] match any number
[:lower:] matches any lowercase letter
[:upper:] matches any capital letter

Here are some examples of wildcards:

wildcard match
* All files
a* any file starting with a
a*.txt Any file that starts with a, has any number of characters in between, and ends with .txt
a??? any file starting with a followed by 3 characters
[abc]* any file starting with any of abc
a[0-9][0-9][0-9] Any file starting with a followed by 3 numbers
[[:upper:]]* Any file starting with an uppercase letter
[![:digit:]]* any file that does not start with a number
*[[:lower:]123] Any file ending with a lowercase letter or any of the numbers 1, 2, 3

Guess you like

Origin blog.csdn.net/weixin_43598687/article/details/126280245