25 Essential Commands for Linux Command Line Beginners

Getting started with the Linux command line may seem daunting at first, but many of the commands you’ll need to use will become familiar to you sooner than you think. Let’s start with some introductory but very important commands.

ⓒ Getty Images Bank

First, open a command terminal or log in to a remote Linux server and a command prompt will appear. It can be a simple $, or something more complex like (george@system ~)$ that changes when you move from one directory to another. However, the user is located in the home directory. The first commands you need to know are pwd and ls.

pwd: Displays the current directory (e.g. /home/george).
ls: Lists files in the current location.
When you run ls for the first time, files may not be listed. That doesn’t mean the file isn’t there. It just means you have to make a little more effort to see the file. The ls command assumes that you do not want to see any files in your new home directory that begin with a dot (such as .bashrc). You can view some files using the ls -a command.

cat: Display the contents of a text file
To view the contents of a .bashrc file or text file, you can use the cat command. This command has nothing to do with meowing. cat is an abbreviation for concatenate, which will be discussed in more detail later.

more: Displays the text file one screen at a time.
less: You can display and replay a text file one screen at a time.

If you want to display a text file with many lines, you can use the cat command to display the file as the lines move quickly across the terminal window. To view these files one screen at a time, you can use the more or less command. This command is very similar, but the less command can be reversed using the up arrow key.

cd: Go to a specific directory or return to home
To move to another directory, use the cd command. For example, you can use the cd /tmp command to navigate to the /tmp directory. Returning to your home directory is much easier. Just type cd to go directly back to your home directory.

Touch: Create an empty file
To create a new file, you can use touch commands. This command (e.g. touch newfile) creates an empty file, but you can add more files to the list.

Echo: Display specified text
If you want to add some content to a new file, you can use the echo command to redirect the output to a file with a command like echo “read me”>newfile. Even if you run the same command again, the file will not change. The reason is that if you use just one >, the echo command will overwrite the previous content. Use >> to add a line to an existing file.

rm: delete file
To delete a file, use the rm command (e.g. rm myfile). As you can guess, you can only delete files that you own. Therefore, it is also a good idea to try using another ls command, ls -l. Rather than simply listing the files, this command displays them with many additional details, as in this example.

-rw-r–r–. 1 george george   8 Nov  6 13:28 newfile

You might be thinking, “What is this all about??” The – at the beginning means that we are looking at a regular file. That is, it is not a directory or file that points to another file. The rw- part means that the user (george in this example) This means that if other users are members of george’s group (if the group exists), they will only have read access (then one user in the same group as george). There are rarely more than one member. The final r- grants read access if anyone else logging into the system has access to the home directory, but in most cases this is usually not the case and only the account owner and the full-privileged root account can touch the file. there is.

mv: rename file
To rename a file, use the mv command (e.g. mv newfile oldfile). Including the full path not only allows you to rename the file, but also allows you to move the file to another location if you have write permission to that directory. To move the files to a publicly accessible /tmp directory, use the following command:

mv newfile /tmp

To move a file and rename it at the same time, you can use the following command:

mv newfile /tmp/oldfile

cp: copy file
To copy a file, use the cp command (e.g. cp thisfile thatfile). You can copy a file to another directory using its full path (e.g. cp myfile /tmp/yourfile).

passwd: Change password
If you need to change your password, use the passwd command. You’ll be prompted to enter your current password and then enter your new password twice. Don’t be surprised if it doesn’t work at first. Linux systems tend to have strict password settings. You want to avoid using words that are too long and generic. Therefore, it is a good idea to check whether you can remember the password you choose. Otherwise, you may need to reset your password with the help of someone with access to a strong root account.

Once you’re comfortable logging into your Linux system and looking at your account’s files, it’s time to try out some additional commands.

clear: Clear the terminal screen
One command you should try right away is the clear command. This command clears the terminal window. This command is useful when you want to see the output of a specific command without being distracted by other items that appear at startup.

head: Displays the top line of the text file.
tail: Displays the bottom line in a text file.

You can use the head and tail commands to view the top and bottom lines of a text file. Each of these commands displays 10 lines by default. Entering a command like “head .bashrc” will display the first 10 lines, while ‘tail .bashrc’ will display the last 10 lines. If you want to see only the bottom two commands in a file, you can use a command like this:

$ tail -2 .bashrc
alias byebye=”echo “Have a happy and very profitable day!”; exit”
alias c=clear

The Byebye alias sends a message wishing you a good day and then exits the terminal window or system (if logged in remotely). Semicolons separate commands and allow them to be executed on one line.

man : Provides a description of the function of the command
Another command you can use right away is the man command. Here, “man” means “manual.” In other words, it provides a description of the command the user inquires about. For example, typing “man pwd” will display a description of what the pwd command does, along with details about all command options.

date : View current date and time
Use the date command to see the current date and time. Details are displayed in output in the following format:

$ date
Sun Nov 10 01:32:00 PM EST 2024

cal: View the calendar for the current month
To view the current month’s calendar, use the cal command. To view the calendar for another month, add the month and year as follows.

$ cal 03 1949
March 1949
Su Mo Tu We Th Fri Sat
1  2  3  4  5
6  7  8  9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

whoami: Displays the user ID of the account being used
The whoami command displays the user ID associated with the account in use. Normally it’s so obvious that you don’t need to ask, but this command can be helpful if you’re switching accounts and want to be reminded which account you’re currently using.

alias: Set short names for frequently used commands
As you start using more complex commands, it’s a good idea to use the alias command to set short names for frequently used commands. For example, if you want to clear the screen by typing ‘c’ just once, you can make this possible by setting an alias. The aliases are as follows:

Alias c=clear

One complication is that if you want this alias to be available every time you log in, you’ll need to add it to your .bashrc file. It can be added with the following command.

echo “alias c=clear” >> .bashrc

One thing to note is that you should use >>, not >. Using only > overwrites the file, while >> adds the file.

History: View recent commands
To view recently executed commands, use the History command. The most recently executed commands appear last, but the list can contain up to 1,000 recently executed commands. To see how many commands are visible, you can display the settings with a command like this:

echo $HISTSIZE
1000

grep: Find a specific word or string in a text file
To find a specific word or string (group of words) in a text file, you can use the grep command to find command aliases in the .bashrc file, as follows:

$ grep alias ~/.bashrc

~/ means that when you run this command, it will look for the .bashrc file even if the user is not in the home directory. ~ refers to the home directory (e.g. /home/username).

To see all directories that can be checked when running a command, use the “echo $PATH” command, and the first matching command will be executed. To check the location of the command to be executed, use the following command.

$ which date
/usr/bin/date

sort: Sort the contents of a text file
To sort the contents of a text file, use the sort command as follows.

$ sort friends
Alice
Betty
Christopher
Diane
George
Patty
Ricky
Sam
Tim

mkdir : Create a new directory
To create a new directory (i.e. a subdirectory) in your home directory, use the mkdir command as follows:

mkdir reports

After creating a new subdirectory, you can navigate to it with the cd command.

ps: View running processes
To view running processes, use the ps command without any arguments. In the following example, the only two running processes are the bash shell and the ps command itself.

$ ps
PID TTY          TIME CMD
64681 pts/1    00:00:00 bash
68330 pts/1    00:00:00 ps

To see all processes running on your system, use one of the following ps commands: The correct command depends on the Linux distribution you are using.

$ ps -ef
ps -aux

To view a running process one screen at a time, you can use something called “pipe,” a special command option that allows you to send the output of one command to another. The following is the command to perform this task.

$ ps -ef | more

Here, the output of the ps -ef command is sent to the more command, which displays that output one screen at a time.

This is the beginning. I hope this piqued your interest in the Linux command line.
editor@itworld.co.kr

Source: www.itworld.co.kr