Introduction
The command line interface (CLI) in Linux offers a powerful and efficient way to interact with the operating system. This article will guide you through essential command line tools and techniques in Linux.
From basic navigation and file manipulation to process management, you’ll gain a solid foundation in using the command line effectively. By mastering these essential skills, we become more proficient in managing our Linux systems and executing various tasks efficiently.
- Changing Directories:
cdcommand: Change to a specific directory. For example,cd /path/to/directorymoves to the specified directory.cd ..command: Move up one directory level. For instance,cd ..moves to the parent directory.
- Listing Files and Directories:
lscommand: List files and directories in the current directory. Use options like-lfor a detailed list and-ato include hidden files.
- Creating Directories and Files:
mkdircommand: Create a new directory. For example,mkdir new_directorycreates a directory named “new_directory”.touchcommand: Create a new empty file. Usetouch filenameto create a file named “filename”.
- Copying Files and Directories:
cpcommand: Copy files and directories. For instance,cp file.txt /path/to/destinationcopies “file.txt” to the specified destination.cp -rcommand: Copy directories and their contents recursively. Usecp -r directory /path/to/destinationto copy the directory and its contents.
- Moving and Renaming Files and Directories:
mvcommand: Move or rename files and directories. For example,mv file.txt /path/to/destinationmoves the file to the specified destination. Usemv oldname newnameto rename a file or directory.
- Removing Files and Directories:
rmcommand: Remove files. For instance,rm file.txtdeletes the specified file.rm -rcommand: Remove directories and their contents recursively. Userm -r directoryto delete the directory and its contents.
- Searching for Files and Text:
findcommand: Search for files and directories based on various criteria. For example,find /path/to/search -name filenamesearches for a file named “filename” in the specified path.grepcommand: Search for text patterns within files. Usegrep pattern file.txtto search for “pattern” in “file.txt”.
- Viewing File Contents:
catcommand: Display the contents of a file. For instance,cat file.txtshows the contents of “file.txt”.lesscommand: View file contents page by page. Useless file.txtto view the contents and navigate using the arrow keys or the Page Up/Page Down keys.
- Managing Processes:
pscommand: Display information about running processes. Use options likeps auxto show a detailed list of all processes, including system and user processes.topcommand: Monitor real-time information about running processes, including CPU and memory usage. Pressqto exit thetopcommand.
- Process Control:
killcommand: Terminate a process by its process ID (PID). For example,kill PIDterminates the process with the specified PID.killallcommand: Terminate processes by their name. Usekillall process_nameto terminate all processes with the specified name.
- Process Prioritization:
nicecommand: Launch a command with a specific priority. For instance,nice -n 10 commandstarts the command with a lower priority (higher nice value).renicecommand: Change the priority of running processes. Userenice priority PIDto adjust the priority of a running process.
- Background and Foreground Execution:
&operator: Execute a command in the background. For example,command &runs the command in the background, allowing you to continue using the terminal.fgcommand: Bring a background process to the foreground. Usefg %job_numberto resume a background process.
- Monitoring Process Resources:
htopcommand: Interactive process viewer with real-time resource monitoring. It provides a more user-friendly and feature-rich alternative totop.atopcommand: Advanced system and process monitor that records resource usage. Useatopto view historical information about processes and system activity.
- Process Information:
/procdirectory: Explore the/procdirectory to access detailed information about processes. Each process has a corresponding directory with its process ID (PID).