Cybernetic Odyssey : Ep-4 Getting Started with Linux Commands

12 min read
Cybernetic Odyssey : Ep-4 Getting Started with Linux Commands

Welcome back to Cybernetic Odyssey, your go-to resource for demystifying the world of cybersecurity. In this episode, we'll explore some essential Linux commands that every cybersecurity enthusiast should know. Linux is widely used in the cybersecurity field due to its power, flexibility, and security features. Whether you're a beginner or an experienced user, these commands will help you navigate through the Linux environment efficiently and securely.

Let's dive right in!


Linux Commands

Linux commands are text-based instructions you give to your computer's operating system, like telling a highly obedient robot what to do. They perform various tasks, such as listing files (ls), navigating folders (cd), creating directories (mkdir), making copies (cp), moving files (mv), deleting files (rm), displaying file content (cat), searching for text (grep), and adjusting permissions (chmod). These commands are entered into the terminal, a special text interface, and when you press Enter, your computer executes the task. While they may seem daunting initially, mastering these commands empowers you to efficiently control your Linux system and accomplish various tasks.


ls

The ls command in Linux is used to list files and directories in the current directory. It provides a comprehensive view of the contents, including file permissions, sizes, and timestamps. Options such as -l display detailed information, and -a shows hidden files starting with a dot.

Terminal
ls

cd

The cd command allows you to navigate the file system by changing your current working directory. Simply type cd followed by the directory name to move to a new location. No specific options are needed for basic directory navigation.

Terminal
cd Downloads

pwd

pwd displays the absolute path of your current working directory. This command is useful for identifying your location within the file system and does not require any options.

Terminal
pwd

mkdir

With mkdir, you can create new directories in the file system. Simply provide the directory name as an argument. Options like -p allow you to create parent directories if they don't exist.

Terminal
mkdir new_directory

rmdir

The rmdir command is used to delete empty directories. Specify the directory name as an argument.

Terminal
rmdir empty_directory

touch

Creates a new empty file. It is often used to create an empty file with a specified name or update the access and modification timestamps of an existing file.  

To add contents to the file you can use the echo command or use any text editor.

Terminal
touch new_file.txt

echo

echo is a versatile command for printing text to the terminal. It is commonly used in shell scripts to display messages or variables. Options are not necessary for basic text output, but -e can be used for interpreting escape sequences.

Terminal
echo "This is a new line of text" >> new_file.txt

cat

Displays the contents of a file. It is used to view the content of a text file in the terminal. It can also be used to concatenate and display multiple files' contents.

Terminal
cat new_file.txt

Output

Terminal
This is a new line of text

cp

Copies a file or directory. You specify the source file/directory and the destination where you want to copy it to.

Terminal
cp file.txt /destinationPath

mv

Moves or renames a file or directory. It can be used to move a file/directory from one location to another or rename it by specifying the new name.

moving

Terminal
mv file.txt /newLocationPath

renaming

Terminal
mv old_name.txt new_name.txt

rm

Deletes a file or directory. Be cautious when using this command, as it can permanently delete files and directories. Use with care.

Terminal
rm file.txt

man

It is used to display manual pages (documentation) for other commands. It provides detailed information on how to use a command, its options, and its purpose.

Terminal
man ls

history

By simply typing history in the terminal, you can view a numbered list of commands with timestamps. This helps users recall and reuse past commands efficiently. You can also use specific options to customize its behavior, such as history -c to clear the history, history -d to delete specific entries, or history -n to display line numbers.

Terminal
history

whoami

Typing whoami in the terminal will display the username of the current user. This information is helpful for conditional logic in scripts or verifying the user's identity before executing privileged commands. Although it doesn't have many options, you can combine it with other commands or use it in scripts to tailor its functionality as needed.

Terminal
whoami

Output

Terminal
root

find

It is used to search for files and directories within a specified location based on various criteria.

It will search for sample.txt in GFG directory.

Terminal
find ./ABC -name sample.txt

Here is a list of some common option used with find command :-

  • -name pattern: Searches for files and directories with a specific name pattern. Replace pattern with the name you're looking for.
  • -type type: Specifies the type of file to search for, such as f for regular files or d for directories.
  • -mtime n: Finds files modified approximately n days ago.
  • -atime n: Finds files accessed approximately n days ago.

grep

It is used to search for text patterns in files or input text. It can be used for pattern matching and filtering.

Terminal
grep -i "unix" file.txt

Here is a list of some common options used with grep command :-

  • -i or --ignore-case: Ignores case distinctions in both the pattern and input files, making the search case-insensitive.

  • -v or --invert-match: Inverts the match, displaying lines that do not contain the specified pattern.

  • -l or --files-with-matches: Displays the names of files that contain the pattern but not the matching lines themselves.

  • -L or --files-without-match: Displays the names of files that do not contain the pattern.

  • -n or --line-number: Displays the line numbers along with matching lines.

  • -c or --count: Displays only the count of matching lines, not the actual lines.

  • -r or --recursive: Searches for patterns recursively in directories and subdirectories.


nano

nano is a simple, user-friendly text editor that is often recommended for beginners. It provides a basic and straightforward interface for text editing.

Terminal
nano fileName.txt

How to use:-

  • When you open a file with nano, you'll see the contents of the file displayed in the terminal window.

  • At the bottom of the screen, you'll find a list of common keyboard shortcuts for performing various actions, such as saving, quitting, and searching.

  • You can make your edits in the text area, and you'll see your changes in real-time.

  • To save your changes, you typically press Ctrl + O, confirm the file name, and then press Enter.

  • To exit nano, you usually press Ctrl + X.


vim

vim (Vi IMproved) is a powerful and highly configurable text editor that is known for its efficiency and extensibility. It has a steeper learning curve than nano but offers advanced features for experienced users.

Terminal
vim fileName.txt

How to use :-

  • When you open a file with vim, you'll see the contents of the file displayed in the terminal window.

  • vim operates in different modes, with the most common being "normal mode" for navigation and "insert mode" for editing. You start in normal mode.

  • To enter insert mode and start editing, press i in normal mode.

  • To save your changes and return to normal mode, press Esc, and then, to save the changes, type :w and press Enter.

  • To exit vim, while in normal mode, type :q and press Enter. To save and exit in one command, use :wq or to force save and exit, use :wq!.


useradd

To add a new user to the system.

Terminal
useradd username

passwd

This command allows a user to change their password or an administrator to change another user's password.

Terminal
passwd username

sudo

sudo (short for "superuser do") allows authorized users to execute commands with superuser (administrative) privileges.

Terminal
sudo [options] command [arguments]

Common options:

  • -l: List the allowed (or forbidden) commands for the current user.

  • -u: Run the command as a specified user.

Terminal
sudo nano /etc/config.conf

su

su (short for "switch user") allows you to switch to another user account, including the root (superuser) account.

Terminal
su [options] [username]

Common options:

  • -: Switch to the root user.
Terminal
su -
  • -l or --login: Start a login shell as the specified user.

Example - To switch to another user account, use su username.

Terminal
su username

chmod

chmod is used to change file permissions (read, write, execute) for files and directories in Linux.

Terminal
chmod [options] permissions file(s)

Common options:

  • -R: Recursively change permissions for directories and their contents.

  • +: Add permissions.

  • -: Remove permissions.

  • =: Set permissions explicitly.

Common permission symbols:

  • u (user): Owner of the file.

  • g (group): Group associated with the file.

  • o (others): Everyone else.

  • a (all): Equivalent to ugo.

Common permission symbols:

  • r (read): Permission to read the file.

  • w (write): Permission to write to the file.

  • x (execute): Permission to execute the file.

Example

Terminal
chmod u+rw file.txt

shutdown

shutdown command is used to gracefully shut down or restart the system.  

It allows you to schedule a system shutdown or restart, specify a message to be displayed to logged-in users, and set a delay before the action takes place.  

The shutdown command is typically used with administrative privileges, so you may need to run it as the root user or with sudo.

Terminal
shutdown [OPTIONS] [TIME] [MESSAGE]

OPTIONS:  

These are various options you can specify with the command. Some common options include:

  • -h: Halt the system (shut it down).

  • -r: Reboot the system.

  • -c: Cancel a previously scheduled shutdown.

  • -k: Send a warning message to users without actually shutting down or rebooting.

TIME:  

This is the time when the shutdown or reboot should occur. You can specify it in different formats:

  • now: Immediate shutdown or reboot.

  • +m: Shutdown or reboot in m minutes from the current time.

  • hh:mm: Specify a specific time in hours and minutes (24-hour format).

MESSAGE:  

You can include an optional message that will be sent to logged-in users before the shutdown or reboot. Enclose the message in quotes.

Example - To shut down the system immediately with a custom message:

Terminal
sudo shutdown -h now "System is going down for maintenance."

Command-Line Operators

In Linux and Unix-like operating systems, operators such as >>, >, <, |, and & are used in the command line to manipulate input and output streams, connect commands, and control processes. Here's an overview of these operators:

  • > (Redirect Output):  

  The > operator is used to redirect the standard output (stdout) of a command to a file. If the specified file already exists, it will be overwritten.  

Terminal
echo "dsd" > test
  • >> (Append Output):

  The >> operator is used to redirect the standard output (stdout) of a command and append it to a file. If the file does not exist, it will be created; if it does exist, the output is added to the end of the file.  

Terminal
echo "bssss" >> test
  • < (Redirect Input):

  The < operator is used to redirect the standard input (stdin) of a command from a file. It allows a command to read its input from a file rather than from the keyboard.  

  You can use < to provide input from input.txt to the cat command:

Terminal
cat < input.txt
  • | Piping:

  The | operator is used to create a pipe, which connects the standard output (stdout) of one command to the standard input (stdin) of another. This allows the output of one command to be used as the input for another.

Terminal
cat test | grep -i "abc"
  • & Ampersand Operator:

  The & operator is used to run a command in the background, allowing you to continue using the terminal without waiting for the command to complete.

Terminal
gedit &
  • ; (Command Separator):

  The ; operator is used to separate multiple commands on a single line. It allows you to execute multiple commands sequentially, one after the other.

Terminal
ls ; mkdir abc
  • && (Logical AND):

  The && operator is used to execute the second command only if the first command succeeds (returns a zero exit status).

Terminal
pwd && mkdir test && cd test && bad_command && ls
  • || (Logical OR):

  The || operator is used to execute the second command only if the first command fails (returns a non-zero exit status).

Terminal
bad_command || ls

These operators provide powerful ways to manipulate and control the flow of data and processes in the Linux command line. They are essential for tasks like input/output redirection, process management, and creating complex command pipelines.


Conclusion

Mastering these fundamental Linux commands is an essential step towards becoming a proficient cybersecurity practitioner. They lay the groundwork for efficiently managing files and directories within a Linux environment, a skillset that's invaluable when working with cybersecurity tools and systems.

As you continue your journey into the realm of cybersecurity, remember that Linux is not just an operating system but a powerful tool that can aid you in tasks ranging from file analysis to network monitoring and much more. By building a solid understanding of these commands, you're taking significant strides towards becoming a more effective and confident cybersecurity professional.

Follow us on social media

Logo
Copyright © 2024 CYUN. All rights reserved.