Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic file operation commands in RHEL6

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the RHEL6 file basic operation commands which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

File (folder) View class command

Ls-- displays the contents of the specified directory

Description: ls displays the results in different colors to distinguish between file categories. Blue represents a directory, gray represents a normal file, green represents an executable file, red represents a compressed file, and light blue represents a linked file.

-a Murray-shows everything, including hidden files

Description: in the Linux system, with "." The beginning is to hide the file or the directory.

-lMusure-displays the details of a file or directory in a long format (more detailed content).

Description: the ls-l command can be simplified to ll

The output information is divided into seven groups:

File category and file permissions, number of links or subdirectories, file owner, group to which the file belongs, file size in bytes B, file creation or modification time, file name.

File category: the first bit of the first group represents the file category, "-" represents the ordinary file, "d" represents the directory, "l" represents the symbolic link, "c" represents the character device, and "b" represents the block device.

File permissions: the last 9 bits of the first group represent file permissions, the first 3 bits are user, the middle 3 bits are group, and the last 3 bits are other permissions

-dmurmuri-displays the properties of the directory itself instead of the contents of the directory.

[root@localhost] # ls-ld / homedrwxr-xr-x. 4 root root 4096 September 22 10:41 / home [root@localhost ~] # ls-d / home/home [root@localhost ~] #

-hmurmuri-displays the file size in K, M, G, etc. (default is bytes)

[root@localhost] # ls-h / homejustin lost+found [root@localhost] # ls-lh / home total dosage 20KdrwxMurray. 27 justin justin 4.0K September 22 13:19 justindrwx-. 2 root root 16K September 18 15:30 lost+found [root@localhost ~] #

-Rmurmuri-if there are files in the directory, they will also be listed in sequence.

[root@localhost] # ls-lR / home/home: the total dosage is 20drwxmuri. 27 justin justin 4096 September 22 13:19 justindrwx-. 2 root root 16384 September 18 15:30 lost+found/home/justin: total dosage 32drwxr-xr-x. 2 justin justin 4096 September 22 10:49 Public drwxr-xr-x. 2 justin justin 4096 September 22 10:49 template drwxr-xr-x. 2 justin justin 4096 September 22 10:49 video drwxr-xr-x. 2 justin justin 4096 September 22 10:49 picture drwxr-xr-x. 2 justin justin 4096 September 22 10:49 document drwxr-xr-x. 2 justin justin 4096 September 22 10:49 download drwxr-xr-x. 2 justin justin 4096 September 22 10:49 Music drwxr-xr-x. 2 justin justin 4096 September 22 10:49 Desktop / home/justin/ Public: total dosage 0/home/justin/ template: total dosage 0/home/justin/ Video: total dosage 0/home/justin/ Picture: total dosage 0/home/justin/ document: total dosage 0/home/justin/ download: total dosage 0/home/justin/ Music: total dosage 0/home/justin/ Desktop: total dosage 0/home/lost+found: total dosage 0 [root@localhost ~] #

-tmurmuri-list the files in the order in which they were created

The total amount of [root@localhost] # ls-l / home is 20drwxMel. 27 justin justin 4096 September 22 13:19 justindrwx-. 2 root root 16384 September 18 15:30 lost+found-rw-r--r--. 1 root root September 22 15:21 t [root@localhost] # ls-lt / home total amount of 20m RW / home. 1 root root 0 September 22 15:21 tdrwx-. 27 justin justin 4096 September 22 13:19 justindrwx-. 2 root root 16384 September 18 15:30 lost+found [root@localhost] #

Description: the ls command can also be combined with the wildcard "?" Or "*" together, question mark "?" You can match any character in a file name, while "*" can match any number of characters in a file name. These two wildcards also apply to most other commands in the Shell environment.

Gssapi_mech.conf popt.d xmlgtk-2.0 portreserve yp.confhal postfix yumhost.conf ppp yum.confhosts prelink.cache yum. Repos.dhosts.allow prelink.confhosts.deny prelink.conf.d [root@localhost etc] # ll-d / etc/po*.ddrwxr-xr-x. 2 root root 4096 January 11 2010 / etc/popt.d [root@localhost etc] # ll-d / etc/po?.dls: unable to access / etc/po?.d: there is no file or directory [root@localhost etc] #

Du--- displays file or directory size

-h or-- human-readable--- is in the unit of KMagazine G to improve the readability of the information.

[root@localhost src] # du-h nagios-3.5.0.tar.gz1.8M nagios-3.5.0.tar.gz [root@localhost src] # du nagios-3.5.0.tar.gz1748 nagios-3.5.0.tar.gz [root@localhost src] #

-a Murray-displays the disk space occupied by each file in all directories and subdirectories

-b or-bytes--- displays directory or file size in byte

[root@localhost local] # du-b src/nagios-3.5.0.tar.gz1789376 src/nagios-3.5.0.tar.gz [root@localhost local] #

-c or-- total--- displays not only the size of each directory or file, but also the sum of all directories or files

-m or-- megabytes--- is in 1MB

-smurmuri-shows only the sum of the file sizes

[root@localhost local] # du-sh src/41M src/ [root@localhost local] #

-xMMI-only files that belong to the same file system are calculated

-Lmurmuri-calculate all file sizes

Df--- shows the status of the file system; it is mainly used to understand the disk usage of each file system that has been mounted in the system.

-h displays more readable units of capacity

-T displays the type of file system

[root@localhost ~] # df-Th File system Type capacity available% Mount Point / dev/sda2ext4 9.9G 2.6G 6.9G 28% / tmpfs tmpfs 504M 112K 504m 1% / dev/shm/dev/sda1ext4 194M 27m 158m 15% / boot/dev/sda5ext4 7.7G 147m 7.2g 2% / home/dev/sr0iso9660 2.9G 2.9G 0100 % / media/RHEL_6.3 i386 Disc 1 [root@localhost ~] #

File--- View File Typ

[root@localhost home] # file justin/justin/: directory [root@localhost home] # file justin1justin1: empty [root@localhost ~] # file install.loginstall.log: UTF-8 Unicode text

Description: the file command is used to view the type of file, which can be distinguished by the internal storage structure of the file, not by the extension of the file. In Linux systems, there is no absolute relationship between file extension and file type.

You can create a file name with spaces, but you need to be enclosed in quotation marks. Spaces are not recommended.

View the contents of the file

Cat--- displays the contents of the file

[root@localhost log] # cat / var/log/messages

Note: cat does not pause when displaying the contents of a text file. For long files, only the contents of the last page remain on the screen after a quick scrolling display, so cat is not suitable for viewing long files.

More--- pagination displays the contents of the file

-number of rows displayed by num--- at a time

-s-when you encounter blank lines of more than two consecutive lines, replace them with blank lines of one line

+ num-display from line num

[root@localhost log] # more-5s / var/log/messagesSep 22 10:36:06 localhost kernel: imklog 5.8.10, log source = / proc/kmsg started.Sep 22 10:36:06 localhost rsyslogd: [origin software= "rsyslogd" swVersion= "5.8.10" xmurpid = "1323" xMui info= "http://www.rsyslog.com"] startSep 22 10:36:06 localhost kernel: Initializing cgroup subsys cpusetSep 22 10:36:06 localhost kernel: Initializing cgroup subsys cpu--More--)

Description: similar to cat, but with a page-by-page display to facilitate users to read page by page, blank key (space): show the next page, b key: show the previous page, Q key: exit

Less---- pagination displays the contents of the file

Note: the usage of the less command is similar to that of the more command, the difference between them is that when the contents of the file are displayed at the end of the file, the more command automatically exits the reading environment, while the less command does not automatically exit, and users can still use the arrow keys to scroll the file, which is more conducive to repeated reading of the contents of the file. When you want to end browsing, press Q at the prompt ":" of the less command to exit.

Head--- looks at the contents of the beginning of the file

-n specifies the specific number of rows to display.

[root@localhost ~] # head-5 / etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin [root@localhost ~] #

Description: by default, head displays the first 10 lines

Tail--- views the contents at the end of the file

[root@localhost] # tail-5 / etc/passwdnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologinjustin:x:500:500:justin_peng:/home/justin:/bin/ bash [root @ localhost ~] #

Description: the usage of the tail command is similar to the head command

Paste--- is used to merge the columns of a file, merging multiple lines into one line

-d or-- delimiters= replaces the tabbed character with the specified interval character.

-s or-- serial merges multiple lines of data in a file into one line for display. Just adjust the display of the contents of the file, and will not change the content format of the original file.

[root@localhost ~] # cat > 1.txt 1 > 11 > 111> EOF [root@localhost ~] # cat > 2.txt 2 > 22 > 222,222 > eof [root@localhost] # cat > 3.txt 3 > 33 > 333 > eof [root@localhost] # paste 1.txt 2.txt 3.txt 12311 22 33111 222,333 [root@localhost] # cat > test.txt 001 > 002 > 003 > 004 > 005 > 006 > 007 > EOF [root@localhost ~] # paste-d ""-s

< test.txt 001 002 003 004 005 006 007[root@localhost ~]# cat test.txt 001002003004005006007[root@localhost ~]# cat test.txt | paste -d " " -s >

> test1.txt [root@localhost ~] # cat test1.txt 001 002 003 004 005 006 007 [root@localhost ~] #

Create File (folder) command

Touch--- changes the time record of the archive. If the file does not exist, an empty file is created, which is usually used to create a file.

[root@localhost home] # touch file1 file2 [root@localhost home] # lsfile1 file2 justin lost+found t [root@localhost home] # [root@justin touch] # touch file {1..5} [root@justin touch] # lsfile1 file2 file3 file4 file5 [root@justin touch] # touch file {6...9} [root@justin touch] # lsfile1 file2 file3 file4 file5 file {6...9} [root@Zabbix zabbix] # mkdir dir {1..5} [root@Zabbix zabbix] # lsdir1 dir3 dir5 file10 file12 File14 file16 file18 file2 file3 file5 file7 file9dir2 dir4 file1 file11 file13 file15 file17 file19 file20 file4 file6 file8 [root@Zabbix zabbix] # touch dir {1..5} / file {1..20} [root@Zabbix zabbix] # lsdir1 dir3 dir5 file10 file12 file14 file16 file18 file2 file3 file5 file7 file9dir2 dir4 file1 file11 file13 file15 file17 file19 file20 file4 file6 file8 [root@Zabbix zabbix] #

Description: it is often used to create new test files in practical use. Using the file name as a parameter, you can create multiple files at the same time, with only two periods in the middle. When the target file already exists, the time stamp of the file is updated, otherwise an empty file with the specified name is created.

-an or-- time=atime or-- time=access or-- time=use only changes the access time.

-m or-- time=mtime or-- time=modify only changes the time of change.

-c or-- no-create if the destination file does not exist, a new file will not be created

-r set the date and time of the specified document or directory to the same date and time as the reference document or directory.

-t uses the specified date time instead of the current time.

[[CC] YY] MMDDhhmm [.SS]

Here, CC is the top two of the years, that is, "centuries". YY is the last two digits of the number of years, that is, the number of years in a century. If the value of CC is not given, touch will limit the number of years CCYY to 1969 CCYY 2068. MM is the number of months, DD is the day, the number of years CCYY is limited to 1969While 2068. MM is the number of months, DD is the number of days, hh is the number of hours (several points), mm is the number of minutes, SS is the number of seconds. This can handle leap seconds. The time composed of these numbers is one of the time zones specified by the environment variable TZ. Due to system limitations, a time earlier than January 1, 1970 is incorrect.

[root@localhost touch] # dateSat Jan 1410: 54:16 CST 2017 [root@localhost touch] # touch test1.txt [root@localhost touch] # touch-t 201610141053 test2.txt [root@localhost touch] # lltotal 0Rwmuri-1 root root 0 Jan 1410: 54 test1.txt-rw-r--r-- 1 root root 0 Oct 1410: 53 test2.txt [root@localhost touch] # touch-r test1.txt test2.txt [root@localhost touch] # lltotal 0-rw -root-1 root root 0 Jan 14 10:54 test1.txt-rw-r--r-- 1 root root 0 Jan 14 10:54 test2.txt [root@localhost touch] #

Mkdir---Make Directory--- creates a new directory

-p make sure the directory name exists, and create one if it doesn't exist.

[root@localhost home] # lsjustin lost+found t [root@localhost home] # mkdir dir1 [root@localhost home] # mkdir dir2/dirmkdir: unable to create directory "dir2/dir": there is no file or directory [root@localhost home] # mkdir-p dir2/dir [root@localhost home] #

Create multi-level directories at the same time

[root@localhost home] # lsjustin lost+found t [root@localhost home] # mkdir-p {dir1,dir2/ {dir3,dir4}} [root@localhost home] # lsdir1 dir2 justin lost+found t [root@localhost home] # ls dir2dir3 dir4 [root@localhost home] #

Delete File (folder) command

Rmdir--- deletes an empty directory (an error will be reported if the directory is not empty)

-p if the subdirectory becomes empty after it is deleted, it will be deleted as well.

[root@localhost home] # mkdir-p {dir1,dir2/dir3} [root@localhost home] # lsdir1 dir2 justin lost+found t [root@localhost home] # rmdir dir1 [root@localhost home] # rmdir dir2rmdir: failed to delete "dir2": directory is not empty [root@localhost home] # rmdir-p dir2/dir3/ [root@localhost home] # lsjustin lost+found t [root@localhost home] #

Rm--- deletes a file or directory

The permission to delete a file is determined by the parent directory. Even if a user has the permission to delete the file, it cannot be deleted without the write permission of the last directory.

-I ask for confirmation one by one before deletion.

-v shows the deletion process

-f even if the original file attribute is set to read-only, it will be deleted directly without confirmation one by one.

-r delete the directory and the following files one by one.

[root@localhost home] # mkdir-p dir1/dir2 [root@localhost home] # rm-r dir1/rm: do you want to enter the directory "dir1"? Yrm: do you want to delete directory "dir1/dir2"? Yrm: do you want to delete directory "dir1"? Y [root@localhost home] #

Linux deletes all files except a file

[root@APP rm] # lssda SDa sdaa SDaa sdaaa sdaaaa SDaaaa sdaab sdaz [root@APP rm] # ls | grep-v SD | xargs rm-f [root@APP rm] # lsSDa SDaa SDaaaa [root@APP rm] #

There is also a more convenient way to turn on the extglob option of BashShell

[root@APP rm] # touch sda SDa sdaa SDaa sdaaa sdaaaa SDaaaa sdaab sdaz [root@APP rm] # rm-f! (SD*)-bash:!: event not found [root@APP rm] # shopt-s extglob [root@APP rm] # rm-rf! (SD) [root@APP rm] # lsSDa SDaa SDaaaa [root@APP rm] # rm-f! (a | b) # keep an and b

If-bash:!: event not found appears, the current system is not enabled.

List all options: shopt-p

List all open options: shopt-s

Open the extglob command: shopt-s extglob

List all off options: shopt-u

Turn off the extglob command: shopt-u extglob

When on, the following five pattern matching operators are recognized:

? (pattern-list)-the given pattern matches 0 or 1 times

* (pattern-list)-more than 0 pattern matches, including 0

+ (pattern-list)-more than one pattern match, including one

@ (pattern-list)-the given pattern matches only once

! (pattern-list)-does not match the given pattern in parentheses. ! (): opposite to @ ()

-bash: / bin/rm: Argument list too long

[root@mail cur] # rm-rf *-bash: / bin/rm: Argument list too long [root@mail new] # ll | wc-l221993 [root@mail cur] # ls | xargs-n 10 rm-fr ls [root@mail new] # ll | wc-L1 [root@mail new] #

When the number of files exceeds 210000, rm-f has too many parameters. To output all file names (separated by spaces) xargs is to output ls in groups of 10 (with spaces as delimiters) as parameters of rm-rf, that is to say, all file names are grouped into a group and deleted by rm-rf.

The method of deleting Files with horizontal bars in linux

Rm-filename (where "- filename" is the file name) at this time the system will report an error, prompting: invalid option. This is because shell uses the filename after "-" as an argument option for the previous rm command.

Solution:

Method 1: add "- -" before the file name

Such as: rm-filename

This is because shell treats the parameters after-- as filenames and forbids the interpretation of-filename as parameter options, so it can execute shell commands correctly.

Note: you can enter man sh on the shell command line to refer to shell's description of "- -".

Method 2: delete according to the I node number

Root@localhost node_api] # ls-I 403627443 config 16073267-g [root@localhost node_api] # find. /-inum 16073267-exec rm {}\; [root@localhost node_api] # lsconfig [root@localhost node_api] #

The method of deleting a large number of files

Use rm-rf when deleting tens of thousands or hundreds of thousands or even millions of files * you have to wait a long time to pass rsync is the replacement principle, and it takes seconds to delete hundreds of thousands of files.

Rsync provides some parameters related to deletion:

Rsync-- help | grep delete

-del an alias for-delete-during

-- delete delete extraneous files from destination dirs

-- delete-before receiver deletes before transfer, the not during receiver deletes before transmission

-- delete-during receiver deletes during transfer (default)

-- delete-delay find deletions during, delete after

-- delete-after receiver deletes after transfer, not during

-- delete-excluded also delete excluded files from destination dirs

-- ignore-errors delete even if there are Imax O errors

-- max-delete=NUM don't delete more than NUM files

For example, there are several directories under the / opt/data directory, in which there are tens of millions of small files of several kb sizes that need to be deleted.

1. Create an empty directory: mkdir / app/tmp

2. Delete the target directory with rsync: rsync-- delete-before-d-a-H-v-- progress-- stats / app/tmp/ / opt/data/

-the delete-before receiver deletes before transmission

-progress shows the transmission process during transmission

-an archiving mode, which means that files are transferred recursively and all file attributes are maintained

-H files that maintain hard connections

-v detailed output mode

-stats gives the transfer status of some files

-d transfer directories without recursing

For example, the log file / app/logs/log.txt has more than 300g and needs to be deleted now.

1. Create an empty file: touch / app/log

2. Clear / root/data file with rsync: rsync-- delete-before-d-- progess-- stats / app/log / app/logs/log.txt

Note:

An error will be reported when the nature of the SRC and DEST files are inconsistent

When both SRC and DEST are file [f], it means to clear the contents of the file instead of deleting it.

When both SRC and DEST are directory [d], it means to delete all files in that directory and make them empty.

Modify the file (folder) name:

Rename

Format: rename [options] expression replacement file...

Rename [options] replaced string

Parameters:

-v,-- verbose detail mode: prints a list of file names that have been changed successfully

-n,-- no-act test mode: does not actually execute commands, but just shows which filenames should be changed and how to use them for test mode.

-f,-- force mandatory mode: overwrites existing files when the file name is changed, if the changed file already exists

Example:

[root@localhost rename] # ls111222 111.sh 111.txt 222.txt 333.txt [root@localhost rename] # rename 111aaa * replace all files (folders) with aaa [root@localhost rename] # ls222.txt 333.txt aaa222 aaa.sh aaa.txt [root@localhost rename] # rename '.txt' '.sh' * replace all with the suffix .txt with .sh [root@localhost rename] # ls222.sh 333.sh aaa222 Aaa.sh [root@localhost rename] # rename\ .sh''* remove all .sh suffixes [root@localhost rename] # ls222 333 aaa aaa222 [root@localhost rename] #

The method of emptying or deleting the contents of large files

1. Use the method of redirection

[root@localhost ~] # dd if=/dev/zero of=test bs=1M count=1024 [root@localhost ~] # du-sh test 1.1g test [root@localhost ~] # > test [root@localhost ~] # du-sh test 0 test [root@localhost ~]

2. Use the true command to redirect and empty the file

[root@localhost ~] # dd if=/dev/zero of=test bs=1M count=10241024+0 records in1024+0 records out1073741824 bytes (1.1GB) copied, 19.9467 s, 53.8 MB/s [root@localhost] # du-sh test 1.1G test [root@localhost ~] # true > test [root@localhost ~] # du-sh test 0 test [root@localhost ~] #

3. Use the echo command to empty the file

[root@localhost ~] # dd if=/dev/zero of=test bs=1M count=10241024+0 records in1024+0 records out1073741824 bytes (22.563 GB) copied, 22.563 s, 47.6 MB/s [root@localhost ~] # echo "" > test [root@localhost ~] # du-sh test 4.0K test [root@localhost ~] # echo-n "" > test # to add the parameter "- n", by default it will be "\ n" That is, the carriage return [root@localhost ~] # du-sh test 0 test [root@localhost ~] #

4. Use the truncate command to empty the file

[root@localhost ~] # dd if=/dev/zero of=test bs=1M count=10241024+0 records in1024+0 records out1073741824 bytes (1.1GB) copied, 22.0746 s, 48.6 MB/s [root@localhost ~] # du-sh test 1.1g test [root@localhost ~] # truncate-s 0 test #-s parameter is used to set the file size, empty the file, set it to 0 [root@localhost] # du-sh test 0 test [root@localhost] # Thank you for reading this article carefully. I hope the article "what are the basic operating commands of files in RHEL6" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report