Linux is a powerful operating system with commands to help you manage and interact with your system. Whether you’re a beginner or an experienced user, mastering the most common Linux commands is essential for efficient and effective system administration. In this article, I tell you about my top 10 commands, and provide practical examples to get you started.
What’s a prompt?
Before getting started, you need to know what a “prompt” is. The command prompt on Linux usually looks like a dollar sign ($
), and it’s the computer’s way of telling you that it’s ready to receive a command. If you don’t see a prompt, then the computer’s currently running a command and can’t take another one, at least in that terminal. When this article tells you to type a command, never type in the very first $
because that’s meant to represent your computer’s prompt.
1. ls for “list”
The ls
command lists files and directories. It provides useful information like file size, ownership, and permissions. Open a terminal and type:
1 |
$ ls |
You can see hidden files and directories with the -a
option:
1 |
$ ls -a |
Get detailed information about each file and directory:
1 |
$ ls -l |
And you can sort items by modification time with the -t
option:
1 |
$ ls -lt |
2. cd for “change directory”
The cd
command permits you to change directories from your current directory to a new directory. It’s used to navigate through the file system.
1 |
$ cd |
You can move to a specific directory by providing the full path to where you want to go:
1 |
$ cd /path/to/directory |
Move just one directory up:
1 |
$ cd .. |
Move two directories up:
1 |
$ cd ../.. |
3. mkdir for “make directory”
The mkdir
command allows you to create directories within the file system.
1 |
$ mkdir new_directory |
You can make several directories at once, one inside the other, using the -p
option:
1 |
$ mkdir -p path/to/new_directory |
Or you can make several directories at one, next to one another:
1 |
$ mkdir -p path/to/new_directory |
4. rm for “remove”
The rm
command permanently removes files and directories. There is no un-remove command, so use this judiciously!
$ rm file.txt
Remove a directory:
1 |
$ rm -r dir1 |
5. cp for “copy”
The cp
command copies files and directories to your specified destination:
1 |
$ cp file.txt /path/to/destination |
Copy a whole directory and everything within it:
1 |
$ cp -r directory /path/to/destination |
6. mv for “move” (and “rename”)
Use the mv
command to move and rename files (renaming a file is really moving it to its same location but with a new name).
1 2 |
$ mv file.txt /path/to/destination $ mv directory /path/to/destination |
Rename a file:
1 |
$ mv old_name new_name |
7. grep for “filter”
The grep
command is a powerful tool for finding patterns in text. You can search the output of the ls
command to find a specific file in a long list:
1 2 |
$ ls | grep myImportantFile myImportFile.txt |
Or search the output of ls for all files containing a specific pattern of letters:
1 2 3 |
$ ls | grep jpg picture_001.jpg picture_002.jpg |
Alternately, you can search the text within a file for specific words and patterns:
1 |
$ grep pattern file.txt |
Search for a pattern in multiple files:
1 |
$ grep pattern file1.txt file2.txt |
Search for a pattern recursively in a directory
1 |
$ grep -r pattern /path/to/directory |
8. chmod for “change mode”
The chmod
command is used to change file and directory permissions.
Set read, write, and execute permissions for the user who owns the file:
1 |
$ chmod u=rwx file.txt |
Remove write permissions for the group and others:
1 |
$ chmod go-w file.txt |
Set read and execute permissions for all users:
1 |
$ chmod a=rx file.txt |
9. sudo for administration
The sudo
command allows you to execute a command with superuser privileges.
1 |
$ sudo command |
For example, when you need to edit a configuration file you don’t normally permission to edit as a regular user:
1 |
$ sudo nano /etc/file.conf |
10. man for “manual”
The man
‘command provides access to the user manual (documentation) for each command. These “man pages” provide detailed information on all the different options you have for a command.
1 |
$ man command |
Search for a keyword within manual pages:
1 |
$ man -k keyword |
To exit a man page, press Q on your keyboard.
More commands to discover
These 10 Linux commands are the bedrock of your journey as a Linux user. Familiarize yourself with these commands and their advanced options to gain greater control of your system. With great power comes great responsibility, too. Be careful how you use sudo
and rm
lest you wreak havoc on your system! And be sure you know where you are on the file system before executing a command. It’s always a good idea to use the pwd
command, which provides a printout to your display of your current position within the file system.
And this is just the beginning. The more you use Linux, the more useful commands you’ll discover. Start learning more today, and then consider writing an article about what you’ve found for Sysadmin Signal!
0 Comments