Introduction
The Linux command line, also known as the terminal or shell, is a powerful tool that provides users with direct access to the operating system’s functions. Unlike graphical user interfaces (GUIs), the command line allows for more precise and flexible control over the system. Whether you are a system administrator, developer, or a hobbyist, mastering the command line can significantly enhance your productivity and understanding of Linux. This guide will introduce you to the basics of using the Linux command line.
Getting Started
Opening the Terminal
To begin using the command line, you first need to open the terminal. The method to do this varies depending on your Linux distribution:
- Ubuntu/Debian: Press
Ctrl + Alt + T
. - Fedora: Press
Ctrl + Alt + T
or search for “Terminal” in the activities menu. - CentOS/RHEL: Search for “Terminal” in the applications menu.
Once the terminal is open, you will see a prompt, which typically looks something like this:
username@hostname:~$
This prompt indicates that the terminal is ready to accept commands.
Basic Commands
Here are some fundamental commands to get you started:
1. pwd (Print Working Directory)
The pwd
command displays the current directory you are in.
$ pwd
/home/username
2. ls (List Directory Contents)
The ls
command lists the files and directories in the current directory.
$ ls
Desktop Documents Downloads Music Pictures Videos
Options:
ls -l
: Lists files in a long format.ls -a
: Includes hidden files in the listing.
3. cd (Change Directory)
The cd
command changes the current directory.
$ cd Documents
$ pwd
/home/username/Documents
To go back to the home directory:
$ cd ~
To go up one directory level:
$ cd ..
4. mkdir (Make Directory)
The mkdir
command creates a new directory.
$ mkdir new_folder
5. rmdir (Remove Directory)
The rmdir
command removes an empty directory.
$ rmdir new_folder
6. touch (Create Empty File)
The touch
command creates an empty file or updates the timestamp of an existing file.
$ touch newfile.txt
7. cp (Copy Files and Directories)
The cp
command copies files or directories.
$ cp source_file.txt destination_file.txt
To copy a directory:
$ cp -r source_directory/ destination_directory/
8. mv (Move/Rename Files and Directories)
The mv
command moves or renames files and directories.
To move a file:
$ mv file.txt /new/location/
To rename a file:
$ mv oldname.txt newname.txt
9. rm (Remove Files and Directories)
The rm
command removes files or directories.
To remove a file:
$ rm file.txt
To remove a directory and its contents:
$ rm -r directory/
Viewing and Editing Files
1. cat (Concatenate and Display Files)
The cat
command displays the contents of a file.
$ cat file.txt
2. less (View File Contents One Screen at a Time)
The less
command allows you to view file contents one screen at a time.
$ less file.txt
3. nano (Text Editor)
nano
is a simple text editor for quick edits.
$ nano file.txt
4. vim (Text Editor)
vim
is a more powerful text editor, suitable for advanced users.
$ vim file.txt
Getting Help
If you need help with a command, you can use the man
(manual) command.
$ man ls
Alternatively, many commands support the --help
option.
$ ls --help
Conclusion
The Linux command line may seem intimidating at first, but with practice, it becomes an invaluable tool. The commands covered in this guide are just the beginning. As you become more comfortable, you’ll discover the true power and flexibility of the command line. Happy learning!