File wildcards, file creation, copying, and other operations in linux

Source: Internet
Author: User
Tags current time mkdir touch command

1: bash feature status return value

Variable $ ?, It is used to save the status of successful command execution. 0 indicates successful, 1-indicates failed, take the command ls as an example:

Execution successful

[Root @ localhost ~] # Ls/usr/
Bin etc games include lib lib64 libexec local sbin share src tmp
[Root @ localhost ~] # Echo $?
0
Command input error

[Root @ localhost ~] # Lss/usr
-Bash: lss: command not found
[Root @ localhost ~] # Echo $?
127
Command parameter input error


[Root @ localhost ~] # Ls/usrrr
Ls: cannot access/usrrr: No such file or directory
[Root @ localhost ~] # Echo $?
2
Other error statuses, $? The result is not 0.

2: expand the command line

2.1: use the command line to expand and create the \ _ c, a \ _ d, B \ _ c, B \ _ d files under/tmp.


[Root @ localhost ~] # Mkdir-pv {a, B }_{ c, d}
Mkdir: created directory 'a _ C'
Mkdir: created directory 'a _ d'
Mkdir: created directory 'B _ C'
Mkdir: created directory 'B _ d'
[Root @ localhost ~] # Ls-d {a, B }_{ c, d}
A_c a_d B _c B _d
2.2: create the following file under/tmp.

Write the image description here


[Root @ localhost ~] # Mkdir-pv/tmp/mylinux/{bin, boot/grub, etc {/rc. d/init. d,/sysconfig/network-scripts}, dev, lib/modules, lib64, proc, sbin, sys, tmp, usr/local {/bin,/sbin }, var {/lock,/log,/run }}
[Root @ localhost ~] # Tree/tmp/mylinux // tmp/mylinux/
| -- Bin
| -- Boot
| '-- Grub
| -- Dev
| -- Etc
| -- Rc. d
| '-- Init. d
| '-- Sysconfig
| '-- Network-scripts
| -- Lib
| '-- Modules
| -- Lib64
| -- Proc
| -- Sbin
| -- Sys
| -- Tmp
| -- Usr
| '-- Local
| -- Bin
| '-- Sbin
'-- Var
| -- Lock
| -- Log
'-- Run24 directories, 0 files

3: How to define a command alias? How can I reference the results of another command in one command?
You can use the reverse quote "or $ () to reference the execution results of other commands:

Use quotation marks

[Root @ localhost ~] # Dirname 'which touch'
/Bin
 
# Display the path of the touch Command
Use $ ()

[Root @ localhost ~] # Dirname $ (which touch)
/Bin

4: show all files and directories in the/var directory that start with the letter "l" and end with a lower-case letter and contain at least one digit (which can contain other characters) in the middle.


[Root @ localhost ~] # Ls-d/var/l * [[: digit:] * [[: lower:]
/Var/l13u/var/lfs3k
5: displays the files and directories starting with any number and ending with a non-number in the/etc directory.
1
2
[Root @ localhost ~] # Ls-d/etc/[[: digit:] * [^ [: digit:]
/Etc/222kkk/etc/333uuu

6: displays files and directories in the/etc directory that start with a non-letter and are followed by a letter and any other characters of any length.


[Root @ localhost ~] # Ls-d/etc/[^ [: alpha:] [[: alpha:] *
/Etc/1djj

7: create a file starting with tfile in/tmp, followed by the current time


[Root @ localhost ~] # Touch/tmp/tfile-'date + % F-% H-% M-% s'
[Root @ localhost ~] # Touch/tmp/tfile-'date + % F-% H-% M-% s'
[Root @ localhost ~] # Ls/tmp
Tfile-2016-08-25-08-14-35 tfile-2016-08-25-08-14-36

8: copy the files starting with "p" in the/etc directory and ending with non-numbers to the/tmp/mytest1 directory.


[Root @ localhost ~] # Mkdir/tmp/mytest1
[Root @ localhost ~] # Cp-r/etc/p * [^ [: digit:]/tmp/mytest1/
[Root @ localhost ~] # Ls/tmp/mytest1/
Pam. d passwd-pki plymouth Uth pm popt. d postfix ppp prelink. conf. d printcap profile. d protocols

9: Copy all directories and files ending with. d in the/etc directory to the/tmp/mytest2 directory.


[Root @ localhost ~] # Mkdir/tmp/mytest2
[Root @ localhost ~] # Cp-r/etc/*. d/tmp/mytest2
[Root @ localhost ~] # Ls/tmp/mytest2
Bash_completion.d dracut. conf. d makedev. d prelink. conf. d rc1.d rc5.d statetab. d yum. repos. d
Chkconfig. d init. d modprobe. d profile. d rc2.d rc6.d sudoers. d
Cron. d ld. so. conf. d pam. d rc. d rc3.d rsyslog. d sysctl. d
Depmod. d logrotate. d popt. d rc0.d rc4.d rwtab. d xinetd. d

10: Copy all files starting with l, n, or m and ending with. conf to the/tmp/mytest3 directory.


[Root @ localhost ~] # Mkdir/tmp/mytest3
[Root @ localhost ~] # Cp-r/etc/[lmn] *. conf/tmp/mytest3
[Root @ localhost ~] # Ls/tmp/mytest3
Ld. so. conf libaudit. conf libuser. conf logrotate. conf mke2fs. conf nsswitch. conf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.