Linux - directory and file operations

1. Operation directory

1.1 Directory switching

  • cd /homeGo into the '/home' directory.
  • cd /enter the root directory
  • cd ..Return to the previous directory.
  • cd ../..Returns the directory two levels above.
  • cdGo to your personal home directory.
  • cd ~Go to your personal home directory.
  • cd -Return to the last directory you were in.

1.2 Browse the directory

  • pwdDisplays the working path.
  • lsView the files in the directory.
  • ls -FView the files in the directory.
  • ls -lDisplay details of files and directories.
  • ls -aShow hidden files.
  • ls *[0-9]*Displays file and directory names that contain numbers.
  • treeDisplays a tree structure of files and directories starting with the root directory.
  • lstreeDisplays a tree structure of files and directories starting with the root directory.

1.3 Directory Creation

  • mkdir dir1Create a directory called 'dir1'.
  • mkdir dir1 dir2Create two directories at the same time.
  • mkdir -p /tmp/dir1/dir2Create multi-level directories

1.4 Directory deletion

  • rmdir dir1Delete a directory called 'dir1'.
  • rm -rf dir1Delete a directory called 'dir1' and recursively delete subdirectories and files in the directory.
  • rm -rf dir1 dir2Delete both directories and their contents at the same time.
  • rm -ri dir1: Ask to confirm deletion before deleting

1.5 Copy directory

  • cp dir/* .Copy all files in a directory to the current working directory.
  • cp -a /tmp/dir1 .Copy a directory to the current working directory.
  • cp -a dir1 dir2Copy dir1 to dir2.
  • cp -r [目录1] [目录2]Recursively copy all files and folders in directory 1 to directory 2.
  • cp -ri [目录1] [目录2Ask the user before overwriting the original directory when performing a copy operation

1.6 Moving or Renaming a Directory

  • mv dir1 new_dirRename/move a directory.

2. Manipulating files

2.1 Find files

  • find / -name file1Searches for files and directories into the root filesystem starting at '/'.
  • find / -user user1Search for files and directories belonging to user 'user1'.
  • find /home/user1 -name \*.binSearches for files ending in '.bin' in the directory '/home/user1'.

2.2 View file information

  • stat file1Look at the value of the file creation/modification time 'Change'.
  • du -h 文件名Check the file size.

2.3 View file content

  • cat -n file1View the entire contents of the file starting from the first byte and displaying line numbers.
  • tac file1View the contents of a file in reverse, starting from the last line.
  • head -2 file1View the first two lines of a file.
  • tail -2 file1View the last two lines of a file.
  • tail -f /var/log/file1See what's being added to a file in real time.
  • tail -f -n 100 日志文件名View the latest 100 lines of content in real time.
  • tail -n500 -f ./file.log | grep "exception"Query 500 lines of log information, and query the log information that is changing, and use the pipe character to filter (for example, filter exception information).
  • wc -l file1View file line count.

2.4 Create file

  • touch file1.txtCreate a file.
  • echo "hello shyu" > shuyu.txtCreate a file and write "hello shuyu".

2.5 File modification-vim

Old street cat.

Basic operation:

  • vim file1Enter the file1 file editing page, the default is "command mode".
  • iEnter edit mode.
  • ESCSwitch from edit mode to command mode.
  • :Switch to the bottom line command mode and enter commands on the bottom line.

Bottom line command mode:

  • /wordLook under the cursor for a string named word.
  • ?wordLook for a character string named word above the cursor.
  • nRepeat the previous search action.
  • :n1,n2s/word1/word2/gFind the word1 string between lines n1 and n2, and replace the string with word2, for example: 100,200s/shuyu/SHUYU/g
  • :wWrite the edited data to the hard disk file (commonly used).
  • :w!If the file attribute is "read-only", the file is forced to be written.
  • :qLeave vi (used).
  • :q!If you have modified the file and don't want to save it, use ! to force you to leave without saving the file.
  • :wqSave and exit.

2.6 Delete files

  • rm -f file1Delete a file called file1'

2.7 Copying and renaming files

  • mv file1 new_filerename/move a file
  • cp -a /tmp/file .copy a file to the current working directory
  • cp -a file1 file2copy a file

3. Compress or decompress files or directories

3.1 Compression

  • tar -zcvf filename.tar.gz filename: Compress filename into a package in .tar.gz format
  • tar -zcvf filename.tgz filename: Compress filename into a package in .tar format
  • tar -jcvf filename.tar.bz2 filename: Compress filename into a package in .tar.bz2 format
  • zip filename.zip filename: Compress filename into a package in .zip format
  • zip -r file1.zip file1 file2 dir1: Simultaneously compress several files and directories into a compressed package in zip format
  • gzip -d filename.gz filename: Compress filename into a package in .gz format
  • rar -a filename.rar filename: Compress filename into a package in rar format
  • rar a file1.rar file1 file2 dir1: Simultaneously compress 'file1', 'file2' and directory 'dir1'

3.2 Decompression

  • tar -tf archive.tar: Display the contents of a package
  • tar -xvf filename.tar -C /tmp: Decompress a tar format compressed package to the /tmp directory
  • tar -zxvf filename.tar.gz: Decompress a tar.gz format compressed package to the current file
  • tar -zxvf filename.tgz: Decompress a tgz format compressed package to the current file
  • tar -jxvf filename.tar.bz2: Decompress a tar.bz2 format compressed package to the current file
  • tar -xZvf filename.tar.Z: Decompress a tar.Z format archive to the current file
  • unrar e filename.rar: Decompress a rar format compressed package to the current file
  • unrar x filename.rar [path]: Decompress a rar format compressed package to the specified path
  • unzip filename.zip -d [path]: Decompress a compressed package in zip format to the specified path
  • xz -d filename.tar.xz > tar -xvf filename.tar: Decompress a tar.xz format compressed package to the current file

Guess you like

Origin blog.csdn.net/weixin_44988085/article/details/128626643