ls (list files) command is used to display the list of file directories, which is similar to the DOS command dir under Windows system. When executing ls command, only the file name of non hidden file, sorting by file name and the color represented by file name are displayed by default. When no parameters are added, the list information of the current directory is listed by default. The ls command is used frequently and has many parameters, up to forty or fifty. This article only introduces some common parameters. For other parameters, you can view the help manual through the man command.
Note: under linux, the file name is dotted The beginning indicates that the file is hidden, such as bashrc.
ls command syntax
ls [option] [Directory or file name] Parameters: -a: --all Display all files, including hidden files(with.Start file),Refer to example 1.(Commonly used) -A: --almost-all The abbreviation of shows all files, including hidden files, but excluding the current directory.And parent directory..For these two documents, refer to example 2. -c: and-lt Use together: displays a list and displays it in ctime(Last change time of file status)Sort. and-l Use with: display ctime And sorted by file name. Other cases, to ctime Sort. Refer to example 3. -d: --directory For the abbreviation of, only the directory itself is listed, not the list of contents in the directory. Refer to example 4.(Commonly used) -f: List the results directly without sorting(ls Sort by file name by default) --color[=WHEN]: Whether to display color according to file type, WHEN Can be never,always perhaps auto --full-time: Display in full actual mode(Including mm / DD / yyyy),Similar to ls -l --time-style=full-iso,Refer to example 5. -g: List display results, and-l Similar, but the owner of the file is not displayed. -h: Change the file content size to GB,KB And so on, refer to example 6. -i: combination-l Parameter that lists the of each file inode,Refer to example 7. -l: List long data strings and display data information such as file attributes and permissions(Commonly used) -n: and-l Similarly, only the displayed users and groups are not names, but corresponding id,Refer to example 8. -r: --reverse,Display the sorting results in reverse order, refer to Example 9. -S: Sort by file size, refer to Example 9. -t: Sort by modified time --help: display help information
ls command example
[centos@s202 ~]$ ls 1.log a.txt coprocessor.log filename.txt for99.sh hadoop-2.7.3.tar.gz ini log student.txt test4.sh tf.sh 1.txt caseeg.sh file file.out ha hadoop.txt jdk-8u131-linux-x64.tar.gz logs test2.sh test5.sh zookeeper anaconda3 color99.sh file.err for99 hadoop header.txt kafka logZip.sh test3.sh test.sh zookeeper.out
Example 1: list all files (note the difference with the - A parameter. The result includes the two files representing the current directory. And the parent directory.
[centos@s202 ~]$ ls -a . anaconda3 .bash_profile color99.sh file.err for99 hadoop-2.7.3.tar.gz jdk-8u131-linux-x64.tar.gz logZip.sh student.txt test5.sh zookeeper.out .. a.txt .bashrc .continuum filename.txt for99.sh hadoop.txt kafka .oracle_jre_usage test2.sh test.sh 1.log .bash_history .bashrc-anaconda3.bak coprocessor.log file.out ha header.txt log .python_history test3.sh tf.sh 1.txt .bash_logout caseeg.sh file .flume hadoop ini logs .ssh test4.sh zookeeper
Example 2: list all files, but not the current directory And parent directory These two documents.
[centos@s202 ~]$ ls -A 1.log .bash_history .bashrc-anaconda3.bak coprocessor.log file.out ha header.txt log .python_history test3.sh tf.sh 1.txt .bash_logout caseeg.sh file .flume hadoop ini logs .ssh test4.sh zookeeper anaconda3 .bash_profile color99.sh file.err for99 hadoop-2.7.3.tar.gz jdk-8u131-linux-x64.tar.gz logZip.sh student.txt test5.sh zookeeper.out a.txt .bashrc .continuum filename.txt for99.sh hadoop.txt kafka .oracle_jre_usage test2.sh test.sh [centos@s202 ~]$ ls --almost-all 1.log .bash_history .bashrc-anaconda3.bak coprocessor.log file.out ha header.txt log .python_history test3.sh tf.sh 1.txt .bash_logout caseeg.sh file .flume hadoop ini logs .ssh test4.sh zookeeper anaconda3 .bash_profile color99.sh file.err for99 hadoop-2.7.3.tar.gz jdk-8u131-linux-x64.tar.gz logZip.sh student.txt test5.sh zookeeper.out a.txt .bashrc .continuum filename.txt for99.sh hadoop.txt kafka .oracle_jre_usage test2.sh test.sh
Example 3: display the list and sort by ctime
[centos@s202 ~]$ ls -clt total 390824 -rw-rw-r--. 1 centos centos 65 May 23 14:59 1.log -rw-rw-r--. 1 centos centos 157 May 23 14:59 test5.sh -rw-rw-r--. 1 centos centos 205 May 23 11:27 logZip.sh -rw-rw-r--. 1 centos centos 108 May 23 11:15 test4.sh -rw-rw-r--. 1 centos centos 139 May 23 09:15 ini -rw-rw-r--. 1 centos centos 268 May 23 01:16 test3.sh -rw-rw-r--. 1 centos centos 51 May 21 17:38 filename.txt -rw-rw-r--. 1 centos centos 1 May 21 17:38 file.err -rw-rw-r--. 1 centos centos 1 May 21 17:37 file.out -rw-rw-r--. 1 centos centos 51 May 21 17:30 a.txt -rw-rw-r--. 1 centos centos 56 May 21 16:33 test.sh -rw-rw-r--. 1 centos centos 5 May 21 14:39 file -rw-rw-r--. 1 centos centos 8 Apr 5 01:18 test2.sh drwxrwxr-x. 3 centos centos 35 Apr 5 01:03 zookeeper -rw-rw-r--. 1 centos centos 474970 Apr 5 01:03 zookeeper.out drwxrwxr-x. 12 centos centos 150 Jan 26 17:06 anaconda3 drwxrwxr-x. 3 centos centos 18 Nov 2 2018 kafka drwxrwxr-x. 4 centos centos 37 Oct 11 2018 ha -rw-rw-r--. 1 centos centos 4 Aug 24 2018 student.txt drwxrwxr-x. 2 centos centos 63 Aug 24 2018 log drwxrwxr-x. 2 centos centos 6 Aug 24 2018 logs -rw-rw-r--. 1 centos centos 10 Aug 24 2018 header.txt -rw-rw-r--. 1 centos centos 219 Aug 18 2018 coprocessor.log drwxrwxr-x. 4 centos centos 37 Jul 22 2018 hadoop -rw-rw-r--. 1 centos centos 1060 Jul 20 2018 hadoop.txt -rw-rw-r--. 1 centos centos 214092195 Jul 20 2018 hadoop-2.7.3.tar.gz -rwxrwxr-x. 1 centos centos 86 Jul 17 2018 caseeg.sh -rwxrw-r--. 1 centos centos 90 Jul 17 2018 color99.sh -rwxrwxr-x. 1 centos centos 53 Jul 17 2018 tf.sh -rwxrwxr-x. 1 centos centos 140 Jul 17 2018 for99.sh -rwxrwxr-x. 1 centos centos 108 Jul 17 2018 for99 -rw-rw-r--. 1 centos centos 20 Jul 17 2018 1.txt -rw-rw-r--. 1 centos centos 185540433 Jul 17 2018 jdk-8u131-linux-x64.tar.gz
[centos@s202 ~]$ ls -cl total 390824 -rw-rw-r--. 1 centos centos 65 May 23 14:59 1.log -rw-rw-r--. 1 centos centos 20 Jul 17 2018 1.txt drwxrwxr-x. 12 centos centos 150 Jan 26 17:06 anaconda3 -rw-rw-r--. 1 centos centos 51 May 21 17:30 a.txt -rwxrwxr-x. 1 centos centos 86 Jul 17 2018 caseeg.sh -rwxrw-r--. 1 centos centos 90 Jul 17 2018 color99.sh -rw-rw-r--. 1 centos centos 219 Aug 18 2018 coprocessor.log -rw-rw-r--. 1 centos centos 5 May 21 14:39 file -rw-rw-r--. 1 centos centos 1 May 21 17:38 file.err -rw-rw-r--. 1 centos centos 51 May 21 17:38 filename.txt -rw-rw-r--. 1 centos centos 1 May 21 17:37 file.out -rwxrwxr-x. 1 centos centos 108 Jul 17 2018 for99 -rwxrwxr-x. 1 centos centos 140 Jul 17 2018 for99.sh drwxrwxr-x. 4 centos centos 37 Oct 11 2018 ha drwxrwxr-x. 4 centos centos 37 Jul 22 2018 hadoop -rw-rw-r--. 1 centos centos 214092195 Jul 20 2018 hadoop-2.7.3.tar.gz -rw-rw-r--. 1 centos centos 1060 Jul 20 2018 hadoop.txt -rw-rw-r--. 1 centos centos 10 Aug 24 2018 header.txt -rw-rw-r--. 1 centos centos 139 May 23 09:15 ini -rw-rw-r--. 1 centos centos 185540433 Jul 17 2018 jdk-8u131-linux-x64.tar.gz drwxrwxr-x. 3 centos centos 18 Nov 2 2018 kafka drwxrwxr-x. 2 centos centos 63 Aug 24 2018 log drwxrwxr-x. 2 centos centos 6 Aug 24 2018 logs -rw-rw-r--. 1 centos centos 205 May 23 11:27 logZip.sh -rw-rw-r--. 1 centos centos 4 Aug 24 2018 student.txt -rw-rw-r--. 1 centos centos 8 Apr 5 01:18 test2.sh -rw-rw-r--. 1 centos centos 268 May 23 01:16 test3.sh -rw-rw-r--. 1 centos centos 108 May 23 11:15 test4.sh -rw-rw-r--. 1 centos centos 157 May 23 14:59 test5.sh -rw-rw-r--. 1 centos centos 56 May 21 16:33 test.sh -rwxrwxr-x. 1 centos centos 53 Jul 17 2018 tf.sh drwxrwxr-x. 3 centos centos 35 Apr 5 01:03 zookeeper -rw-rw-r--. 1 centos centos 474970 Apr 5 01:03 zookeeper.out
Example 4: only the directory itself is listed, and there is no need to list the contents in the directory
[centos@s202 ~]$ ls -d /home #Only the / home directory itself is listed /home [centos@s202 ~]$ ls /home #List the contents of the / home directory centos
Example 5: display full time
[centos@s202 ~]$ ls --full-time / total 20 lrwxrwxrwx. 1 root root 7 2018-07-16 22:14:25.675333601 +0800 bin -> usr/bin dr-xr-xr-x. 4 root root 4096 2018-07-16 22:20:27.271001648 +0800 boot drwxr-xr-x. 20 root root 3180 2019-05-24 20:32:14.446999702 +0800 dev drwxr-xr-x. 78 root root 8192 2019-05-24 20:32:06.675999960 +0800 etc drwxr-xr-x. 3 root root 20 2018-07-16 22:18:42.624343276 +0800 home lrwxrwxrwx. 1 root root 7 2018-07-16 22:14:25.677333601 +0800 lib -> usr/lib lrwxrwxrwx. 1 root root 9 2018-07-16 22:14:25.677333601 +0800 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 6 2016-11-05 23:38:36.000000000 +0800 media drwxr-xr-x. 2 root root 6 2016-11-05 23:38:36.000000000 +0800 mnt drwxr-xr-x. 2 root root 45 2019-01-26 17:08:03.622231114 +0800 opt dr-xr-xr-x. 156 root root 0 2019-05-24 20:31:59.011000000 +0800 proc dr-xr-x---. 7 root root 209 2018-08-31 14:37:12.818765051 +0800 root drwxr-xr-x. 21 root root 600 2019-05-24 20:32:26.359999305 +0800 run lrwxrwxrwx. 1 root root 8 2018-07-16 22:14:25.677333601 +0800 sbin -> usr/sbin drwxr-xr-x. 10 centos centos 4096 2018-10-09 15:46:23.455045955 +0800 soft drwxr-xr-x. 2 root root 6 2016-11-05 23:38:36.000000000 +0800 srv dr-xr-xr-x. 13 root root 0 2019-05-24 20:32:07.496999933 +0800 sys drwxrwxrwt. 7 root root 93 2019-05-24 21:31:02.252882289 +0800 tmp drwxr-xr-x. 13 root root 155 2018-07-16 22:14:25.839333607 +0800 usr drwxr-xr-x. 19 root root 267 2019-05-24 20:32:06.676999960 +0800 var
Example 6: display a list in a readable manner
[centos@s203 ~]$ ls -l total 524 drwxrwxr-x. 12 centos centos 150 Jan 26 17:17 anaconda3 drwxrwxr-x. 4 centos centos 37 Oct 11 2018 ha drwxrwxr-x. 4 centos centos 37 Jul 22 2018 hadoop drwxrwxr-x. 3 centos centos 18 Nov 2 2018 kafka drwxrwxr-x. 2 centos centos 19 Aug 23 2018 log drwxrwxr-x. 2 centos centos 21 Aug 24 2018 logs drwxrwxr-x. 3 centos centos 56 Apr 5 01:03 zookeeper -rw-rw-r--. 1 centos centos 534884 Apr 5 01:03 zookeeper.out [centos@s203 ~]$ ls -lh total 524K drwxrwxr-x. 12 centos centos 150 Jan 26 17:17 anaconda3 drwxrwxr-x. 4 centos centos 37 Oct 11 2018 ha drwxrwxr-x. 4 centos centos 37 Jul 22 2018 hadoop drwxrwxr-x. 3 centos centos 18 Nov 2 2018 kafka drwxrwxr-x. 2 centos centos 19 Aug 23 2018 log drwxrwxr-x. 2 centos centos 21 Aug 24 2018 logs drwxrwxr-x. 3 centos centos 56 Apr 5 01:03 zookeeper -rw-rw-r--. 1 centos centos 523K Apr 5 01:03 zookeeper.out [centos@s203 ~]$
Example 7: displaying inode s
[centos@s203 ~]$ ls -li / total 20 95 lrwxrwxrwx. 1 root root 7 Jul 16 2018 bin -> usr/bin 64 dr-xr-xr-x. 4 root root 4096 Jul 16 2018 boot 1025 drwxr-xr-x. 20 root root 3180 May 27 11:29 dev 16777281 drwxr-xr-x. 78 root root 8192 May 27 11:29 etc 50331740 drwxr-xr-x. 3 root root 20 Jul 16 2018 home 98 lrwxrwxrwx. 1 root root 7 Jul 16 2018 lib -> usr/lib 82 lrwxrwxrwx. 1 root root 9 Jul 16 2018 lib64 -> usr/lib64 83 drwxr-xr-x. 2 root root 6 Nov 5 2016 media 16777649 drwxr-xr-x. 2 root root 6 Nov 5 2016 mnt 33575378 drwxr-xr-x. 2 root root 45 Jan 26 16:48 opt 1 dr-xr-xr-x. 154 root root 0 May 27 11:29 proc 33574977 dr-xr-x---. 7 root root 213 Jan 26 17:15 root 9241 drwxr-xr-x. 21 root root 600 May 27 11:30 run 99 lrwxrwxrwx. 1 root root 8 Jul 16 2018 sbin -> usr/sbin 33575338 drwxr-xr-x. 10 centos centos 4096 Oct 9 2018 soft 50331741 drwxr-xr-x. 2 root root 6 Nov 5 2016 srv 1 dr-xr-xr-x. 13 root root 0 May 27 11:29 sys 16777288 drwxrwxrwt. 7 root root 93 May 27 12:48 tmp 33575353 drwxr-xr-x. 13 root root 155 Jul 16 2018 usr 50331713 drwxr-xr-x. 19 root root 267 May 27 11:29 var
The Linux cp (copy file) command is mainly used to copy files or directories.
cp command syntax
cp [options] source dest
or
cp [options] source... directory
Parameter Description:
- -a: This option is usually used when copying a directory. It retains links, file properties, and copies everything under the directory. Its function is equal to dpR parameter combination.
- -d: Keep links when copying. The links mentioned here are equivalent to shortcuts in Windows system.
- -f: Overwrite the existing target file without prompting.
- -i: Contrary to the - f option, a prompt is given before overwriting the target file to ask the user to confirm whether to overwrite. When you answer y, the target file will be overwritten.
- -p: In addition to copying the contents of the file, the modification time and access rights are also copied to the new file.
- -r: If the given source file is a directory file, all subdirectories and files in the directory will be copied.
- -l: Do not copy files, just generate linked files.
-a. -- archive equals - dR --preserve=all --backup[=CONTROL create a backup for each existing target file
-b similar - backup but does not accept the parameter -- copy contents copies the contents of special files in recursive processing
-d equals – no dereference -- preserve = links
-f. -- force if the target file cannot be opened, remove it and try again (do not select this option when the - n option exists)
-i. -- interactive ask before overwriting (invalidates the previous - n option)
-H follows the command line symbolic link in the source file
-l. -- link link file without copying
-50. -- dereference always follows symbolic links
-n. -- no clobber do not overwrite existing files (invalidate the previous - i option)
-P. -- no dereference does not follow symbolic links in the source file
-p equals – preserve = mode, ownership, timestamp -- preserve [= the attribute list maintains the specified attributes (default: mode, ownership, timestamp), and if possible, additional attributes: environment, link, xattr, etc
-R. - R, -- recursive copy the directory and all items in the directory
cp command example
1, Copy a source file to a destination file (folder).
The command format is: cp source file destination file (folder)
This is the most frequently used command, which is responsible for copying a source file to the target file (folder). As shown in the following figure, if you copy to a folder, the file name will remain unchanged, and if you copy to a file, the file name will change. If the target file already exists or the target folder contains a file with the same name, the target file or the file with the same name in the target folder will be overwritten after copying.
The cp command copies a file
2, Copy multiple files to the target file (folder) at the same time.
The command format is: cp source file 1 source file 2 destination folder or cp file * destination folder
This command is also used frequently. The first command format can copy multiple listed files to the target folder at one time, and each file is directly separated by a space; The second command format uses wildcard *, as shown in the following figure. You can copy all files ending in ". txt" to the folder testDir.
The cp command copies multiple files
The cp command copies multiple files
3, Copy the source folder to the destination folder.
The command format is: cp -r source folder destination folder
This is also a frequently used command. Just remember to add "- r" parameter to copy folders, otherwise "CP: submitting directory" error will appear.
Copy folder with cp command
4, Copy only if the source file is newer than the destination file.
The command format is: cp -u source file destination file
This command is useful, especially when updating files. As shown in the figure below, the source file will be copied to the target file only when the source file is newer than the target file. Otherwise, the command will not be copied if it is executed in time.
The cp command copies only when the file is newer
5, Create a soft link to the file.
The command format is: cp -s source file destination file
You can also use the ln command to achieve the same function. When a file path is too deep (such as a/b/c/d/e/orginalFile.txt below) and it is very inconvenient to access, a soft link to the file will be created to make it easier to access. Soft links are equivalent to shortcuts on windows.
The cp command creates a soft link
6, Create a hard link to the file.
The command format is: cp -l source file destination file
You can also use the ln command to achieve the same function. Sometimes, for some reason, you need to create a hard link, as shown in the following figure.
The cp command creates a hard link
Here is a brief introduction to the difference between hard link and soft link. As shown in the following figure, the inodes of the soft link file (softLink.txt) and the linked file (orginalFile.txt) are different (133827 and 139819), and the hard link file (hardLink.txt) and the linked file (orginalFile22.txt) have the same inodes (156981). When the linked file is deleted, the soft link file cannot be accessed, while the hard link file can still be accessed normally.