Summary of Linux file directory management commands

Summary of Linux file directory management commands

touch Command

It has two functions: one is to update the time tags of existing files to the current system time (default method), and their data will be retained intact; the other is to create new empty files.

Options:

  • -a: or --time=atime or --time=access or --time=use only changes the access time;
  • -c: or --no-create does not create any files;
  • -d: <time date> Use the specified date time instead of the current time;
  • -f: This parameter will be ignored and is only used to solve the compatibility problem of the BSD version of the touch command;
  • -m: or --time=mtime or --time=modify only changes the time;
  • -r: <reference file or directory> Set the date and time of the specified file or directory to the same as the date and time of the reference file or directory;
  • -t: <date time> Use the specified date time instead of the current time;

mkdir Command

Create a Directory

Options:

  • -Z: Set the security context, which is effective when SELinux is used;
  • -m<target attribute> or --mode<target attribute> sets the directory permissions while creating the directory;
  • -p or --parents If the parent directory of the directory to be created has not been created yet, the parent directory will be created at the same time;

parameter:

Directory: Specify the directory list to be created. Multiple directories are separated by spaces.

cp command

Used to copy one or more source files or directories to a specified destination file or directory.

Options:

  • -a: The effect of this parameter is the same as specifying the "-dpR" parameter at the same time;
  • -d: When copying a symbolic link, the target file or directory is also created as a symbolic link, and points to the original file or directory connected to the source file or directory;
  • -f: Force copying of files or directories, regardless of whether the target file or directory already exists;
  • -i: Ask the user before overwriting existing files;
  • -l: Create a hard link to the source file instead of copying the file;
  • -p: retain the attributes of the source file or directory;
  • -R/r: recursive processing, processing all files and subdirectories under the specified directory together;
  • -s: Create a symbolic link to the source file instead of copying the file;
  • -u: With this parameter, the file will be copied only when the modification time of the source file is newer than that of the target file or when the target file with the corresponding name does not exist;
  • -S: When backing up files, use the specified suffix "SUFFIX" instead of the default suffix of the file;
  • -b: Back up the target file before overwriting the existing file target;
  • -v: Displays detailed information about the operations performed by the command.

parameter:

Source Files: Create a list of source files. By default, the cp command cannot copy directories. If you want to copy directories, you must use the -R option.

Target file: Specify the target file. When the "source file" is multiple files, the "target file" is required to be a specified directory.

Note: All directories specified by the target files must already exist. The cp command cannot create directories.

Examples:

When we use the cp command to copy files in Linux, sometimes we need to overwrite some files with the same name. When overwriting files, there will be a prompt: you need to keep pressing Y to confirm the overwrite. It's OK if there are not many files, but if there are hundreds of them, I'm afraid I'll vomit blood when I press Y, so I summed up a method after a long time:

cp aaa/* /bbb

Copy all the files in directory aaa to directory /bbb. If there is a file with the same name as aaa in directory /bbb, press Y to confirm and the subdirectories in directory aaa will be skipped.

cp -r aaa/* /bbb
This time you still need to press Y to confirm the operation, but the subdirectories are not ignored.

cp -r -a aaa/* /bbb
You still need to press Y to confirm the operation, and the aaa directory and its subdirectories and file attributes are also transferred to /bbb.

\cp -r -a aaa/* /bbb
Success, no prompt to press Y, directory attributes passed, no directories skipped.

mv Command

Used to rename a file or directory, or to move a file from one directory to another.

Options:

  • --backup=<backup mode>: If you need to overwrite the file, back it up before overwriting;
  • -b: When the file exists, create a backup for it before overwriting it;
  • -f: If the target file or directory is the same as an existing file or directory, the existing file or directory will be overwritten directly;
  • -i: Interactive operation. Ask the user before overwriting. If the source file has the same name as the target file or a file in the target directory, ask the user whether to overwrite the target file. If the user enters "y", it means that the target file will be overwritten; if the user enters "n", it means that the move of the source file will be canceled. This will prevent files from being accidentally overwritten.
  • --strip-trailing-slashes: Remove slashes "/" from source files;
  • -S<suffix>: specify a suffix for the backup file instead of using the default suffix;
  • --target-directory=<directory>: specifies the target directory to which the source files are to be moved;
  • -u: The move operation is performed only when the source file is newer than the target file or the target file does not exist.

Examples:

Move all files in the directory /usr/men to the current directory (indicated by .):

mv /usr/men/* .

rm Command

Delete one or more files or directories in a directory, or delete a directory and all the files and subdirectories under it. For linked files, just delete the entire linked file and keep the original file unchanged.

Options:

  • -d: directly delete the hard link data of the directory to be deleted to 0 and delete the directory;
  • -f: force deletion of files or directories;
  • -i: Ask the user before deleting existing files or directories;
  • -r or -R: recursive processing, processing all files and subdirectories under the specified directory together;
  • --preserve-root: Do not recurse the root directory;
  • -v: Display the detailed execution process of the command

dd command

Copies a file and converts and formats the contents of the original file. It is recommended to use dd to operate on physical disks when necessary. When using dd to operate on disks, it is best to use block device files.

Options:

  • bs=<number of bytes>: Set ibs (input) and obs (output) to the specified number of bytes;
  • cbs=<number of bytes>: when converting, only the specified number of bytes are converted each time;
  • conv=<keyword>: specifies the file conversion method;
  • count=<number of blocks>: read only the specified number of blocks;
  • ibs=<number of bytes>: the number of bytes read each time;
  • obs=<number of bytes>: the number of bytes output each time;
  • of=<file>: output to file;
  • seek=<number of blocks>: skip the specified number of blocks at the beginning of output;
  • skip=<number of blocks>: skip the specified number of blocks when reading at the beginning;

Examples:

dd if=/dev/zero of=sun.txt bs=1M count=1

This command creates a 1M file sun.txt, where the parameters are explained as follows:

  • if represents the input file. If if is not specified, input is read from stdin by default.
  • of represents the output file. If of is not specified, stdout will be used as the default output.
  • bs stands for the block size in bytes.
  • count represents the number of blocks copied.
  • /dev/zero is a character device that will always return bytes with value 0 (\0).

file command

Used to detect the type of a given file. The file command's file check is divided into three steps: file system check, magic number check, and language check.

Options:

  • -f<name file>: specifies a name file. When its content contains one or more file names, let file identify these files in sequence, with one file name per column;
  • -b: When listing the identification results, do not display the file name;
  • -c: Displays the instruction execution process in detail, which is convenient for debugging or analyzing the program execution situation;
  • -L: Directly display the file category pointed to by the symbolic link;
  • -m<magic number file>: specify the magic number file;
  • -z: Try to decrypt the contents of compressed files.

parameter:

File: To determine the type of file list, multiple files are separated by spaces, and shell wildcards can be used to match multiple files.

Examples:

Display file types

[root@localhost ~]# file install.log
install.log: UTF-8 Unicode text

[root@localhost ~]# file -b install.log <== Do not display file name UTF-8 Unicode text

[root@localhost ~]# file -i install.log <== Display MIME categories.
install.log: text/plain; charset=utf-8

[root@localhost ~]# file -b -i install.log
text/plain; charset=utf-8

Display the file type of a symbolic link

[root@localhost ~]# ls -l /var/mail
lrwxrwxrwx 1 root root 10 08-13 00:11 /var/mail -> spool/mail

[root@localhost ~]# file /var/mail
/var/mail: symbolic link to `spool/mail'

[root@localhost ~]# file -L /var/mail
/var/mail:directory

[root@localhost ~]# file /var/spool/mail
/var/spool/mail:directory

[root@localhost ~]# file -L /var/spool/mail
/var/spool/mail:directory

The above is all the knowledge points introduced this time. Thank you for your learning and support for 123WORDPRESS.COM.

You may also be interested in:
  • A brief analysis of common Linux file management commands
  • Detailed explanation of Linux file management
  • Sharing of Linux operating system file manager
  • Linux file and user management practice
  • Linux file/directory permissions and ownership management
  • Detailed steps for Linux account file control management
  • Some Linux file permission management methods you may not know
  • Detailed explanation of Linux file permissions and directory management
  • Linux du command to view folder sizes and sort in descending order
  • How to retrieve file contents using grep command in Linux
  • Detailed application of command get to download files and put to upload files in Linux ftp command line
  • Linux commands to delete folders and files (forced deletion including non-empty files)
  • Linux file management command example analysis [permissions, create, delete, copy, move, search, etc.]

<<:  MySQL slow query optimization: the advantages of limit from theory and practice

>>:  Reasons and solutions for slow MySQL query stuck in sending data

Recommend

Vue implements time countdown function

This article example shares the specific code of ...

A mobile adaptive web page effect solves the problem of small display page

For work needs, I need to make a mobile phone adap...

Echarts Bar horizontal bar chart example code

Table of contents Horizontal bar chart Dynamicall...

Method of iframe adaptation in web responsive layout

Problem <br />In responsive layout, we shou...

Eight implementation solutions for cross-domain js front-end

Table of contents 1. jsonp cross-domain 2. docume...

CSS3+HTML5+JS realizes the shrinking and expanding animation effect of a block

When I was working on a project recently, I found...

Echarts implements switching different X-axes in one graph (example code)

Rendering If you want to achieve the effect shown...

Alibaba Cloud Server Domain Name Resolution Steps (Tutorial for Beginners)

For novices who have just started to build a webs...

JavaScript implements AI tic-tac-toe game through the maximum and minimum algorithm

Without further ado, let’s run the screenshot dir...

Use crontab to run the script of executing jar program regularly in centOS6

1. Write a simple Java program public class tests...

Common commands for mysql authorization, startup, and service startup

1. Four startup methods: 1.mysqld Start mysql ser...

CSS code to distinguish ie8/ie9/ie10/ie11 chrome firefox

Website compatibility debugging is really annoyin...