Common Linux Commands

Common Commands

Command English Function
ls list View the contents (files/directories) of the current directory
pwd print work directory View the current directory
cd [directory name] change directory Switch directories
touch [file name] touch Create a new file if it does not exist
mkdir [directory name] make directory Create a directory
rm [file name] remove Delete a specified file

Usage Tips:

  • Use the Tab key for auto-completion.
  • Press the Tab key twice to get operation suggestions.
  • Use the up and down arrow keys to quickly recall previously used commands.
  • Use the clear command or the Ctrl + L shortcut to clear the screen.

Linux Command Format

1
command [-options] [parameter]

Explanation:

  • command: The name of the command.
  • [-options]: Optional flags to modify the command behavior (can be omitted).
  • [parameter]: Parameters passed to the command (can be none, one, or multiple).

Note:

  • Square brackets ([ ]) indicate optional elements.
  • There should be spaces between the command name, options, and parameters.

Examples:

  • ls: This is the command name.
  • ls -l: Here, ls is the command name, and -l is the option, which specifies the format for displaying files in the directory.
  • cd /root: cd is the command name, and /root is the parameter.
  • touch 1.txt 2.txt 3.txt: touch is the command name, used to create files, followed by multiple parameters separated by spaces.

File and Directory Commands

1. ls Command

Function: Displays the contents of a specified directory.

Syntax:

1
ls [-a or -l or -al] dir

Explanation:

  • -a: Shows all files and directories, including hidden ones (those starting with .), e.g., ls -a.
  • -l: Displays detailed information about files and directories, such as type (d for directory, - for file), permissions, owner, file size, etc., e.g., ls -l.
  • ls -al: Combines -a and -l to show all files and directories (including hidden ones) with detailed information.
  • ls -al /etc: Here, /etc is the directory to be listed.

Note:
The ls -l command can be simplified to ll.

2. cd Command

Function: Switches the current working directory.

Syntax:

1
cd [dirName]

Special Cases:

  • ~: Represents the user’s home directory.
  • .: Represents the current directory.
  • ..: Represents the parent directory of the current directory.

Examples:

  • cd ..: Switch to the parent directory.
  • cd ~: Switch to the user’s home directory.
  • cd /usr/local: Switch to the /usr/local directory.

3. cat Command

Function: Displays the contents of a file.

Syntax:

1
cat [-n] fileName

Explanation:

  • -n: Numbers all output lines starting from 1.

Examples:

  • cat /etc/profile: View the contents of the profile file in the /etc directory.
  • cat -n /etc/profile: View the contents of the profile file with line numbers.

4. more Command

Function: Displays file contents in a paginated format, which is more convenient than cat for long files.

Syntax:

1
more fileName

Instructions:

  • Press Enter: Scroll down one line.
  • Press Space: Scroll down one screen.
  • Press b: Go back one screen.
  • Press q or Ctrl + C: Exit more.

Example:
more /etc/profile: Displays the profile file in a paginated format.

5. tail Command

Function: Displays the last part of a file.

Syntax:

1
tail [-f] fileName

Explanation:

  • -f: Dynamically reads and displays the last part of the file, useful for monitoring log files.

Examples:

  • tail /etc/profile: Displays the last 10 lines of the profile file by default.
  • tail -20 /etc/profile: Displays the last 20 lines of the profile file.
  • tail -f ~/my.log: Dynamically reads and displays the last part of my.log. You can add content to the file in another terminal window using echo 'text' >> my.log, and the changes will appear in real time.

6. mkdir Command

Function: Creates directories.

Syntax:

1
mkdir [-p] dirName

Explanation:

  • -p: Ensures parent directories are created if they don’t already exist, allowing the creation of nested directories.

Examples:

  • mkdir kathy: Creates a directory named kathy in the current directory.
  • mkdir -p kathy/test: Creates a nested directory test inside kathy. If kathy does not exist, it will also be created.

7. rmdir Command

Function: Deletes empty directories.

Syntax:

1
rmdir [-p] dirName

Explanation:

  • -p: If the parent directory becomes empty after deleting the subdirectory, it will also be removed.

Examples:

  • rmdir kathy: Deletes the kathy directory if it is empty.
  • rmdir -p kathy/test: Deletes the test subdirectory inside kathy, and deletes kathy if it becomes empty.
  • rmdir kathy*: Deletes all empty directories starting with kathy.

8. rm Command

Function: Deletes files or directories, including non-empty ones.

Syntax:

1
rm [-rf] name

Explanation:

  • -r: Recursively deletes directories and their contents.
  • -f: Force delete without confirmation.

Examples:

  • rm -r itcast/: Deletes the itcast directory and all its contents, with confirmation.
  • rm -rf itcast/: Deletes the itcast directory and all its contents without confirmation.
  • rm -f hello.txt: Deletes the hello.txt file without confirmation.

Copying Command cp

Function: Copies files or directories.

Syntax:

1
cp [-r] source dest

Explanation:

  • -r: Required for copying directories. Not needed for files.

Examples:

  • cp hello.txt itcast/: Copies hello.txt to the itcast directory.
  • cp hello.txt ./hi.txt: Copies hello.txt to the current directory and renames it to hi.txt.
  • cp -r itcast/ ./itheima/: Copies the itcast directory and its contents to the itheima directory.

Moving Command mv

Function: Renames files or directories or moves them to another location.

Syntax:

1
mv source dest

Examples:

  • mv hello.txt hi.txt: Renames hello.txt to hi.txt.
  • mv hi.txt itheima/: Moves hi.txt to the itheima directory.

Packaging and Compression Command: tar

Function: Used for packaging, unpacking, compressing, and decompressing files.

Installation:
On CentOS, the tar compression and decompression tool is not pre-installed. If the tar command is missing, you will see the error -bash: tar: Command Not Found. To resolve this, install tar using:

1
yum install -y tar

Syntax:

1
tar [-zcxvf] output_file_name [files_to_process]
  • Files with a .tar suffix: Only packaged, not compressed.
  • Files with a .tar.gz suffix: Packaged and compressed.

Options:

  • -z: Use gzip for compression or decompression.
  • -c: Create a new package file (packaging).
  • -x: Extract files from a package file (unpacking).
  • -v: Display the command execution process (verbose).
  • -f: Specify the name of the package file.
  • [files]: Specify which files to package.

Examples:

  • tar -cvf 01.tar 01: Package the 01 directory into a file named 01.tar.
  • tar -zcvf 01.tar.gz 01: Package and compress the 01 directory into a file named 01.tar.gz.
  • tar -xvf 01.tar: Unpack the 01.tar file into the current directory. The default extracted directory name will be 01.
  • tar -zxvf 01.tar.gz: Decompress and unpack the 01.tar.gz file into the current directory (commonly used).
  • tar -zxvf 01.tar.gz -C /another_directory: Decompress and unpack the file into a specified directory.

Text Editing Command vi/vim

1. vi Command

Function: The vi command is a text editor provided by Linux systems, used for editing file contents, similar to Notepad in Windows.

Syntax:

1
vi fileName

Notes:

  1. vim is an enhanced version of vi, offering additional features like syntax highlighting, making it more convenient for editing. In practical use, vim is more commonly used.
  2. To use the vim command, you may need to install it first:
    1
    yum install vim

2. vim Command

Function: Used for editing file contents. Essentially, vim is a text editor.

Syntax:

1
vim fileName

Notes:

  1. If the specified file exists, vim will open it; if it does not exist, a new file will be created.
    Example: vim HelloWorld.java.
  2. vim operates in three modes: Command Mode, Insert Mode, and Last Line Mode. You need to be aware of the current mode while using vim.

vim Modes Explanation

  1. Command Mode:

    • Used for viewing file content and moving the cursor (e.g., using arrow keys, gg to move to the start, G to move to the end).
    • This is the default mode when opening a file with vim.
    • To switch to other modes, you must first return to Command Mode.
  2. Insert Mode:

    • Used for editing file content.
    • Switch from Command Mode to Insert Mode by pressing i, a, or o. Once in Insert Mode, you will see [insert] at the bottom.
    • Press ESC to return to Command Mode.
  3. Last Line Mode:

    • Used for tasks like searching for content, displaying line numbers, or exiting.
    • Switch to Last Line Mode from Command Mode by pressing : or /.
    • /text: Search for “text” in the file. (Exit with q! after searching.)
    • :wq: Save and exit.
    • :q!: Exit without saving.
    • :set nu: Display line numbers.

Search Command

1. find Command

Function: Search for files in a specified directory.

Syntax:

1
find dirName -option fileName

Examples:

  • find . -name "*.java": Search for all files ending with .java in the current directory and its subdirectories.
    Here:

    • .: Refers to the current directory.
    • -name: Specifies the search criteria based on file names.
    • *.java: Uses a wildcard to match files ending with .java.
  • find /itcast -name "*.java": Search for all files ending with .java in the /itcast directory and its subdirectories.

2. grep Command

Function: Search for specific text content in a specified file.

Syntax:

1
grep word fileName
  • word: The text to search for.
  • fileName: The file to search in.

Examples:

  • grep Hello HelloWorld.java: Find all occurrences of the string Hello in the HelloWorld.java file.
  • grep hello *.java: Search for all occurrences of the string hello in all .java files in the current directory.