<p>The core commands that must be mastered include ls, cd, pwd, mkdir, touch, cp, mv, rm and file viewing tools cat, less, head, tail, and make good use of Tab completion to improve efficiency; 2. File permissions are composed of rwx, set permission numbers through chmod (such as 755), use chown to modify the users and groups to ensure that the script has execution permissions; 3. Use pipeline (|) to connect commands, and combine grep, awk, sed, cut, sort, uniq and other tools to process text, such as history | awk '{print $2}' | sort | uniq -c | sort -nr | head -5 to count the most commonly used commands; 4. Be familiar with the root directory structure such as /, /home, /etc, /var/log, and use find, locate, grep -r perform file and content search, which or type locate program path; 5. Use ps aux, top/htop to monitor the process, kill or killall to terminate the task, & implement background operation, crontab -e to configure timing tasks; 6. Remote login through ssh, securely transfer files through scp and rsync, it is recommended to configure SSH key to log in without password; 7. Set alias alias in ~/.bashrc such as ll='ls -alF', custom prompt, use Ctrl R to search for historical commands, write .sh scripts and chmod x to give execution permissions to automate repeated work; continue to practice, learn a new command every week, and consult the man manual when encountering problems, and gradually build confidence and proficiency. </p>
<p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175417248277797.jpg" class="lazy" alt="Mastering the Linux Command Line: A Comprehensive Guide"></p>
<p> Mastering the Linux command line isn't about memorizing every command—it's about understanding core principles, building muscle memory, and knowing how to solve problems efficiently. Whether you're managing a server, automating tasks, or just exploring Linux, the terminal is your most powerful tool. Here's a practical guide to help you go from beginner to confident user. </p>
<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175417248394047.jpeg" class="lazy" alt="Mastering the Linux Command Line: A Comprehensive Guide"><hr>
<h3> 1. <strong>Essential Commands You Should Know</strong>
</h3>
<p> Start with the basics. These commands form the foundation of daily Linux use:</p>
<ul>
<li> <code>ls</code> – List directory contents<br> Use <code>ls -l</code> for details, <code>ls -a</code> to show hidden files.</li>
<li> <code>cd</code> – Change directory<br> <code>cd ..</code> moves up, <code>cd ~</code> goes to your home folder.</li>
<li> <code>pwd</code> – Print working directory<br> Tells you exactly where you are.</li>
<li> <code>mkdir</code> – Create a directory<br> <code>mkdir project</code> creates a folder named "project".</li>
<li> <code>touch</code> – Create an empty file or update timestamp<br> <code>touch notes.txt</code> makes a new file.</li>
<li> <code>cp</code> and <code>mv</code> – Copy and move files<br> <code>cp file.txt backup/</code> , <code>mv old.txt new.txt</code>
</li>
<li> <code>rm</code> – Remove files<br> Be careful: <code>rm -r folder/</code> deletes recursively (and permanently).</li>
<li> <code>cat</code> , <code>less</code> , <code>head</code> , <code>tail</code> – View file contents<br> <code>less bigfile.log</code> lets you scroll safely; <code>tail -f log.txt</code> monitors real-time updates.</li>
</ul>
<blockquote>
<p> Pro tip: Use <code>tab</code> completion to save time and avoid typos. Type <code>cat fil</code> and press Tab—it'll autocomplete if there's only one match. </p>
<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175417248428539.jpeg" class="lazy" alt="Mastering the Linux Command Line: A Comprehensive Guide">
</blockquote>
<hr>
<h3> 2. <strong>File Permissions and Ownership</strong>
</h3>
<p> Linux takes security seriously. Understanding permissions is key:</p>
<p> Run <code>ls -l</code> and you'll see something like: </p>
<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175417248526050.jpeg" class="lazy" alt="Mastering the Linux Command Line: A Comprehensive Guide"><pre class='brush:php;toolbar:false;'> -rw-r--r-- 1 user group 1024 Oct 5 10:00 file.txt</pre><p> Breakdown:</p><ul><li> First character: file type ( <code>-</code> = regular file, <code>d</code> = directory)</li><li> Next 9 characters: permissions in three groups (user, group, others)<ul><li> <code>r</code> = read, <code>w</code> = write, <code>x</code> = execute</li></ul></li></ul><p> To change permissions:</p><pre class='brush:php;toolbar:false;'> chmod 755 script.sh</pre><p> This gives owner full access (read write execute = 7), and read execute to group and others (5).</p><p> To change ownership:</p><pre class='brush:php;toolbar:false;'> sudo chown alice:developers file.txt</pre><blockquote><p> Always double-check permissions on scripts and config files. A missing <code>x</code> means the script won't run.</p></blockquote><hr /><h3 id="strong-Pipes-Redirection-and-Text-Processing-strong"> 3. <strong>Pipes, Redirection, and Text Processing</strong></h3><p> One of the command line's superpowers is chaining commands together.</p><ul><li><p> <strong>Pipes ( <code>|</code> )</strong> send output from one command to another:</p><pre class='brush:php;toolbar:false;'> ps aux | grep nginx</pre><p> Shows all processes, then filters for "nginx".</p></li><li><p> <strong>Redirection</strong> saves or feeds data:</p><ul><li> <code>></code> overwrites a file: <code>echo "Hello" > output.txt</code></li><li> <code>>></code> appends: <code>date >> log.txt</code></li><li> <code><</code> feeds input: <code>sort < names.txt</code></li></ul></li></ul><p> Useful text tools:</p><ul><li> <code>grep</code> – Search text: <code>grep "error" /var/log/syslog</code></li><li> <code>awk</code> – Extract fields: <code>awk &#39;{print $1}&#39; data.txt</code> prints first column</li><li> <code>sed</code> – Stream editor: <code>sed &#39;s/foo/bar/g&#39; file.txt</code> replaces "foo" with "bar"</li><li> <code>cut</code> , <code>sort</code> , <code>uniq</code> – Manipulate and clean data</li></ul><blockquote><p> Example: Find top 5 most-used commands in your history:</p><pre class='brush:php;toolbar:false;'> history | awk &#39;{print $2}&#39; | sort | uniq -c | sort -nr | head -5</pre></blockquote><hr /><h3 id="strong-Filesystem-Navigation-and-Searching-strong"> 4. <strong>Filesystem Navigation and Searching</strong></h3><p> Know your way around the directory tree:</p><ul><li> <code>/</code> – Root</li><li> <code>/home</code> – User directories</li><li> <code>/etc</code> – Configuration files</li><li> <code>/var/log</code> – Logs</li><li> <code>/tmp</code> – Temporary files</li></ul><p> Search for files:</p><ul><li> <code>find /home -name "*.conf"</code> – Find all <code>.conf</code> files in <code>/home</code></li><li> <code>find . -mtime -7</code> – Files modified in the last 7 days</li><li> <code>locate filename</code> – Fast search (uses a database; run <code>updatedb</code> if missing)</li></ul><p> Search inside files:</p><ul><li> <code>grep -r "password" /etc/</code> – Recursively search for "password" in <code>/etc</code></li></ul><blockquote><p> Use <code>which command</code> or <code>type command</code> to find where a program is located.</p></blockquote><hr /><h3 id="strong-Process-Management-and-System-Monitoring-strong"> 5. <strong>Process Management and System Monitoring</strong></h3><p> See what's running:</p><ul><li> <code>ps aux</code> – List all processes</li><li> <code>top</code> or <code>htop</code> – Live system monitor (install <code>htop</code> for a better UI)</li><li> <code>kill PID</code> – Terminate a process by ID</li><li> <code>killall firefox</code> – Kill all processes named "firefox"</li></ul><p> Run background jobs:</p><ul><li> Add <code>&</code> to run in background: <code>ping google.com &</code></li><li> Use <code>jobs</code> to list background tasks, <code>fg</code> to bring one to foreground</li></ul><p> Schedule recurring tasks with <code>cron</code> : Edit your crontab with <code>crontab -e</code> :</p><pre class='brush:php;toolbar:false;'> # Run backup script every day at 2 AM
0 2 * * * /home/user/scripts/backup.sh</pre><hr /><h3 id="strong-Remote-Access-and-File-Transfer-strong"> 6. <strong>Remote Access and File Transfer</strong></h3><ul><li> <code>ssh user@server.com</code> – Securely log into a remote machine</li><li> <code>scp file.txt user@remote:/path/</code> – Copy files over SSH</li><li> <code>rsync -avz folder/ user@remote:backup/</code> – Sync directories efficiently (great for backups)</li></ul><blockquote><p> Tip: Set up SSH keys to avoid typing passwords every time.</p></blockquote><hr /><h3 id="strong-Customize-Your-Shell-Bash-Tips-strong"> 7. <strong>Customize Your Shell (Bash Tips)</strong></h3><p> Make the terminal work for you:</p><ul><li><p> <strong>Aliases</strong> : Shortcuts for long commands<br /> Add to <code>~/.bashrc</code> :</p><pre class='brush:php;toolbar:false;'> alias ll=&#39;ls -alF&#39;
alias gs=&#39;git status&#39;</pre></li><li><p> <strong>Prompt customization</strong><br /> Change what your prompt looks like (eg, add git branch, color).</p></li><li><p> <strong>History tricks</strong><br /> Press <code>Ctrl R</code> to search command history. Type part of a command and it finds matches.</p></li><li><p> <strong>Scripts</strong><br /> Save repetitive tasks in <code>.sh</code> files:</p><pre class='brush:php;toolbar:false;'> #!/bin/bash
echo "Backing up..."
cp -r ~/docs/backup/</pre><p> Make it executable: <code>chmod x backup.sh</code> , then run with <code>./backup.sh</code> .</p>
<hr>
<p> Mastering the Linux command line takes practice, but you don't need to know everything at once. Focus on daily use, learn one new command or trick per week, and always read the manual ( <code>man command</code> ) when in doubt.</p>
<p> Basically, just keep typing.</p>
The above is the detailed content of Mastering the Linux Command Line: A Comprehensive Guide. For more information, please follow other related articles on the PHP Chinese website!