[Linux] common instruction summary (b)

A command .cp

Syntax: cp [options] source file or directory target file or directory

  • Function: Copy a file or folder
  • Description: cp command is used to copy a file or directory, such as specifying two or more files or directories, and the destination is an existing directory, all files or directories specified above it will copy this directory . If specify multiple files or directories, then the destination is not an existing directory, an error message appears
  • Common options:
    -f forced to copy a file or directory, regardless of the destination file or directory already exists
    to ask the user before overwriting files -i
    -r recursive processing, the files and subdirectories in the specified directory dealt with together. If the form of the source file or directory, not a directory or a symbolic link, be regarded as an ordinary file processing
    -R recursive processing, the files and subdirectories in the specified directory treated together
  • For example:
cp b.txt ../tt   //拷贝文件到其他目录
cp b.txt  s.txt   //重写s.txt
cp a.txt b.txt ../tt
cp ../test   /root/a

Two .mv instruction

mv command to move is the abbreviation for moving a file or rename the file,

  • Syntax: mv [options] source file or directory target file or directory
  • Function:
    mv command in the second argument of a different type (the target file or target directory), mv command to rename the file or move it to a new directory.
    When the second argument is the file type, mv command to rename the file to complete, this time, only one source file (directory name may be a source), it will be given source file or directory rename given target file name.
    When the second parameter is the name of the directory already exists, the source file or directory can have multiple parameters, mv command to specify the parameters of the source files are moved to the destination directory.
  • Common options:
    -f: Force mandatory sense, if the target file already exists, do not ask directly covered
    -i: If the target file (destination) already exists, it will ask whether to overwrite!
mv a.txt  b.txt  //重命名
mv a.txt ../test  //将a.txt文件移动至目标目录

Three .zip instruction

1.zip instruction

  • Syntax:. Zip zip zip file required directory or file
  • Function: directories or files into a zip format
  • Common options
    -f parameter to all the files and subdirectories in the specified directory recursively processing

2.unzip instruction

  • Syntax: unzip compressed files

  • Function: extract the zip file

  • Common options
    -d parameter to specify the path to store the extracted files

  • For example

touch a.txt
zip zz.zip a.txt
unzip zz.zip -d ../zip 

Four .tar command

  • Function: is also packing and unpacking instructions, more powerful, can directly view the package file.
  • Common Options
    -c: a directive to establish parameters of the compressed file (create meaning);
    -x: unlock a file compression parameter instructions!
    -t: View tarfile files inside!
    -z: gzip compression
    -j: bzip2 to compress
    -v: display the file compression process! The commonly used, but is not recommended for use during the execution of the background!
    -f: Use the file name, please note that after f to then file name immediately. Plus parameters is not recommended.
    -C: unzip to the specified directory
    -p: retain the original file permissions! !
范例一:将整个 /etc 目录下的文件全部打包成为 `/tmp/etc.tar`
tar -cvf /tmp/etc.tar /etc    //仅打包,不压缩!
tar -zcvf /tmp/etc.tar.gz /etc   //打包后,以 gzip 压缩
tar -jcvf /tmp/etc.tar.bz2 /etc //打包后,以 bzip2 压缩
在参数 f 之后的文件档名是自己取的,习惯上都用 .tar 来作为辨识。
如果加 z 参数,则以 .tar.gz 或 .tgz 作为名称。
如果加 j 参数,则以 .tar.bz2 来作为附档名。
范例二:查阅上述 /tmp/etc.tar.gz 文件内有哪些文件
tar -ztvf /tmp/etc.tar.gz
范例三:将 /tmp/etc.tar.gz 文件解压缩在 /usr/local/src 底下
cd /usr/local/src
tar -zxvf /tmp/etc.tar.gz   //可以将压缩档在任何地方解开。
范例四:在 /tmp 底下,只解压单个文件 
cd /tmp
tar -zxvf /tmp/etc.tar.gz etc/passwd    //后为文件名
//可以透过 tar -ztvf 来查阅 tarfile 内的文件名称。
范例五:备份 /home, /etc ,但不备份 /home/dmtsai
tar --exclude /home/dmtsai -zcvf myfile.tar.gz /home/*  /etc

Five .find instruction

  • Function: Find files in the file tree, and make the appropriate treatment
  • Common options:
    -name find files by file name
    , for example:
find  -name b.txt
find ~  -name b.txt  //前面可以加查询路径,如不加则为当前目录!!!
find  -name b.*   //*号代表任意字符(长度不限)
find  -name b*.txt

Here Insert Picture Description

Six .grep instruction

  • Syntax: grep [options] Search string file
  • Features: search string in the file, find the line of print
  • Common options:
    -i: Ignore different letter, so that the case considered the same
    -n: Incidentally outputs row number
    -v: reverse selection, i.e., showed no 'search string' content of the line
  • For example:
grep"it" c.txt b.txt 

Here Insert Picture Description

Published 58 original articles · won praise 49 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_43550839/article/details/104092128