Linux Commands for Beginners

Linux Commands for Beginners

Common Linux Commands for Beginners

Linux is a powerful and versatile operating system that can be used for various tasks, such as web development, data analysis, system administration, and more. However, to use Linux effectively, you need to know some basic commands that can help you navigate, manipulate, and manage files and directories, processes, network, and system information.

In this blog post, we will introduce some of the most common and useful Linux commands for beginners, along with some examples and explanations. We will also provide a link to a cheat sheet that you can download and use as a reference.

đŸ’¡
Want to practice Linux commands without the fear of ruining the system? DartNode offers VPS a Linux VPS for only $24/ Year.

File and Directory Commands

Files and directories are the basic units of data storage in Linux. You can use the following commands to list, create, delete, move, copy, and edit files and directories in Linux.

- `ls`: This command lists the contents of a directory. You can use various options to customize the output, such as `-l` for a long listing format, `-a` for showing hidden files, and `-h` for human-readable file sizes. For example, `ls -lah` will list all files and directories in the current directory, along with their permissions, owners, sizes, and modification dates.

- `pwd`: This command prints the path of the current working directory. For example, `pwd` might output `/home/user/Documents` if you are in the Documents directory under your home directory.

- `cd`: This command changes the current working directory to the specified one. You can use an absolute path or a relative path to specify the destination directory. For example, `cd /var/log` will change the current directory to `/var/log`, while `cd ..` will move up one level in the directory hierarchy.

- `mkdir`: This command creates a new directory with the specified name. You can use the `-p` option to create parent directories if they do not exist. For example, `mkdir -p projects/linux` will create a directory named `linux` under a directory named `projects`, and create the `projects` directory if it does not exist.

- `rm`: This command deletes files and directories. You can use the `-r` option to delete directories recursively, and the `-f` option to force deletion without prompting. For example, `rm -rf temp` will delete a directory named `temp` and all its contents, without asking for confirmation.

- `cp`: This command copies files and directories. You can use the `-r` option to copy directories recursively, and the `-i` option to prompt before overwriting existing files. For example, `cp -ri old new` will copy a directory named `old` and all its contents to a directory named `new`, and ask for confirmation before overwriting any existing files.

- `mv`: This command moves or renames files and directories. You can use the `-i` option to prompt before overwriting existing files. For example, `mv -i file1 file2` will rename a file named `file1` to `file2`, and ask for confirmation if `file2` already exists.

- `touch`: This command creates a new empty file with the specified name, or updates the modification time of an existing file. For example, `touch test.txt` will create a file named `test.txt` if it does not exist, or update its modification time if it does.

- `file`: This command checks the type of a file, such as text, binary, image, etc. For example, `file image.jpg` might output `image.jpg: JPEG image data, JFIF standard 1.01`.

- `zip` and `unzip`: These commands create and extract ZIP archives, which are compressed files that can contain multiple files and directories. For example, `zip -r archive.zip dir1 dir2` will create a ZIP archive named `archive.zip` that contains the directories `dir1` and `dir2` and all their contents, while `unzip archive.zip` will extract the contents of `archive.zip` to the current directory.

- `tar`: This command archives files without compression in a TAR format, which can be used to store multiple files and directories in a single file. You can use various options to compress or decompress TAR files, such as `-z` for gzip, `-j` for bzip2, and `-J` for xz. For example, `tar -czvf archive.tar.gz dir1 dir2` will create a compressed TAR archive named `archive.tar.gz` that contains the directories `dir1` and `dir2` and all their contents, while `tar -xzvf archive.tar.gz` will extract the contents of `archive.tar.gz` to the current directory.

Text Editing and Processing Commands

Text files are ubiquitous in Linux, as they are used for configuration, documentation, scripting, and more. You can use the following commands to edit, view, and manipulate text files in Linux.

- `nano`, `vi`, and `jed`: These are some of the most popular text editors in Linux, which allow you to create and modify text files interactively. Each editor has its own set of commands and shortcuts, which you can learn by reading their manuals or tutorials. For example, `nano file.txt` will open a file named `file.txt` in nano, `vi file.txt` will open it in vi, and `jed file.txt` will open it in jed.

- `cat`: This command lists, combines, and writes a file's content as a standard output, which can be redirected to another file or command. For example, `cat file1 file2 > file3` will concatenate the contents of `file1` and `file2` and write them to `file3`, while `cat file.txt | grep keyword` will search for the word `keyword` in `file.txt` and print the matching lines.

- `grep`: This command searches for a string within a file or an output, and prints the matching lines. You can use various options to customize the search, such as `-i` for case-insensitive, `-v` for inverted, and `-c` for count. For example, `grep -ic linux file.txt` will count the number of lines that contain the word `linux` in `file.txt`, ignoring the case.

- `sed`: This command finds, replaces, or deletes patterns in a file or an output, using regular expressions. You can use various options to customize the operation, such as `-i` for in-place, `-n` for quiet, and `-e` for multiple expressions. For example, `sed -i 's/linux/LINUX/g' file.txt` will replace all occurrences of the word `linux` with `LINUX` in `file.txt`, modifying the file directly.

- `head`: This command displays the first ten lines of a file or an output, or the specified number of lines if the `-n` option is used. For example, `head -n 5 file.txt` will print the first five lines of `file.txt`.

- `tail`: This command prints the last ten lines of a file or an output, or the specified number of lines if the `-n` option is used. You can also use the `-f` option to follow the file as it grows, which is useful for monitoring log files. For example, `tail -f /var/log/syslog` will print the last ten lines of the system log file and keep updating the output as new lines are added.

- `awk`: This command finds and manipulates patterns in a file or an output, using a scripting language that can perform various actions, such as printing, formatting, arithmetic, and conditional operations. For example, `awk '{print $1, $3}' file.txt` will print the first and third fields of each line in `file.txt`, assuming the fields are separated by spaces.

- `sort`: This command reorders the content of a file or an output, based on various criteria, such as alphabetical, numerical, reverse, or random. You can use various options to customize the sorting, such as `-r` for reverse, `-n` for numerical, and `-u` for unique. For example, `sort -rn file.txt` will sort the lines of `file.txt` in descending numerical order.

- `cut`: This command sections and prints lines from a file or an output, based on a specified delimiter and field. You can use various options to customize the output, such as `-d` for delimiter, `-f` for field, and `-c` for character. For example, `cut -d: -f1 /etc/passwd` will print the first field of each line in the `/etc/passwd` file, using the colon as the delimiter.

- `diff`: This command compares two files' content and prints their differences, line by line. You can use various options to customize the output, such as `-y` for side-by-side, `-u` for unified, and `-q` for brief. For example, `diff -y file1 file2` will compare the contents of `file1` and `file2` and print them in two columns, indicating the differences with symbols such as `<`, `>`, and `|`.

- `tee`: This command prints the command outputs in Terminal and a file, simultaneously. You can use the `-a` option to append the output to an existing file, instead of overwriting it. For example, `ls -l | tee output.txt` will list the contents of the current directory in a long listing format, and write the output to a file named `output.txt` as well as displaying it on the screen.

System and Network Commands

Linux is a multi-user and multi-tasking operating system that can run on various platforms, such as servers, desktops, laptops, and embedded devices. You can use the following commands to monitor, manage, and troubleshoot the system and network performance, security, and configuration.

- `ps`: This command lists the processes that are running on the system, along with their process IDs, statuses, CPU and memory usage, and other information. You can use various options to customize the output, such as `-e` for all processes, `-u` for user processes, and `-f` for full format. For example, `ps -ef` will list all processes in a full format, showing the parent process ID, start time, command line, and other details.

- `top`: This command displays the dynamic information about the system and the processes, such as CPU and memory usage, load average, uptime, and process statistics. You can use various commands within the top interface to interact with the output, such as `h` for help, `q` for quit, `k` for kill, and `n` for number of processes. For example, `top -n 5` will display the top interface with only five processes shown.

- `kill`: This command sends a signal to a process to terminate it, using its process ID. You can use various signals to specify the type of termination, such as `SIGTERM` for graceful, `SIGKILL` for forceful, and `SIGSTOP` for pause. For example, `kill -9 1234` will send a `SIGKILL` signal to the process with the ID 1234, which will terminate it immediately and unconditionally.

- `ping`: This command tests the connectivity and latency between the local system and a remote host, using the Internet Control Message Protocol (ICMP). You can use various options to customize the output, such as `-c` for count, `-i` for interval, and `-t` for time to live. For example, `ping -c 4 www.bing.com` will send four ICMP packets to the website www.bing.com and display the response time and packet loss statistics.

- `traceroute`: This command traces the route and measures the transit delays of packets across the network, from the local system to a remote host. You can use various options to customize the output, such as `-n` for numeric, `-I` for ICMP, and `-T` for TCP. For example, `traceroute -n www.bing.com` will display the IP addresses and round-trip times of the intermediate hops between the local system and the website www.bing.com.

- `ssh`: This command establishes a secure shell (SSH) connection to a remote host, using encryption and authentication. You can use various options to customize the connection, such as `-p` for port, `-i` for identity file, and `-X` for X11 forwarding. For example, `ssh -p 2222 user@host` will connect to the remote host using the username `user`, the port 2222, and the default identity file.

- `scp`: This command copies files securely between the local system and a remote host, using SSH. You can use various options to customize the copying, such as `-r` for recursive, `-p` for preserve, and `-q` for quiet. For example, `scp -r user@host:dir1 .` will copy the directory `dir1` and all its contents from the remote host to the current directory on the local system, using the username `user` and the default port and identity file.

- `curl`: This command transfers data from or to a server, using various protocols, such as HTTP, FTP, SMTP, and more. You can use various options to customize the transfer, such as `-o` for output, `-d` for data, and `-H` for header. For example, `curl -o file.html https://www.bing.com` will download the HTML file from the website www.bing.com and save it as `file.html` on the local system.

- `wget`: This command downloads files from the web, using HTTP, HTTPS, or FTP protocols. You can use various options to customize the download, such as `-c` for continue, `-r` for recursive, and `-O` for output. For example, `wget -r -O index.html https://www.bing.com` will download the website www.bing.com and all its linked files and save them as `index.html` and other files on the local system.

- `ifconfig`: This command configures and displays the network interfaces on the system, such as Ethernet, Wi-Fi, and loopback. You can use various options to customize the configuration, such as `-a` for all, `-s` for short, and `-up` or `-down` for activate or deactivate. For example, `ifconfig -a` will display the information of all network interfaces on the system, such as IP address, netmask, broadcast, and MAC address.

- `iwconfig`: This command configures and displays the wireless network interfaces on the system, such as Wi-Fi and Bluetooth. You can use various options to customize the configuration, such as `-a` for all, `-s` for short, and `-essid` or `-ap` for network name or access point. For example, `iwconfig -s` will display the information of all wireless network interfaces on the system, such as mode, frequency, quality, and encryption.

- `netstat`: This command displays the network connections, routing tables, statistics, and more on the system. You can use various options to customize the output, such as `-a` for all, `-n` for numeric, and `-t` or `-u` for TCP or UDP. For example, `netstat -an` will display all network connections on the system, using IP addresses and port numbers instead of hostnames and service names.