Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Essential Linux Commands for Directory and File Operations

Notes 2

Directory Operations

Displaying and Navigating Directories

  • pwd: Show the current working directory.
  • clear: Clear the terminal screen.
  • cd ~: Navigate to the current user's home directory.
  • cd /: Navigate to the root directory.
  • cd -: Return to the previous directory visited.
  • cd ..: Move up one directory level.
  • Ctrl+R: Search through command history by typing any character from a command.

Viewing Directory Contents

  • ll: List detailed contents of the current directory.
  • ls: List basic contents of the current directory.

Creating Directories

  • mkdir aaa: Create a directory named aaa in the current directory.
  • mkdir /usr/aaa: Create a directory named aaa in the specified /usr directory.

Searching for Files and Directories

  • find / -name 'b': Search the root directory (including subidrectories) for files and directories named exactly b.
  • find / -name 'b*': Search the root directory (including subdirectories) for files and directories starting with b.

Renaming and Moving Directories

  • mv original_directory new_name: Rename a directory; e.g., mv test001 test.
  • mv /aaa /bb: Move the aaa directory from the root to the bb directory.
  • mv bbbb usr/bbb: Move the bbbb directory from the current directory to usr, renaming it to bbb.
  • mv bbb usr/aaa: Move the bbb directory from the current directory to usr, renaming it to aaa.

Copying Directories

  • cp /usr/tmp/aaa /usr: Copy the aaa directory from /usr/tmp to /usr.

Deleting Directories

  • rm -r /bbb: Delete the /bbb directory with confirmation for each file.
  • rm -rf /bbb: Force delete the /bbb directory and all its subdirectories without prompts.

File Operations

Deleting Files

  • rm -r a.java: Delete a.java in the current directory with confirmation.
  • rm -rf a.java: Force delete a.java in the current directory.
  • rm -rf ./a*: Force delete all files starting with a in the current directory.
  • rm -rf ./*: Force delete all files in the current directory (use with caution).
  • rm –f *.txt: Force delete all files with .txt extension.

Creating Files

  • touch test: Create an empty file named test.

Editing Files with vi/vim

Modes in vim

  • Command Mode: Default mode when vim starts; press Esc multiple times to return to this mode from any other mode.
  • Insert Mode: Press i to enter; allows text input; press Esc to return to Command Mode.
  • Ex Mode: Press : to enter; cursor moves to the bottom for saving, quitting, or executing commands.

Common Commands in Command Mode

  • i: Insert text before the cursor.
  • o: Insert a new line below the current line.
  • dd: Delete the entire current line.
  • yy: Copy the current line to the buffer.
  • n+yy: Copy n lines to the buffer.
  • p: Paste buffer contents after the cursor.
  • u: Undo the last operation.
  • r: Replace the current character.
  • /: Search for a keyword.

Common Commands in Ex Mode

  • :w: Save changes.
  • :q: Quit vim.
  • :q!: Force quit without saving changes.
  • :x: Save and quit, equivalent to :wq.
  • :set number: Display line numbers.
  • :! system_command: Execute a system command and show results.
  • :sh: Switch to the command line; use Ctrl+D to return to vim.

Output and Viewing Commands

  • echo: Display input content; e.g., echo "liuyazhuang" >> liuyazhuang.txt appends text to a file.
  • cat: Display file contents or merge multiple files; e.g., cat test.txt shows content, cat test.txt test2.txt > readme.txt merges files.
  • head: Display the first few lines of a file (default 10); e.g., head -n 20 filename shows first 20 lines.
  • tail: Display the last few lines of a file (default 10); e.g., tail -f filename continuously displays new content (useful for logs).
  • more: Page through file content downward only; e.g., ls -al | more.
  • less: Page through file content with up/down navigation; e.g., ls -al | less.

Hard Links and Soft Links

Creating Soft Links

  • ln -s test.txt link_1: Create a soft link named link_1 pointing to test.txt.
  • ln -sf test.txt link_2: Force replace an existing soft link with link_2.
  • ln -sv /opt/test1 /opt/test2: Create a soft link for a directory.

Creating Hard Links

  • ln real.txt link_2: Create a hard link named link_2; modifications affect the source file.

Compression and Decompression

Using tar

  • tar -cvf start.tar a.java b.java: Create a tar archive start.tar from specified files.
  • tar -cvf start.tar ./*: Archive all files in the current directory.
  • tar -zcvf start.tar.gz a.java b.java: Create a gzipped tar archive.
  • tar -zcvf start.tar.gz ./*: Gzip archive all files in the current directory.
  • tar -xvf start.tar: Extract start.tar to the current directory.
  • tar -xvf start.tar -C /usr/local: Extract to a specified directory.
  • tar -zxvf start.tar.gz: Extract a gzipped tar archive to the current directory.
  • tar -zxvf start.tar.gz -C /usr/local: Extract to a specified directory.

Using zip and unzip

  • zip lib.zip tomcat.jar: Compress a single file into lib.zip.
  • zip -r lib.zip lib/: Compress a directory into lib.zip.
  • zip -r lib.zip tomcat-embed.jar xml-aps.jar: Compress multiple files into lib.zip.
  • unzip file1.zip: Extract a zip archive.
  • unzip -d /usr/app/ test.zip: Extract test.zip to a specified directory.

Search Commands

Using grep

  • ps -ef | grep sshd: Find processes related to SSH service.
  • ps -ef | grep sshd | grep -v grep: Exclude the grep process itself.
  • ps -ef | grep sshd -c: Count the number of specified processes.

Using find

  • find . -name "*.log" -ls: Find .log files in the current directory with details.
  • find /root/ -perm 600: Find files in /root/ with permission 600.
  • find . -type f -name "*.log": Find regular files ending with .log.
  • find . -type d | sort: List and sort all directories in the current directory.
  • find . -size +100M: Find files larger than 100MB.
  • find . -name *liuyazhuang*: Find files containing "liuyazhuang" in their name.
  • find / -perm 777: Find all files with permission 777.

Using locate

  • locate keyword: Quickly search for files or folders by keyword.

Yum Commands

  • yum install iptables-services: Install a package.
  • yum list: List all installed packages.
  • yum search package_name: Search for a package in repositories.
  • yum update package_name.rpm: Update all installed RPM packages.
  • yum update package_name: Update a specific package.
  • yum remove package_name: Remove a package.
  • yum clean all: Clear all cached packages and headers.

System Services

  • service iptables status: Check the status of iptables service.
  • service iptables start: Start the service.
  • service iptables stop: Stop the service.
  • service iptables restart: Restart the service.
  • chkconfig iptables off: Disable service auto-start on boot.
  • chkconfig iptables on: Enable service auto-start on boot.

User Management

  • su - username: Switch user and change directory.
  • exit: Log out of the current account.
  • Note: Using su without a username switches to root, but it is not recommended for security reasons.
  • which: Find the location of a command; e.g., which ls outputs /bin/ls.
  • /etc/passwd: File storing user information.
  • /usr/bin/passwd: Program for changing user passwords.

Other Commands

Process Management

  • ps -ef: View all running processes.
  • kill pid or kill -9 pid: Terminate a process (force kill with -9).

Network Commands

  • ifconfig or ifconfig | more: View network interface information.
  • netstat -an: View current system ports.
  • netstat -an | grep 8080: Search for a specific port.
  • setup: Configure network settings.
  • service network restart: Restart network services.
  • chkconfig iptables off: Disable firewall auto-start.
  • iptables -L: List firewall rules.
  • iptables -F: Flush firewall rules.
  • service iptables stop: Stop the firewall service.

File Permissions

  • chmod 777: Change file permissions to read, write, and execute for all.

System Information

  • Ctrl + L: Clear the terminal screen.
  • du -lh: Check disk usage of directories.
  • free: View system memory usage.
  • df -lh: Check filesystem partition usage.
  • date: View or set the system time; e.g., date +%Y--%m--%d for formatted output.
  • date -s: Set the system time.
  • uptime: Check system uptime.

Network Connectivity

  • ping www.baidu.com: Test connectivity to a host.
  • telnet baidu.com 80: Check if a remote port is open; "Connected" indicates success.

Network Connectivity Test

Related Articles

Designing Alertmanager Templates for Prometheus Notifications

How to craft Alertmanager templates to format alert messages, improving clarity and presentation. Alertmanager uses Go’s text/template engine with additional helper functions. Alerting rules referenc...

Deploying a Maven Web Application to Tomcat 9 Using the Tomcat Manager

Tomcat 9 does not provide a dedicated Maven plugin. The Tomcat Manager interface, however, is backward-compatible, so the Tomcat 7 Maven Plugin can be used to deploy to Tomcat 9. This guide shows two...

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.