Linux Command Line 101: A Beginner’s Guide to Essential Commands
Linux is a powerful and versatile operating system that has gained immense popularity among developers and system administrators. It offers a variety of command-line tools and utilities that help users manage their systems efficiently.
Let’s discuss why you should use Linux.
- Software Development 🚀
Linux is the preferred operating system for software developers. It offers a range of development tools, programming languages, and libraries that make it easy to develop and test software. - Web Hosting 🌐
Many web servers run on Linux as it is stable and secure, and offers a range of tools and features to host websites and web applications. - Privacy and Security 🔒
Linux is a highly secure operating system, making it an ideal choice for individuals and businesses concerned about privacy and security. - Customization and Control (OpenSource)) 🛠️
Linux offers a high degree of customization and control, allowing users to tailor it to their specific needs. Users can choose from a range of desktop environments, window managers, and tools to create their ideal computing environment.
If you’re new to Linux, you might be overwhelmed by the number of commands available to you. But don’t worry! In this article, I’ll provide you with a comprehensive guide to Linux commands that will help you get started.
Basic Linux Commands
1. lsls
: list all files and directories in the current directory
ls -l
: list files and directories in the current directory with additional details, including permissions, ownership, and sizels /path/to/directory
: list all files and directories in the specified directory
$ ls
file1.txt file2.txt directory1
$ ls -l
-rw-r--r-- 1 user user 1024 Mar 10 14:15 file1.txt
-rw-r--r-- 1 user user 512 Mar 10 14:20 file2.txt
drwxr-xr-x 2 user user 4096 Mar 10 14:25 directory1
$ ls /home/user/Documents
document1.txt document2.pdf directory2ruby
2. echo
echo
- it is used to display a line of text/string on the standard output.
echo "Hello World"
Hello World
3. pwdpwd
— present working directory
pwd
- It is used to display the current working directory in the terminal.
pwd
/home/user/Documents
current_dir=$(pwd)
echo "The current directory is: $current_dir"
4. cdcd
- change directory
cd /path/to/directory
: change the current working directory to the specified directorycd ..
: move up one level in the directory hierarchycd
: change to the user's home directory
$ cd /home/user/Documents
$ pwd
/home/user/Documents
$ cd ..
$ pwd
/home/user
$ cd
$ pwd
/home/user
5. mkdirmkdir
- create a new directory
mkdir directory_name
: create a new directory with the specified namemkdir -p /path/to/directory
: create a new directory and all necessary parent directories
$ mkdir directory1
$ ls
directory1 file1.txt file2.txt
$ mkdir -p directory2/subdirectory1/subdirectory2
$ ls
directory1 directory2 file1.txt file2.txt
$ ls directory2
subdirectory1
$ ls directory2/subdirectory1
subdirectory2
6. rmrm
- remove a file or directory
rm file.txt
: remove the specified filerm -r directory
: remove the specified directory and all of its contents (use with caution!)
$ rm file2.txt
$ ls
directory1 file1.txt
$ rm -r directory2
$ ls
directory1 file1.txt
7. cpcp
- copy a file or directory
cp file.txt /path/to/destination
: copy the specified file to the destination directorycp -r directory /path/to/destination
: copy the specified directory and its contents to the destination directory
$ cp file1.txt /home/user/Documents
$ ls /home/user/Documents
document1.txt document2.pdf directory2 file1.txt
$ cp -r directory1 /home/user/Documents
$ ls /home/user/Documents
directory1 document1.txt document2.pdf directory2 file1.txt
8. date
date
: displays the current date and time in the specified format. You can use various options to customize the output format.
$ date
Mon Apr 4 15:27:10 UTC 2023
$ date "+%Y-%m-%d"
2023-04-04
9. cat
cat
: display the contents of one or more files on the terminal. You can use it to concatenate and display multiple files, or create a new file from existing files.
$ cat file1.txt
This is the contents of file1.txt.
$ cat file1.txt file2.txt > newfile.txt
10. head
head
: display the first few lines of a file. By default, it displays the first 10 lines of the file. You can specify the number of lines to display using the-n
option.
Example:
$ head file.txt
This is the first line.
This is the second line.
This is the third line.
...
$ head -n 5 file.txt
This is the first line.
This is the second line.
This is the third line.
This is the fourth line.
This is the fifth line.
11. tail
tail
: display the last few lines of a file. By default, it displays the last 10 lines of the file. You can specify the number of lines to display using the-n
option.
$ tail file.txt
This is the 991st line.
This is the 992nd line.
This is the 993rd line.
...
$ tail -n 5 file.txt
This is the 996th line.
This is the 997th line.
This is the 998th line.
This is the 999th line.
This is the 1000th line.
12. mvmv
: move
mv
: move files or directories from one location to another. It can also be used to rename files/directories. You need to specify the source and destination file/directory paths.
$ mv file.txt documents/file.txt
$ mv file.txt newfile.txt
In conclusion, Linux commands are essential tools for any user working with the Linux operating system. Understanding and mastering these commands can greatly improve your efficiency and productivity when working on the command line. The commands covered in this article are just a few examples of the numerous commands available on Linux. With practice and exploration, you can discover many other powerful Linux commands that can help you to accomplish various tasks quickly and efficiently. Whether you are a Linux system administrator, a software developer, or a power user, having a strong understanding of Linux commands is crucial for working effectively with the operating system.